Java中的隐式超级接口? [英] implicit super-interface in Java?

查看:131
本文介绍了Java中的隐式超级接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们都知道所有类都隐式地扩展了Object。接口怎么样?是否存在隐式超级接口?我说有。以下代码编译:

So we all know that all classes implicitly extend Object. How about interfaces? Is there an implicit super-interface? I say there is. The following code compiles:

java.io.Serializable s1 = null;
java.io.Serializable s2 = null;

s1.equals(s2);

在Serializable中未声明等于方法,但在对象中。由于接口只能扩展其他接口,而Object是一个类,而不是接口,因此必须有一些正在扩展的隐式接口。然后 Object 类必须隐式实现这个隐式接口(哇,写起来很奇怪)。

The equals method is not declared in Serializable, but in 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. And the Object class must then implicitly implement this implicit interface (wow, that was weird to write).

所以问题是,这有多正确?

So, the question is, how correct is this?

推荐答案


由于接口只能扩展其他
接口,而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语言规范


如果接口没有直接的
超接口,那么接口
隐式声明一个公共抽象
成员方法
m,带签名s,
返回类型r,throws子句t
对应每个公共实例
方法
m,带签名s ,返回类型
r,以及在
Object 中声明的throws子句t ,除非具有相同
签名,相同返回类型和
兼容的方法throws子句是​​接口声明的
。如果接口显式

中声明了这样的方法m,其中m被声明为
Object中的final。

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方法,并且您无法在界面中拥有这些修饰符。

The difference between this and your "implicit super interface" is that Object has a number of final and protected methods, and you couldn't have those modifiers in an interface.

这篇关于Java中的隐式超级接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆