public class Practice {
public static void main(String[] args) {
Father child=new Child();
child.s();
}
}
class Father{
int age=100;
public Father() {
System.out.println(this);//构造器返回同类指针,emm子类也是父类,好像没什么毛病
this.s();/*这时的this已经和Child类构造器里的this指针相同,知道要在方法区的Chil
d类里复制代码,所以打印“我是儿子” */
/*System.out.println(abc);报错,Father构造器启动时只初始化了Father
类有的属性和方法*/
System.out.println(age);
}
void s() {
System.out.println("我是爸爸");
}
}
class Child extends Father{
int abc=200;
public Child() {
System.out.println(this);
System.out.println(abc);
}
void s() {
System.out.println("我是儿子");
}
}
输出结果:
test多线程.Child@15db9742
我是儿子
100
test多线程.Child@15db9742
200
我是儿子
以前的错误思路已经删除了,然后现在的我会说:
懂得都懂,然后就是在构造方法块执行的时候,对象的属性到底是默认的0、false、null或者是你显式赋的值。类似的问题只要自己敲一下就知道了,学会自己想问的问题提炼出关键词然后上网去搜是我们以后解决任何问题的非常重要的技能。知道怎么清楚的向你的同行表述你的问题,那么一切都不是问题。关键词关键词关键词说3遍