今天突然发现这样一个问题 就是接口中好像也有默认的toString() 方法 象下面的这个例子:
interface test
{}
public class TesttoString implements test
{
private String s;
public TesttoString(String str){
s = str;
}
public String toString(){
return s;
}
public String returnS(){
return s;
}
public static void main(String[] args){
test o = new TesttoString("I don't know!!");
System.out.println(o);
System.out.println(o.toString());
System.out.println(((TesttoString)o).returnS());
//System.out.println(o.returnS()); }
}
我没有在接口中定义 toString() 方法 但是实现他的类 TesttoString 却可以向上转型付给 test 如果是 子类继承自父类的话 这个就好理解,类都是 Object 的子孙, 但是我好像没有在那本书上看见说接口 有这个功能的呀 ! 象o.returnS()这个调用会报错的,这个也好理解 但就是toString() 呀 !!!!

解决方案 »

  1.   

    toString()是默认加载包java.lang包中类的方法
      

  2.   

    interface test has access to method toString(), this method is from class Object.In class TesttoString, toString() method is overridden. When the program is running, the overridding toString() method in class TesttoString is called based on the object type instead of variable reference type.
      

  3.   

    interface test has access to method toString(), this method is from class Object.This toString() method is overridden in class TesttoString. When the program is running, the overridding toString() method in class TesttoString is called based on the object type instead of variable reference type.
      

  4.   

    难道说接口也默认有 toString 吗
      

  5.   

    toString方法是定义在Object类中的!当然所有的类都会有这个方法!
      

  6.   

    Java中所有的类都默认继承于Object类,哪怕你在写class的时候并没有显示的继承Object类,Java编译器也会隐式的加上
      

  7.   

    这个现像的确很有趣,上面几位好像都没注意到一个问题,那就是这里的研究对像是“接口”,而不是“类”,而接口似乎在理论上是不可以继承Object类的我对这个现象的理解是,JAVA中对于Object这个基类的处理是另外一套方式
    并不是“所有类都继承了Object类,所以所有对象都拥有它的方法”
    而是  “所有的对象都默认拥用Object类的方法,刚好看起来符合继承这条规则”呵呵,大家一起研究研究
      

  8.   

    Sun的官方文档TJLS(The Java Language Specification)吧!其中第9章9.2节关于接口有这么一段话: 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 a 
    method with the same signature, same return type, and a compatible throws 
    clause is explicitly declared by the interface. It is a compile-time error if the 
    interface explicitly declares such a method m in the case where m is declared to 
    be final in Object. 
    接口隐含定义了一套与Object类中的方法签名完全相同的方法;如果接口定义一个在Object中定义为final的会编译出错。
      

  9.   

    接口好像真的隐含了 Object 中的一套方法 我做了下面一个 hashCode() 的测试
    interface test
    {}
    public class TesttoString implements test
    {
    private String s;
    private int i;
    public TesttoString(String str,int j){
    s = str;
    i = j;
    }
    public String toString(){
    return i+" :  "+ s;
    }
    public String returnS(){
    return s;
    }
    public int hashCode(){
    return  i;
    }
    public static void main(String[] args){
    test o = new TesttoString("I don't know!!",1);
    System.out.println(o);
    System.out.println(o.toString());
    System.out.println(((TesttoString)o).returnS());
    System.out.println(o.hashCode());
    //System.out.println(o.returnS()); }
    }结果可以输出 哈希玛 1 的
      

  10.   

    >>这个现像的确很有趣,上面几位好像都没注意到一个问题,那就是这里的研究对像是“接口”,
    >>而不是“类”,而接口似乎在理论上是不可以继承Object类的EmptyInterface e = new SomeImplemention();
    String s = e.toString();//e既然是接口的实例,证明它是对象。能继承接口的,必然是Object。编译器和你我都知道这一点。对不起。
      

  11.   

    //e既然是接口的实例
    >>接口不能有实例的,接口只是提供了一系列的方法接口,且都是空的,以供多肽机制的
    接口当然可以有实例(instance,名词),抽象类也可以有实例,只不过是不能直接实例化而已。实现了一个接口的对象就是这个接口的一个实例。
      

  12.   

    这个只是对Object类中toString()的覆盖,,并不是test中的内容,,