[英]toString,hashcode and equals method inside interface
Below example runs without any errors, can any one explain me how this works?, as interface doesn't contain any toString()/hashcode/equals method declaration how compiler will resolve method call?,as per my understanding toString()/hashcode/equals or Object class methods will be declared by default inside interface? 根据我的理解toString()/ hashcode,下面的示例运行时没有任何错误,有人可以解释一下它的工作原理吗,因为接口不包含任何toString()/ hashcode / equals方法声明,编译器将如何解析方法调用? / equals或Object类方法将默认在接口内部声明? please correct me if am wrong
如果不对,请纠正我
interface int1 { public void show(); 接口int1 {public void show(); }
}
class inttest implements int1
{
public void show()
{
System.out.println("inttest.show()");
}
@Override
public String toString()
{
return "tostring called";
}
}
public class MainClass1
{
public static void main(String[] args) {
int1 i=new inttest();
System.out.println(i.toString());
}
}
Any interface has all the public methods of the Object
class (it either inherits them from a super-interface or declares them implicitly if it doesn't already declare them explicitly). 任何接口都具有
Object
类的所有公共方法(它从超级接口继承它们,或者,如果尚未显式声明它们,则隐式声明它们)。
This makes sense, since any implementing class of any interface must be a (direct or in-direct) sub-class of the Object
class, and therefore will inherit an implementation of all the Object
methods. 这是有道理的,因为任何接口的任何实现类都必须是
Object
类的(直接或间接)子类,因此将继承所有Object
方法的实现。
9.2.
9.2。 Interface Members
界面成员
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 an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
如果接口没有直接的超接口,则该接口隐式声明带有签名s的公共抽象成员方法m,返回类型r,并引发与每个带有签名s的公共实例方法m,返回类型r和throws子句t对应的子句t在Object中声明,除非接口显式声明了具有相同签名,相同返回类型和兼容throws子句的抽象方法。
随着所有对象的扩展, Object
和Object
具有toString()
您正在调用该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.