所以我们都知道所有的类都隐式扩展了Object。接口(interface)怎么样?是否有隐式 super 接口(interface)?我说有。编译以下代码:
java.io.Serializable s1 = null;
java.io.Serializable s2 = null;
s1.equals(s2);
equals
方法不是在 Serializable 中声明的,而是在 Object 中声明的。由于接口(interface)只能扩展其他接口(interface),而 Object 是一个类,而不是接口(interface),因此必须有一些隐式接口(interface)正在被扩展。然后 Object
类必须隐式实现这个隐式接口(interface)(哇,写起来很奇怪)。
那么,问题是,这有多正确?
最佳答案
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.
这与您的“隐式 super 接口(interface)”之间的区别在于 Object
有许多最终和 protected 方法,您不能在接口(interface)中使用这些修饰符。
关于java - Java中的隐式 super 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/803466/