By GokiSoft.com| 17:14 24/02/2021|
Java Basic

[Share Code] Tìm hiểu lập trình hướng đối - tính chất đa hình - tích chất trừu tượng - interface - Java basic


- Tích chất trong lập trình OOP - Tính chất đóng gói: OK - Tính chất kế thừa: OK - Tính chất đa hình - Tính chất trừu tượng - Khi class có ít nhất 1 method không có phần body -> chuyển method đó về abstract method. Lúc đó bắt buộc class -> chuyển về abstract class => Các xác định class có phải là abstract hay không? - Không khởi tạo được object trực tiếp từ abstract class => Chú ý. - Tại sao lại sử dụng tới abstract class? - Bắt buộc override abstract method trong lớp con. (child class) - Tìm hiểu interface - Phân biệt interface & abstract? - Sử dụng interface thay cho abstract được không? - Sử dụng abstract thay cho interface được không? ======================================================== Quản lý động vật: - Animal: class chung - Thuộc tính: name, foodType, age - Hành động (method, function) - showSound() -> Hiển thị ra tiếng kêu của động vật - running() - sleeping() - Dog - Cat Code test về khai báo động vật Quản lý danh sách động - Thiết kế thêm các đối tượng: - Car Thuộc tính: name, color Hành động: - showSound - People Thuộc tính: name, age Hành động: - showSound - running() => Quản lý toàn bộ âm thanh (showSound) của tất cả các đối tượng Dog, Cat, Car, People trong mảng. => Giải pháp là gì trong TH này???




#Animal.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

/**
 *  Khong tao dc 1 object tu abstract class.
 * @author DiepTV
 */
public abstract class Animal implements ISound{
    String name, foodType;
    int age;

    public Animal() {
    }

    public Animal(String name, String foodType, int age) {
        this.name = name;
        this.foodType = foodType;
        this.age = age;
    }
    
    /**
     * showSound() -> chac chan -> dong vat nao cung se co.
     */
//    public abstract void showSound();
    /**public void showSound() {
        //Xac dinh dc am thanh cua dong vat.
        //Am thanh cua moi dong vat la khac nhau -> ko the mo ta chinh xac dc am thanh cua dong vat
        //Khai bao ra => class con se dung toi.
        //Khong co phan body -> ko biet code gi o day. -> chuyen method -> abstract method
    }*/
    
    public void running() {
        System.out.println("running ...");
    }
    
    public void sleeping() {
        System.out.println("sleeping ...");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFoodType() {
        return foodType;
    }

    public void setFoodType(String foodType) {
        this.foodType = foodType;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Animal{" + "name=" + name + ", foodType=" + foodType + ", age=" + age + '}';
    }
}


#Car.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

/**
 *
 * @author DiepTV
 */
public class Car implements ISound{
    String name, color;

    public Car() {
    }

    public Car(String name, String color) {
        this.name = name;
        this.color = color;
    }
    
    @Override
    public void showSound() {
        System.out.println("Abc...");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}


#Cat.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

/**
 *
 * @author DiepTV
 */
public class Cat extends Animal{
    @Override
    public void showSound() {
        System.out.println("Meo ... meo ...");
    }
}


#Dog.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

/**
 *
 * @author DiepTV
 */
public class Dog extends Animal{

    @Override
    public void showSound() {
        System.out.println("Go .. go ..");
    }
    
    public void showMessage(String msg) {
        System.out.println("Xin chao: " + msg);
    }
}


#ISound.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

/**
 *
 * @author DiepTV
 */
public interface ISound {
    void showSound();
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

import java.lang.reflect.Array;
import java.util.ArrayList;

/**
 *
 * @author DiepTV
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Cat cat = new Cat();
        cat.showSound();
        
        Dog dog = new Dog();
        dog.showSound();
        //Tinh chat da hinh
        Animal animal = new Dog();
        //animal -> parent class -> khoi tao ra tu parent class hoac children class.
        //animal -> kieu du lieu la Animal
        //animal = new Dog() -> animal => chinh la doi tuong Dog.
        animal.showSound();
        //dieu dac biet gi trong tinh da hinh
//        animal.showMessage("123");//Trong TH nay goi toi ham showMessage bang cach nao
        //Kiem tra xem animal co khoi tao tu Dog class ko???
        if(animal instanceof Dog) {
            ((Dog) animal).showMessage("123");
        }
        
        //Su dung trong 1 so truong hop (tuy thuoc vao nghiep)
        //Khai bao 1 mang animalList -> quan ly dc tat ca cac loai dong vat: Cat, Dog, Tiger, Beer, ...
        ArrayList<Animal> animalList = new ArrayList<>();
        
        animalList.add(cat);
        animalList.add(dog);
        animalList.add(animal);
        
//        Animal a = new Animal();
//        animalList.add(a);
        
//        animalList.add(new Animal()); //animaList.get(4) -> cach viet nhanh
        System.out.println("====== Test showSound() -> Tinh da hinh");
        for (Animal animal1 : animalList) {
            animal1.showSound();
            if(animal1 instanceof Dog) {
                ((Dog) animal1).showMessage("12312");
            }
        }
    }
    
}


#People.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

/**
 *
 * @author DiepTV
 */
public class People implements ISound{
    String name;
    int age;

    public People() {
    }

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    @Override
    public void showSound() {
        System.out.println("HH ...");
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


#Test.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson06;

import java.util.ArrayList;

/**
 *
 * @author DiepTV
 */
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.showSound();
        
        Cat cat = new Cat();
        cat.showSound();
        
        Car car = new Car();
        car.showSound();
        
        People people = new People();
        people.showSound();
        
        ArrayList<ISound> sounds = new ArrayList<>();
        sounds.add(car);
        sounds.add(dog);
        sounds.add(cat);
        sounds.add(people);
        System.out.println("============== Sound");
        for (ISound sound : sounds) {
            sound.showSound();
        }
    }
}


Tags:

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

5

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