By GokiSoft.com| 20:06 08/07/2021|
Java Basic

[Share Code] Tìm hiểu tính chất kế thừa và tính chất đa hình trong lập trình Java - C2010L



Nội dung kiến thức:
- Tính chất trong lập trình OOP
	- Tính chất bao đóng: OK
	- Tính chất kế thừa: OK
		- override
		- overloading
	- Tính chất đa hình
	- Tính chất trừu tượng
		- Ly do su dung tinh chat truu tuong:
			- Xuat phat -> Than cua method/function -> ko co chua code -> Abstract
			- Yeu cau bat buoc child class -> override nhung phuong khong chua code.
- Interface:
	Xay dung 1 chuong quan ly Dog, Cat, Tiger, Bear, Student, Car
	Quan ly am thanh cua tat ca cac doi tuong tren: Dog, Cat, Tiger, Bear, Student, Car -> Trong 1 ArrayList.
==================================================
Mini Project: Xay dung 1 chuong quan ly dong
	Dong vat: Dog, Cat, Tiger, Bear, ...
Yeu cau chuong trinh:
	- Su dung duoc tinh chat ket thua & tinh chat bao dong
	- Chuong trinh quan ly am thanh cua danh sach dong vat tren
		- ArrayList -> Quan ly dc tat ca cac dong vat trong 1 mang -> Duyet qua chuc nang (method/function) -> showSound

B1. Phan tich du an
- Dog:
	Thuoc tinh
		name -> String
		color -> String
		birthday -> String
	Hanh dong (method/function)
		input
		display
		showSound
- Cat:
	Thuoc tinh
		name -> String
		color -> String
		birthday -> String
	Hanh dong (method/function)
		input
		display
		showSound
- Tiger
- Bear

Nhan xet: Animal
	Thuoc tinh
		name -> String
		color -> String
		birthday -> String
	Hanh dong (method/function)
		input
		display
		showSound
Dog, Cat, Tiger, Bear -> extends -> Animal

B2. Code (Viet chuong trinh)




#Tiger.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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Tiger extends Animal{

    @Override
    public void showSound() {
        System.out.println("Hummm .. hummm ..");
    }
    
}


#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 lesson04;

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        ArrayList<ISound> list = new ArrayList<>();
        
        ISound a = new Dog();
        list.add(a);
        
        a = new Cat();
        list.add(a);
        
        a = new Tiger();
        list.add(a);
        
        a = new Bear();
        list.add(a);
        
        a = new Student();
        list.add(a);
        
        a = new Car();
        list.add(a);
        
        for (ISound iSound : list) {
            iSound.showSound();
        }
    }
}


#Student.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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Student implements ISound{
    @Override
    public void showSound() {
        System.out.println("Sound .. Student ");
    }
}


#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 lesson04;

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Khai bao tuong -> OK
//        Dog dog = new Dog();
//        dog.showMsg();
//        dog.showMsg("ABC");
//        dog.testDog();
//        
//        Cat cat = new Cat();
//        cat.showSound();
//        cat.testCat();
//        
        Animal a2 = new Animal() {
            @Override
            public void showSound() {
                System.out.println("Ho .. ho ..");
                
                testHo();
            }
            
            void testHo() {
                System.out.println("dfsdfjsd fsd fsdfsdf");
            }
        };
        a2.showSound();
        
        //Khai bao cach moi -> tinh chat da hinh.
//        Animal a2 = new Dog();
//        a2.showSound();
//        if(a2 instanceof Dog) {
//            ((Dog) a2).testDog();
//        }
//        
//        Animal a3 = new Cat();
//        a3.showSound();
//        if(a3 instanceof Cat) {
//            ((Cat) a3).testCat();
//        }
        
        ArrayList<Animal> list = new ArrayList<>();
        Animal a1 = new Dog();
        list.add(a1);
        
        a1 = new Cat();
        list.add(a1);
        
        a1 = new Tiger();
        list.add(a1);
        
        a1 = new Bear();
        list.add(a1);
        
        a1 = new Dog();
        list.add(a1);
        
        a1 = new Cat();
        list.add(a1);
        
        for (Animal animal : list) {
            animal.showSound();
        }
        
        
    }
}


#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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public interface ISound{
    void showSound();
}


#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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Dog extends Animal{

    @Override
    public void showSound() {
        System.out.println("Go .. go ..");
    }
    
    public void showMsg() {
        System.out.println("Hello World!!!");
    }
    
    public void showMsg(String msg) {
        System.out.println("Hello World!!! " + msg);
    }
    
    public void showMsg(String msg, String msg02) {
        System.out.println("Hello World!!! " + msg + ", " + msg02);
    }
    
    public void testDog() {
        System.out.println("Test .. dog");
    }
}


#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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Cat extends Animal{

    @Override
    public void showSound() {
        System.out.println("Meo .. meo ..");
    }
    
    public void testCat() {
        System.out.println("Test .. cat");
    }
}


#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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Car implements ISound{
    @Override
    public void showSound() {
        System.out.println("Sound ... Car");
    }
}


#Bear.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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Bear extends Animal{

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


#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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public abstract class Animal implements ISound{
    String name, color, birthday;

    public Animal() {
    }

    public Animal(String name, String color, String birthday) {
        this.name = name;
        this.color = color;
        this.birthday = birthday;
    }

    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;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    
    public void input() {
        System.out.println("Nhap thong tin dong vat");
    }
    
    public void display() {
        System.out.println("Hien thi thong tin dong vat");
    }
    
    /**
     * Ham chung chung
     * Animal co am thanh ko? => Co
     * Am thanh cua no la gi -> ko biet
     *  Dog -> Go .. go
     *  Cat -> Meo .. meo
     *  Tiger -> hummm .. hummm ..
     *  Bear -> ...
     */
    /***
     * Phuong thuc truu tuong.
     */
    public abstract void showSound();
}


#Abc.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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Abc extends Dog{
    
}


Tags:

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

5

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