thể trùng lặp:
How are Java interfaces actually used?Có điểm nào thực sự đối với giao diện Java không?
Tôi không nói từ một điểm từ thông dụng accademic của xem nhưng từ một điểm phát triển thiết thực của quan điểm.
Vì vậy, lấy ví dụ: -
Class1 implements Interface
public String methodOne() {
return "This is Class1.methodOne()";
}
public String methodTwo() {
return "This is Class1.methodTwo()";
}
}
Class2:
Class2 implements Interface
public String methodOne() {
return "This is Class2.methodOne()";
}
public String methodTwo() {
return "This is Class2.methodTwo()";
}
}
Sử dụng giao diện: -
Client {
Interface intface = new Class1();
intface.methodOne();
intface.methodTwo();
Interface intface = new Class2();
intface.methodOne();
intface.methodTwo();
}
Nhưng những lợi ích trên chỉ writting là gì: -
Client {
Class1 clas1 = new Class1();
clas1.methodOne();
clas1.methodTwo();
Class2 clas2 = new Class2();
clas2.methodOne();
clas2.methodTwo();
}
Và bỏ qua các giao diện hoàn toàn.
Giao diện dường như là một lớp mã bổ sung vì lợi ích của một lớp mã bổ sung, hoặc có nhiều hơn cho chúng chứ không chỉ "Đây là các phương thức mà lớp bạn đang truy cập có"?
http://stackoverflow.com/questions/504904/how-are-java-interfaces-actually-used – skaffman