在父类中访问子类的成员变量

ihrthk ihrthk 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

public class Father {

private String father;

public Father() {
Field fs[] = this.getClass().getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
System.out.println(f.getType().getName() + " " + f.getName());
}
}

}


public class Son extends Father {

private int age;
private String name;

public Son() {
super();
// TODO Auto-generated constructor stub
}

}


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Son son = new Son();
}

}

这个是打印结果:
int age
java.lang.String name

请重点分析Father的构造方法this变量,解释一下打印出来的为什么是上面的结果?

我觉得的结果是:
java.lang.String name
...全文
评论 11
收藏 已收藏
分享
返回顶部
ihrthk
ihrthk 领域专家: 操作系统技术领域 领域专家: 操作系统技术领域
Java SE 62,620 成员 307,251 内容