接口有toString方法吗? [英] Do interfaces have toString method?
本文介绍了接口有toString方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用interface Test
的引用变量调用 toString
方法,该方法没有 toString
方法?
How is it possible to call toString
method using the reference variable of interface Test
, which does not have a toString
method?
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文档说...
当一个接口没有直接的SuperInterface时,它将创建一个抽象的公共方法Object类中出现的所有公共方法
。
这就是为什么你能够调用 toString()
接口参考上的方法
This is why you are able to call the toString()
method on the interface reference
这篇关于接口有toString方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文