问题描述
- public Tom(String s) {
this();
System.out.println("I am " + s);
的this为什么不是作用,谢谢 -
class Jerry {
public Jerry() {
System.out.println("Jerry");
}
public void hello() {
System.out.println("Hello in Jerry");
}
}
class Tom extends Jerry {
public Tom() {
System.out.println("Tom");
}
public Tom(String s) {
this();
System.out.println("I am " + s);
}
public void hello() {
System.out.println("Hello in Tom");
}
}
public class Test3 extends Tom {
public Test3() {
this("test");
System.out.println("Test");
}
public Test3(String s) {
super(s);
System.out.println("Hello " + s);
}
public static void main(String args[]) {
Test3 t = new Test3();
t.hello();
}
}
答案
Jerry
Tom
I am test
Hello test
Test
Hello in Tom
解决方案
this()就是调用自身的无参构造函数。打印的tom就是这个无参构造函数的内容的。
解决方案二:
转载:http://blog.sina.com.cn/s/blog_49faf32901008pap.html?作者:Joe Armstrong原文:What’s all this fuss about Erlang译者:朱照远(Joshua Zhu) 许式伟(XuShiWei)What’s all this fuss about Erlang?没人可以预言未来,但我却打算做一些有......
答案就在这里:What’s all this fuss about Erlang
解决方案三:
已经调用过了这个this()了
时间: 2024-11-01 02:18:54