接口是否有toString方法?
问题内容:
如何toString
使用Test
没有toString
方法的interface的引用变量调用方法?
interface Test
{
void show();
String toHi();
}
class Demo implements Test
{
public void show(){
System.out.println("Show");
}
public String toString(){
return "Hello";
}
public String toHi(){
return "Hi";
}
public static void main(String[] args)
{
Test t=new Demo();
String s=t.toString();
System.out.println(s);
}
}
问题答案:
在该 Java文档说…
When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class
。
这就是为什么您可以toString()
在接口引用上调用方法的原因