为什么允许它通过接口引用访问类对象
的方法
您可以使用接口引用调用 Object
类方法,尽管接口不会从 Object class,因为Java中的每个根接口都有对应于 Object
class的每个方法的隐式方法声明。
You can invoke the Object
class methods using an interface reference although an interface doesn't extend from Object
class, because every root interface in Java has implicit declaration of method corresponding to each method of Object
class.
JLS§ 9.2 - 界面成员 :
界面的成员是:
- 如果接口没有直接的超接口,隐式声明一个公共抽象成员方法m,其签名为
s,返回类型为r,throws子句t对应于每个带有签名s的公共
实例方法m,返回类型为r ,并在对象中声明的throws子句t
,除非具有相同签名的方法,相同的
返回类型和兼容的throws子句由
接口显式声明。 / li>
- 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.