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属性呢?
(2)JavaBean 类必须有一个空的构造函数:类中必须有一个不带参数的公用构造器,此构造器也应该通过调用各个特性的设置方法来设置特性的缺省值。
(3)一个javaBean类不应有公共实例变量,类变量都为private
(4)持有值应该通过一组存取方法(getXxx 和 setXxx)来访问:对于每个特性,应该有一个带匹配公用 getter 和 setter 方法的专用实例变量。
f.fage ——> f.getFage()不就行了么。