在子类中,父类对象为什么不能访问父类的private属性
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属性呢?
...全文


Java SE
