菜鸟问题,求高手解释

shockwave_115 2005-06-18 02:05:14
class test1{
int y=20;
public test1(){System.out.println("father_no_arg");
}
public test1(int i){this();System.out.println("father_int_arg"+y);}
}
public class test extends test1 {
int x=9;
public test(){System.out.println("no_arg");
}
public test(int i){this();System.out.println("int_arg"+x);}

public static void main(String[] args) {
test l=new test(12);
}
}
大家看看结果是什么

另我学c++的时候好像看到过如果子类继承父类的函数如果行为一样知识具体值不一样不会重载,构造函数会这样么?
...全文
70 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
congliu 2005-06-18
  • 打赏
  • 举报
回复
同意yuzl32(有酒醉)
yuzl32 2005-06-18
  • 打赏
  • 举报
回复
子类创建的时候,父类的构造函数自动被被调用,由于并没有给父类传值,所以父类
输出father_no_arg

又由于
public test(int i){this();System.out.println("int_arg"+x);}

中有 this()

所以第二次应该输出 :no_arg
最后输出:'int_arg" + x 即: 'int_arg9
shockwave_115 2005-06-18
  • 打赏
  • 举报
回复
顶一下
没人知道么?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧