请设计 3 个类 , 分别是学生类 Student, 本科生类 Undergaduate, 研究生 类 Postgraduate, 其中 Student 类是一个抽象类 , 它包含一些基本的学生信息
时间: 2024-10-29 10:13:58 浏览: 54
在Java中,我们可以设计如下的三个类来满足这个需求:
1. **Student** (抽象类) - 作为所有学生的基类,包含通用属性和方法:
```java
public abstract class Student {
private String name;
private int id;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
// 公共方法
public abstract void study();
public String getName() {
return name;
}
public int getId() {
return id;
}
}
```
2. **Undergraduate** - 继承自Student,代表本科生:
```java
public class Undergraduate extends Student {
private double gpa;
public Undergraduate(String name, int id, double gpa) {
super(name, id);
this.gpa = gpa;
}
@Override
public void study() {
System.out.println(getName() + " is studying as an undergraduate with GPA: " + gpa);
}
// 可能需要公共方法来获取GPA
public double getGPA() {
return gpa;
}
}
```
3. **Postgraduate** - 也继承自Student,代表研究生:
```java
public class Postgraduate extends Student {
private String researchArea;
public Postgraduate(String name, int id, String researchArea) {
super(name, id);
this.researchArea = researchArea;
}
@Override
public void study() {
System.out.println(getName() + " is pursuing a postgraduate degree in " + researchArea);
}
// 获取研究领域
public String getResearchArea() {
return researchArea;
}
}
阅读全文