public class People { public String name; // 姓名 public int age; // 年龄 public String sex; // 性别 public String sn; // 身份证号 public People(String name, int age, String sex, String sn) { this.name = name; this.age = age; this.sex = sex; this.sn = sn; } public String toString() { return "姓名:" + name + "\n年龄:" + age + "\n性别:" + sex + "\n身份证号:" + sn; } }
子类student
1 2 3 4 5 6 7 8 9 10 11 12
public class Student extends People { private String stuNo; // 学号 private String department; // 所学专业 public Student(String name, int age, String sex, String sn, String stuno, String department) { super(name, age, sex, sn); // 调用父类中的构造方法 this.stuNo = stuno; this.department = department; } public String toString() { return "姓名:" + name + "\n年龄:" + age + "\n性别:" + sex + "\n身份证号:" + sn + "\n学号:" + stuNo + "\n所学专业:" + department; } }
public class Test { public static void main(String[] args) { Figure figure; // 声明Figure类的变量 figure = new Rectangle(9, 9); //父类引用指向子类 System.out.println(figure.area()); } }