Studnet类
package entity;
public class Student {
public String name; //姓名
public Integer age; //年龄
//无参构造函数
public Student() {}
//含参构造函数
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
//打印学生信息
public void Show() {
System.out.println("姓名:"+this.getName()+" 年龄:"+this.getAge());
}
}
Undergrated类
package entity;
public class Undergraduate extends Student {
public String degree; //学位
public Undergraduate(String name,Integer age,String degree) {
super(name,age);
this.degree = degree;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public void Show() {
System.out.println( "姓名:"+this.getName()+" 年龄:"+this.getAge()+" 学位:"+this.getDegree());
}
}
测试类
import entity.Student;
import entity.Undergraduate;
public class main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Student student1=new Student("张三",25);
student1.Show();
Undergraduate undergraduate1=new Undergraduate("李四", 20, "博士");
undergraduate1.Show();
}
}