java实现,成绩是个抽象类,然后有两个子类考试成绩和考核成绩
时间: 2024-02-20 07:59:38 浏览: 132
好的,我可以为您提供一个简单的成绩信息管理系统Java实现的代码示例,具体实现如下:
首先,定义一个抽象类Score(成绩类):
```
public abstract class Score {
protected String subject; // 科目
protected double score; // 成绩
public Score(String subject, double score) {
this.subject = subject;
this.score = score;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public abstract String getType(); // 获取成绩类型
}
```
然后,定义两个子类ExamScore(考试成绩)和CheckScore(考核成绩):
```
public class ExamScore extends Score {
public ExamScore(String subject, double score) {
super(subject, score);
}
@Override
public String getType() {
return "考试成绩";
}
}
public class CheckScore extends Score {
public CheckScore(String subject, double score) {
super(subject, score);
}
@Override
public String getType() {
return "考核成绩";
}
}
```
接着,实现一个成绩信息管理类ScoreManager:
```
import java.util.ArrayList;
public class ScoreManager {
private ArrayList<Score> scores; // 成绩列表
public ScoreManager() {
scores = new ArrayList<>();
}
// 添加成绩
public void addScore(Score score) {
scores.add(score);
}
// 获取所有成绩
public ArrayList<Score> getAllScores() {
return scores;
}
// 获取某个科目的成绩
public ArrayList<Score> getScoresBySubject(String subject) {
ArrayList<Score> result = new ArrayList<>();
for (Score score : scores) {
if (score.getSubject().equals(subject)) {
result.add(score);
}
}
return result;
}
// 获取某个类型的成绩
public ArrayList<Score> getScoresByType(String type) {
ArrayList<Score> result = new ArrayList<>();
for (Score score : scores) {
if (score.getType().equals(type)) {
result.add(score);
}
}
return result;
}
}
```
最后,可以测试一下:
```
public class Main {
public static void main(String[] args) {
ScoreManager manager = new ScoreManager();
ExamScore examScore1 = new ExamScore("数学", 90);
ExamScore examScore2 = new ExamScore("英语", 80);
CheckScore checkScore1 = new CheckScore("语文", 95);
CheckScore checkScore2 = new CheckScore("体育", 85);
manager.addScore(examScore1);
manager.addScore(examScore2);
manager.addScore(checkScore1);
manager.addScore(checkScore2);
ArrayList<Score> allScores = manager.getAllScores();
System.out.println("所有成绩:");
for (Score score : allScores) {
System.out.println(score.getSubject() + " " + score.getType() + " " + score.getScore());
}
ArrayList<Score> mathScores = manager.getScoresBySubject("数学");
System.out.println("数学成绩:");
for (Score score : mathScores) {
System.out.println(score.getType() + " " + score.getScore());
}
ArrayList<Score> checkScores = manager.getScoresByType("考核成绩");
System.out.println("考核成绩:");
for (Score score : checkScores) {
System.out.println(score.getSubject() + " " + score.getScore());
}
}
}
```
输出结果:
```
所有成绩:
数学 考试成绩 90.0
英语 考试成绩 80.0
语文 考核成绩 95.0
体育 考核成绩 85.0
数学成绩:
考试成绩 90.0
考核成绩:
语文 95.0
体育 85.0
```
阅读全文