在子类中,父类对象为什么不能访问父类的private属性

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

class Father {
private int fage;
public int getFage() {
return fage;
}

public void setFage(int fage) {
this.fage = fage;
}

int fweight;
protected String fgender;
public int fheight;

public Father(int age, int weight, String gender, int height) {
this.fage = age;
this.fweight = weight;
this.fgender = gender;
this.fheight = height;
}

public Father() {}


}

public class FatherAndSon extends Father{

private int sage;
int sweight;
protected String sgender;
public int sheight;

public FatherAndSon(int age, int weight, String gender, int height) {
super(55, 130, "male", 160);
this.sage = age;
this.sweight = weight;
this.sgender = gender;
this.sheight = height;
}

public static void main(String[] args) {
Father f = new Father(50, 130, "male", 160);
FatherAndSon s = new FatherAndSon(25, 130, "male", 170);
//System.out.println(f.fage); 为什么父类对象f在这里不能访问自己的private属性
}

}

既然子类不能继承父类的private属性,那么也就无法直接访问父类的private属性。但是为什么在子类中,父类对象也不能访问自己的private属性呢?
...全文

相关推荐

评论 14
收藏 已收藏
分享
返回顶部
修电视的攻城狮
Java SE 62,621 成员 307,251 内容