接口有 toString 方法吗? [英] Do interfaces have toString method?
本文介绍了接口有 toString 方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用接口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屋!
查看全文