父类:
package extend;public class Father { public Father() {
}

public String toInt(){
return "toInt()";
} public String toString(){
return "toString()";
}
}子类:package extend;public class Children extends Father { public Children() {
super();
} public static void main(String arg[]) {
Children c = new Children();
System.out.println(c);
}
}
请问System.out.println(c);输出是什么?

解决方案 »

  1.   

    //Object
    public String toString()
    {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }//Father
    public String toString()
    {
        return "toString()";
    }//Children
    你说呢?
      

  2.   

    子类Children继承了父类Father的所有public的方法,System.out.println(c)执行的时候会调用父类的toString()方法
      

  3.   

    Object是Father的父类,Father是Children的父类,所以先调Father的
      

  4.   

    这真的是sun公司的面试题吗?感觉不是很难耶。有点像在考基础的初始化知识。
      

  5.   

    System.out.println(c);遇到不认识的类型就是去调该类型的toString方法,如果没有就打印类名和对象地址,子类的toString从父类继承过来的
      

  6.   

    不会吧~
    是不是SUN的实习的面试题啊?
    这是OOP的基础的基础啊~而且连一点弯也绕的呀
      

  7.   

    是啊!不就是考tostring 么!
    面试题??!~
    不信!!~!
      

  8.   

    因为
    SomeClass c=new SomeClass();
    String a=c;//执行此句时就会自动调用c.toString();
    System.out.println(...)执行时会自动调用c.toString;
    这是一个语法问题
      

  9.   

    http://www.xwiki.com.cn/archives/56楼主看一下这个。
      

  10.   

    System.out.println(c);由于C是一个对象,上面语句默认调用的是该对象胡toString()方法~!
      

  11.   

    是不是用println输出一个对象时就会调用该对象的toString()方法?
      

  12.   

    任何对象都有toString()方法,在需要将对象自动转换成字符串的地方,对象的toString()方法被调用,用来生成相应的字符串