1.可以将父类中的私有属性写上调用接口 将改父类转化为javabean 子类继承这个改装后的父类 通过调用get set接口来调用私有化的父类成员
2.可以通过在子类中生成构造函数 在构造函数内部使用super调用父类中的private成员,然后再测试类中new对象,通过get set方法赋值取值
父类
public class Employee {
public Employee(){
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
}
子类Manager
public class Manager extends Employee{
public Manager() {
}
public Manager(String name, int age) {
super(name, age);
}
}
子类Teacher
public class Teacher extends Employee{
}
Test类
**public class Test {
public static void main(String[] args) {
Teacher teacher=new Teacher();
teacher.setName("李琦");
teacher.setAge(18);
System.out.println(teacher.getAge()+" "+teacher.getName());
Manager manager=new Manager("淡萌",18);
System.out.println(manager.getName()+" "+manager.getAge());
}
}