面向对象私有属性的继承
package am;
/**
-
私有方法子类如何继承
*/
public class People {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 People() {
}
//构造有参方法
public People(String name, int age) {
this.name = name;
this.age = age;
}
}
package am;
/**
-
子类 学生类
*/
public class Student extends People{private String stuNo;//子类添加的属性,
//子类构造方法 无参
public Student(){
super();
System.out.println(“学生类的无参构造方法被调用”);
}//知识点 this() super() 各自的作用
public Student(String name){
//this(); //this()调用当前类中的构造方法, 两者不能同时存在
//super(); //super() 调用父类的构造方法
}public Student(String name, int age, String stuNo){
//使用super关键字
super(name, age); //首先调用父类中的构造方法
this.stuNo = stuNo;
}//知识点 this super 各自的用法
public void show(){
System.out.println(this.stuNo); //当前对象
System.out.println(super.getName());
}
}
package am;
/**
*测试类
*/
public class Test {
public static void main(String[] args) {
People people = new People();
Student student = new Student("张三",18,"1001");
System.out.println(student.getName());
student.show();
}
}