![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/611572428c82f7dbf86cf8bb972afb0e.png)
public abstract class Student {
private int id;
private String name;
private char sex;
private int age;
private double scores;
public Student(int id, String name, char sex, int age) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScores() {
return scores;
}
public void setScores(double scores) {
this.scores = scores;
}
public abstract void calculate();
}
public class English extends Student {
private int speech;
private int finalExam;
private int period;
public English(int id, String name, char sex, int age,int speech, int finalExam, int period) {
super(id,name,sex,age);
this.speech = speech;
this.finalExam = finalExam;
this.period = period;
}
@Override
public void calculate() {
double sum = speech*0.5+finalExam*0.25+period*0.25;
setScores(sum);
}
}
public class Computer extends Student {
private int operation;
private int english;
private int finalExam;
private int period;
public Computer(int id, String name, char sex, int age,int operation, int english, int finalExam, int period) {
super(id,name,sex,age);
this.operation = operation;
this.english = english;
this.finalExam = finalExam;
this.period = period;
}
@Override
public void calculate() {
double sum = operation*0.4+english*0.2+finalExam*0.2+period*0.2;
setScores(sum);
}
}
public class Literature extends Student {
private int speech;
private int works;
private int finalExam;
private int period;
public Literature(int id, String name, char sex, int age,int speech, int works, int finalExam, int period) {
super(id,name,sex,age);
this.speech = speech;
this.works = works;
this.finalExam = finalExam;
this.period = period;
}
@Override
public void calculate() {
double sum = speech*0.35+works*0.35+finalExam*0.15+period*0.15;
setScores(sum);
}
}
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random r = new Random();
Student[] stus = new Student[5];
stus[0] = new English(1,"s1",'f',20,r.nextInt(101), r.nextInt(101),
r.nextInt(101));
stus[1] = new Computer(2,"s2",'f',19,r.nextInt(101), r.nextInt(101),
r.nextInt(101), r.nextInt(101));
stus[2] = new Literature(3,"s3",'m',21,r.nextInt(101), r.nextInt(101),
r.nextInt(101), r.nextInt(101));
stus[3] = new Computer(4,"s4",'m',19,r.nextInt(101), r.nextInt(101),
r.nextInt(101), r.nextInt(101));
stus[4] = new Literature(5,"s5",'f',20,r.nextInt(101), r.nextInt(101),
r.nextInt(101), r.nextInt(101));
for(Student i : stus){
i.calculate();
System.out.println("学号:"+i.getId()+"\t姓名:"+i.getName()
+"\t性别:"+i.getSex()+"\t年龄:"+i.getAge()+"\t综合成绩:"+i.getScores());
}
}
}