这个问题在这里已经有了答案:
How methods of Object class be visible through interface?
(1 个回答)
2个月前关闭。
根据 The Java Language Specification, Java SE 16 Edition (JLS) §9.2 Interface Members :
If an interface has no direct superinterface types, then the interface implicitly declares a
public abstract
member methodm
with signatures
, return typer
, andthrows
clauset
corresponding to eachpublic
instance methodm
with signatures
, return typer
, andthrows
clauset
declared inObject
(§4.3.2), unless anabstract
method with the same signature, same return type, and a compatiblethrows
clause is explicitly declared by the interface.
为什么任何顶级接口(interface)“隐式”声明 的公共(public)方法对象 类(class)?这个设计的目的是什么?
最佳答案
What is the purpose of this design?
因为你希望能够拨打所有
Object
任何类型的每个对象上的方法( toString
、 equals
、 hashCode
等)。interface Foo {
}
Foo foo = ...;
foo.equals(otherFoo);
我是否真的声明了 equals
并不重要接口(interface)中的方法。
关于java - 接口(interface)隐式声明 Object 类的公共(public)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68874966/