【发布时间】:2013-08-16 14:22:59
【问题描述】:
In which of the following case i get output "Hello World" ?
class A{
@Override
public String toString() {
return "Hello World";
}
}
public class B extends A{
public void print(){
//replace code that displays "Hello World"
}
public static void main(String args[]){
new B().print();
}
}
我。 System.out.println(new A());
二。 System.out.println(new B());
III.System.out.println(this);
1. only I
2. I and III
3. I and II
4. all I,II and III
它的answer is 4 即all I,II and III
i understood about I but why II and III is also right ?
EDIT : Also specify which section of jls provides this specification ?
【问题讨论】:
-
toString()被所有子类覆盖。 -
但这里为什么在创建子类实例的情况下打印输出到超类的字符串
-
在下面查看我的答案。
标签: java tostring overriding