By GokiSoft.com| 16:01 11/03/2020|
Java Basic

Share Code- Chia sẻ code java overview - Interface trong java - Interface in 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 lession8.inf;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        //interface là gì => tại sao chúng ta phải sử dụng interface
        //mọi yêu cầu mọi cải tiến => xuất phát từ nhu cầu
        //interface => xuất phát từ nhu cầu của lập trình viên
        //nếu ko có nhu => thì chẳng tạo ra cái j cả.
        //Vi dụ : viết 1 chương quản lý toàn bộ hành động running của tất cả các 
        //loại đối tượng khác nhau từ xe cộ, con người, sinh viên, động vật, meo
        //tất cả đều có chung 1 phương thức là onRunning()
        List<Dog> dogs = new ArrayList<>();
        dogs.add(new Dog());
        dogs.add(new Dog());
        
        List<Cat> cats = new ArrayList<>();
        cats.add(new Cat());
        
        List<Car> cars = new ArrayList<>();
        cars.add(new Car());
        cars.add(new Car());
        
        List<Student> students = new ArrayList<>();
        students.add(new Student());
        
        //neu nhu chung tao tao 4 mang quan ly 4 loai doi tuong khac nhau
        //cung dc => ko sai => ko van de j ca.
        for (Student student : students) {
            student.onRunning();
        }
        
        for (Car car : cars) {
            car.onRunning();
        }
        
        for (Cat cat : cats) {
            cat.onRunning();
        }
        
        for (Dog dog : dogs) {
            dog.onRunning();
        }
        //nhu cau cua lap trinh => tao ra 1 List quan ly ca 4 class object tren
        //thi lam the nao
        //Giai phap dau tien >> cac ban se nghi toi tinh da hinh trong lap trinh OOP
        //Cat, Dog, Car, Student cung ke thua tu 1 class object cha
        //Dieu nay ve lap trinh => ko sai => OK
        //Nhung sai ve ban chat => y nghia trong doi tuong, y nghia thuc tien => sai
        //Nghi toi 1 giai phap hoan thien hon
        //nhom dc cac hanh dong chung => cua nhieu doi tuong lai thanh 1
        //Interface
        List<IRunning> runnings = new ArrayList<>();
        runnings.add(new Dog());
        runnings.add(new Dog());
        runnings.add(new Car());
        runnings.add(new Student());
        runnings.add(new Student());
        runnings.add(new Cat());
        runnings.add(new Student());
        
        for (IRunning running : runnings) {
            running.onRunning();
        }
    }
}



/*
 * 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 lession8.inf;

/**
 *
 * @author Diep.Tran
 */
public class Car implements IRunning{
    @Override
    public void onRunning() {
        System.out.println("Car is running");
    }
}



/*
 * 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 lession8.inf;

/**
 *
 * @author Diep.Tran
 */
public class Cat implements IRunning{
    @Override
    public void onRunning() {
        System.out.println("Cat is running");
    }
}



/*
 * 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 lession8.inf;

/**
 *
 * @author Diep.Tran
 */
public class Dog implements IRunning{
    @Override
    public void onRunning() {
        System.out.println("Dog is running");
    }
}



/*
 * 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 lession8.inf;

/**
 *
 * @author Diep.Tran
 */
public interface IRunning {
    void onRunning();
}



/*
 * 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 lession8.inf;

/**
 *
 * @author Diep.Tran
 */
public class Student implements IRunning{
    @Override
    public void onRunning() {
        System.out.println("Student is running");
    }
}


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

5

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