By GokiSoft.com| 19:28 22/04/2024|
Java Basic

[Share Code] Tìm hiểu tính chất trừu tượng & interface trong Java - C2307L

Nội dung kiến thức:
	Tính chất trong lập trình OOP
		- T/c bao đóng -> DONE
		- T/c kế thừa -> DONE
		- T/c đa hình -> DONE

		- T/c trừu tượng -> Today
			Khi nào thì sử dụng t/c trừu tượng
				- Class Object -> Phương thức (Không biết viết gì cho phần thân) -> Chuyển nó về Abstract methods => Class Abstract
				- Tất cả các lớp con -> Cần ghe đè (override) -> Phương thức từ lớp cha -> abstract methods

			- Interface -> Today

===============================================================
Đề bài: Xây dựng 1 dự án quản lý diện tích và chu vi của tất cả các hình học
	Shape
		tinhDienTich
		tinhChuVi
	HinhTron
		Thuộc tính:
			radius
		Hàm:
			tinhDienTich
			tinhChuVi
	HinhChuNhat
		Thuộc tính:
			width
			height
		Hàm:
			tinhDienTich
			tinhChuVi

Đề bài: Xây dựng dự án quản lý âm thanh/tiếng kêu của động vật (Bách khoa toàn thư về âm thanh/tiếng kêu của động vật)
	Animal
		showSound
	Dog
	Cat
	Chicken
	...

Đề bài: Xây dựng dự án quản lý âm thanh của động vật, người, xe cộ. Yêu cầu là sử dụng 1 mảng để quản lý tất cả động vật, người, xe cộ đó.
	3 nhóm đối tượng khác nhau
		Động vật
		Người
		Xe cộ
		-> Ko nên sử dụng t/c đa hình để quản lý
		-> TH quản lý hành động của các nhóm đối tượng khác nhau -> Thì làm thế nào???
			-> interface

#Test01.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson06;

import java.util.ArrayList;

/**
 *
 * @author diepvan
 */
public class Test01 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        Cat cat = new Cat();
        Chicken chicken = new Chicken();
        
        dog.showSound();
        cat.showSound();
        chicken.showSound();
        //Yeu cau su dung 1 mang de quan ly tat ca cac doi tuong tren
        //Tinh chat => da hinh
        ArrayList<Animal> animals = new ArrayList<>();
        animals.add(dog);
        animals.add(cat);
        animals.add(chicken);
        
        for (Animal animal : animals) {
            animal.showSound();
        }
        
        
        People p = new People();
        p.showSound();
        
        Car c = new Car();
        c.showSound();
        
        //Yeu cau su dung 1 mang de quan ly tat ca cac doi tuong tren
        //Giai phap la gi???
        ArrayList<ISound> objList = new ArrayList<>();
        objList.add(dog);
        objList.add(cat);
        objList.add(chicken);
        objList.add(c);
        objList.add(p);
        
        for (ISound sound : objList) {
            sound.showSound();
        }
    }
    
    public static interface ISound {
        //Tat ca cac phuong thuc trong Interface no gom t/c sau
        //1. Phuong thuc truu tuong (abstract method)
        //2. Tat ca cac phuong thuc do deu la: public -> Moi phuong deu la public (private, protected, default -> Error)
        //3. Tat ca cac bien trong interface -> bien hang (final)
        int x = 10;//const -> final int
        int y = 20;//const -> final int
        void showSound();
    }
    
    /**
     * 1 class object -> ke thua nhieu interface
     */
    public static class People implements ISound{
        @Override
        public void showSound() {
            System.out.println("Am thanh nguoi ...");
        }
    }
    
    public static class Car implements ISound{
        @Override
        public void showSound() {
            System.out.println("Am thanh oto ...");
        }
    }
    
    public static abstract class Animal implements ISound{
//        public abstract void showSound();
    }
    
    public static class Dog extends Animal {

        @Override
        public void showSound() {
            System.out.println("Go ... go ...");
        }
        
    }
    
    public static class Cat extends Animal {

        @Override
        public void showSound() {
            System.out.println("Meo ... meo ...");
        }
        
    }
    
    public static class Chicken extends Animal {

        @Override
        public void showSound() {
            System.out.println("Cuc tac ...");
        }
        
    }
}

#HinhVuong.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson06;

/**
 *
 * @author diepvan
 */
public class HinhVuong extends Shape{
    double width;

    public HinhVuong() {
    }

    public HinhVuong(double width) {
        this.width = width;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    @Override
    public double tinhChuVi() {
        return 4 * width;
    }

    @Override
    public double tinhDienTich() {
        return width * width;
    }
}

#HinhTron.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson06;

/**
 *
 * @author diepvan
 */
public class HinhTron extends Shape {

    double radius;

    public HinhTron() {
    }

    public HinhTron(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double tinhDienTich() {
        return Math.PI * radius * radius;
    }

    @Override
    public double tinhChuVi() {
        return 2 * Math.PI * radius;
    }
}

#HinhChuNhat.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson06;

/**
 *
 * @author diepvan
 */
public class HinhChuNhat extends Shape {

    double width;
    double height;

    public HinhChuNhat() {
    }

    public HinhChuNhat(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double tinhChuVi() {
        return 2 * (width + height);
    }

    @Override
    public double tinhDienTich() {
        return width * height;
    }
}

#Shape.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson06;

/**
 *
 * @author diepvan
 */
public abstract class Shape {

    public Shape() {
    }

    public abstract double tinhChuVi();

    public abstract double tinhDienTich();
}

#Test.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson06;

/**
 *
 * @author diepvan
 */
public class Test {
    public static void main(String[] args) {
        HinhChuNhat hcn = new HinhChuNhat(5, 10);
        System.out.println("Dien tich: " + hcn.tinhDienTich());
    }
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package lesson06;

/**
 *
 * @author diepvan
 */
public class Main {
    public static void main(String[] args) {
        HinhChuNhat hcn = new HinhChuNhat(22f, 17f);
        HinhTron ht = new HinhTron(22.7f);
        
        //Cach 1
        double dt = hcn.tinhDienTich() + ht.tinhDienTich();
        System.out.println("Tong dien tich: " + dt);
        
        //Cach 2
        Shape[] list = new Shape[2];
        list[0] = hcn;
        list[1] = ht;
        
        dt = tinhTongDienTich(list);
        System.out.println("Tong dien tich: " + dt);
    }
    
    public static double tinhTongDienTich(Shape[] list) {
        double dt = 0;
        
        for (Shape shape : list) {
            dt += shape.tinhDienTich();
        }
        
        return dt;
    }
    
    public static class Shape {
        public Shape() {
        }
        
        public double tinhChuVi() {
            return 0;
        }
        
        public double tinhDienTich() {
            return 0;
        }
    }
    
    public static class HinhTron extends Shape {
        double radius;

        public HinhTron() {
        }

        public HinhTron(double radius) {
            this.radius = radius;
        }
        
        public double getRadius() {
            return radius;
        }

        public void setRadius(double radius) {
            this.radius = radius;
        }

        @Override
        public double tinhDienTich() {
            return Math.PI * radius * radius;
        }

        @Override
        public double tinhChuVi() {
            return 2 * Math.PI * radius;
        }
    }
    
    public static class HinhChuNhat extends Shape {
        double width;
        double height;

        public HinhChuNhat() {
        }

        public HinhChuNhat(double width, double height) {
            this.width = width;
            this.height = height;
        }

        public double getWidth() {
            return width;
        }

        public void setWidth(double width) {
            this.width = width;
        }

        public double getHeight() {
            return height;
        }

        public void setHeight(double height) {
            this.height = height;
        }

        @Override
        public double tinhChuVi() {
            return 2 * (width + height);
        }

        @Override
        public double tinhDienTich() {
            return width * height;
        }
        
    }
}
Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)