设计一个学生类Student,类有成员变量姓名、年龄,有“学习”方法。Student类派生出本科生类,本科生类派生出研究生类,本科生类增加专业和学位属性,覆盖学习方法。研究生类增加研究方向属性,覆盖学习方法。每个类都有显示方法,用于输出属性信息。编写测试类测试这三个类。
/**
* @author : LLL
* @Time : 2023.7.10
* 学生类
*/
public class Student {
//姓名
private String name;
//年龄
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void study() {
System.out.println("在学习");
}
public void display() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
}
}
/**
* @author : LLL
* @Time : 2023.7.10
* 本科生类
*/
public class UndergradStudent extends Student {
private String major;
private String degree;
public UndergradStudent(String name, int age, String major, String degree) {
super(name, age);
this.major = major;
this.degree = degree;
}
@Override
public void study() {
System.out.println("在学习" + major + "专业");
}
public void display() {
super.display();
System.out.println("专业:" + major);
System.out.println("学位:" + degree);
}
}
/**
* @author : LLL
* @Time : 2023.7.10
* 研究生类
*/
public class GradStudent extends UndergradStudent {
private String researchDirection;
public GradStudent(String name, int age, String major, String degree, String researchDirection) {
super(name, age, major, degree);
this.researchDirection = researchDirection;
}
@Override
public void study() {
super.study();
System.out.println("在研究" + researchDirection + "方向");
}
public void display() {
super.display();
System.out.println("研究方向:" + researchDirection);
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student("张三", 20);
s.display();
s.study();
UndergradStudent us = new UndergradStudent("李四", 21, "计算机科学", "学士");
us.display();
us.study();
GradStudent g = new GradStudent("王五", 24, "软件工程", "硕士", "人工智能");
g.display();
g.study();
}
}