先看一個(gè)關(guān)于接口的測(cè)試代碼:
/**
*
* @author examda
*/
interface Test {
public void test();
@Override
public int hashCode();
@Override
public String toString();
}
class TestInterface implements Test {
public void test() {
}
}
public class Main {
public static void main(String[] args) {
Object obj = new Object();
Test test = new TestInterface();
System.out.println(test.toString());
System.out.println(test.hashCode());
System.out.println(test.equals(test));
}
}
我故意保留了NetBeans IDE幫我生成的 @Override標(biāo)記.這個(gè)地方看起來有些怪,按ide的提示,好像接口定義中的hashCode()方法和toString()方法重寫了Object中的對(duì)應(yīng)方法,不過我感覺這么理解很容易給人造成一種混亂,或者假象.例子中還可以看出,通過接口定義的引用變量,可以直接使用Object中定義的方法.
為什么呢?
初學(xué)的時(shí)候接受了The Java Programming Language一書的說法,
You can invoke any of the Object methods using a reference of an interface type because no matter what interfaces the object implements, it is always an Object and so has those methods. In fact, any interface that does not extend some other interface implicitly has members matching each of the public methods of Object (unless the interface explicitly overrides them).
后面也簡(jiǎn)單提了一下實(shí)現(xiàn)機(jī)制.Java語言規(guī)范中說的比較清楚:
The members of an interface are:
Those members declared in the interface.
Those members inherited from direct superinterfaces.
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
It follows that is a compile-time error if the interface declares a method with a signature that is override-equivalent (§8.4.2) to a public method of Object, but has a different return type or incompatible throws clause.
The interface inherits, from the interfaces it extends, all members of those interfaces, except for fields, classes, and interfaces that it hides and methods that it overrides.
原來如此!
奇怪的是,反編譯接口的class文件并沒有發(fā)現(xiàn)什么特殊的地方(除了常量池中能看到"java.lang.Object"),還是不了解編譯器具體是如何處理的.希望隨著學(xué)習(xí)的深入能解決這個(gè)問題.
/**
*
* @author examda
*/
interface Test {
public void test();
@Override
public int hashCode();
@Override
public String toString();
}
class TestInterface implements Test {
public void test() {
}
}
public class Main {
public static void main(String[] args) {
Object obj = new Object();
Test test = new TestInterface();
System.out.println(test.toString());
System.out.println(test.hashCode());
System.out.println(test.equals(test));
}
}
我故意保留了NetBeans IDE幫我生成的 @Override標(biāo)記.這個(gè)地方看起來有些怪,按ide的提示,好像接口定義中的hashCode()方法和toString()方法重寫了Object中的對(duì)應(yīng)方法,不過我感覺這么理解很容易給人造成一種混亂,或者假象.例子中還可以看出,通過接口定義的引用變量,可以直接使用Object中定義的方法.
為什么呢?
初學(xué)的時(shí)候接受了The Java Programming Language一書的說法,
You can invoke any of the Object methods using a reference of an interface type because no matter what interfaces the object implements, it is always an Object and so has those methods. In fact, any interface that does not extend some other interface implicitly has members matching each of the public methods of Object (unless the interface explicitly overrides them).
后面也簡(jiǎn)單提了一下實(shí)現(xiàn)機(jī)制.Java語言規(guī)范中說的比較清楚:
The members of an interface are:
Those members declared in the interface.
Those members inherited from direct superinterfaces.
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
It follows that is a compile-time error if the interface declares a method with a signature that is override-equivalent (§8.4.2) to a public method of Object, but has a different return type or incompatible throws clause.
The interface inherits, from the interfaces it extends, all members of those interfaces, except for fields, classes, and interfaces that it hides and methods that it overrides.
原來如此!
奇怪的是,反編譯接口的class文件并沒有發(fā)現(xiàn)什么特殊的地方(除了常量池中能看到"java.lang.Object"),還是不了解編譯器具體是如何處理的.希望隨著學(xué)習(xí)的深入能解決這個(gè)問題.