如何在接口类型的引用上调用对象 class 的方法?
How it is possible to call methods of Object class on a reference of Interface type?
interface TestInterface{
public void sayHello();
}
class A implements TestInterface{
public void sayHello(){
System.out.println("Hello");
}
public void sayBye(){
System.out.println("Hello");
}
public String toString(){
return "Hello";
}
public static void main(String args[]){
TestInterface ti=new A();
ti.sayHello();
ti.sayBye();//error
ti.toString();//How toString method can be called as it is not the part of the interface contract.
}
}
toString
可以调用是因为任何接口的任何实现都必须是Object的子class,其中包含toString
方法
为了调用任何其他未出现在接口或您的接口扩展的超接口中的方法(并且未在 Object
class 中定义),您必须强制转换包含该方法的 class 类型的接口。
来自 this section in the 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 an abstract
method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
所以Object
的public方法如toString
在所有接口中隐式声明。
这是面向对象语言的本质。接口只定义了具体 class 需要实现的一组方法签名。它们不限制 class(抽象 v 具体)的性质。
因此,当您声明 TestInterface ti
时,在您的示例中 A
实现了 TestInterface
,因此它是 TestInterface
的一个实例。同样 class B implements TestInterface {...}
也是有效的。
TestInterface ti = new A(); // Valid
ti = new B(); // Also valid (given the above definition)
每个 object 都是一个 Object
:) 在 objects 上调用 Object
方法是有意义的.
这就是问题的核心 - 所有引用类型都是 subtypes 的 Object
。引用类型包括
- class类型
- 接口类型
- 数组类型
- 类型变量 (
T
)
- 空类型(对于
null
)
- 路口类型 (
A&B
)
子类型继承超类型的方法。因此所有引用类型都继承 Object
方法。
interface TestInterface{
public void sayHello();
}
class A implements TestInterface{
public void sayHello(){
System.out.println("Hello");
}
public void sayBye(){
System.out.println("Hello");
}
public String toString(){
return "Hello";
}
public static void main(String args[]){
TestInterface ti=new A();
ti.sayHello();
ti.sayBye();//error
ti.toString();//How toString method can be called as it is not the part of the interface contract.
}
}
toString
可以调用是因为任何接口的任何实现都必须是Object的子class,其中包含toString
方法
为了调用任何其他未出现在接口或您的接口扩展的超接口中的方法(并且未在 Object
class 中定义),您必须强制转换包含该方法的 class 类型的接口。
来自 this section in the 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, andthrows
clause t corresponding to eachpublic
instance method m with signature s, return type r, andthrows
clause t declared inObject
, unless anabstract
method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
所以Object
的public方法如toString
在所有接口中隐式声明。
这是面向对象语言的本质。接口只定义了具体 class 需要实现的一组方法签名。它们不限制 class(抽象 v 具体)的性质。
因此,当您声明 TestInterface ti
时,在您的示例中 A
实现了 TestInterface
,因此它是 TestInterface
的一个实例。同样 class B implements TestInterface {...}
也是有效的。
TestInterface ti = new A(); // Valid
ti = new B(); // Also valid (given the above definition)
每个 object 都是一个 Object
:) 在 objects 上调用 Object
方法是有意义的.
这就是问题的核心 - 所有引用类型都是 subtypes 的 Object
。引用类型包括
- class类型
- 接口类型
- 数组类型
- 类型变量 (
T
) - 空类型(对于
null
) - 路口类型 (
A&B
)
子类型继承超类型的方法。因此所有引用类型都继承 Object
方法。