所以我们都知道所有类都隐式地扩展了Object.接口怎么样?是否存在隐式超级接口?我说有.以下代码编译:
java.io.Serializable s1 = null;
java.io.Serializable s2 = null;
s1.equals(s2);
equals方法未在Serializable中声明,而是在Object中声明.由于接口只能扩展其他接口,而Object是一个类,而不是接口,因此必须有一些正在扩展的隐式接口.然后,Object类必须隐式实现这个隐式接口(哇,写起来很奇怪).
所以,问题是,这有多正确?
解决方法
Since interfaces can only extend other interfaces,and Object is a class,not an interface,there must be some implicit interface that is being extended.
引用Java Language Specification:
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.
这与你的“隐式超级接口”之间的区别在于Object有许多final和protected方法,你不能在接口中拥有这些修饰符.
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|