java: Program to an interface, not an implementation?

2007-05-17 4:49 am
Program to an interface, not an implementation 點解?

thanks

回答 (2)

2007-05-17 7:10 am
✔ 最佳答案
Interface 其實指一個abstract class,內裡只有static variable 和 function 的 declaration,而沒有function implementation,以及instance variables。如:

Interface anInterface {
static public final int CONST = 4;
int aMethod();
}

要使用的話,就要像以下的作法:

public class aClass implements anInterface {
int aMethod() { return; }
}

Interface 的出現,其實是為了要解決Multiple Inheritance 的問題。因Java 只限INherit from 一個class,要做到Multiple Inheritance的效果就要用Interface。
(以上…各位不明可以不理 XD)

Java 中有很多Interface,如Listener Interface 就是。
2007-05-20 7:32 am
Interface like an abstract class but NOT an abstract class! Interface does not have "static variable" but "public static final constant"!


收錄日期: 2021-04-13 00:30:05
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070516000051KK04182

檢視 Wayback Machine 備份