Student类
public class Student {
private String name;
private char sex;
private double score;
public Student() {
}
public Student(String name, char sex, double score) {
this.name = name;
this.sex = sex;
this.score = score;
}
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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
ClassManage类
import java.util.ArrayList;
public class ClassManage {
private ArrayList<Student> students = new ArrayList<>();
private StudentOperator studentOperator = new StudentOperatorImpl2();
public ClassManage(){
students.add(new Student("张三",'男',99));
students.add(new Student("李四",'男',100));
students.add(new Student("王五",'男',60));
students.add(new Student("赵六",'女',80));
}
//打印全班全部学生的信息
public void printInfo(){
studentOperator.printAllInfo(students);
}
//打印全班全部的学生的平均分
public void printScore(){
studentOperator.printAverage(students);
}
}
StudentOperator接口
import java.util.ArrayList;
public interface StudentOperator {
void printAllInfo(ArrayList<Student> students);
void printAverage(ArrayList<Student> students);
}
StudentOperatorImpl1类
import java.util.ArrayList;
public class StudentOperatorImpl1 implements StudentOperator{
@Override
public void printAllInfo(ArrayList<Student> students) {
System.out.println("==========全班全部学生信息如下==========");
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println("姓名:" + s.getName() + ",性别:" + s.getSex() + ",成绩:" + s.getScore());
}
System.out.println("=============================");
}
@Override
public void printAverage(ArrayList<Student> students) {
double sum = 0.0;
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
sum += s.getScore();
}
System.out.println("全班平均分为:" + sum/students.size());
}
}
StudentOperatorImpl2类
import java.util.ArrayList;
public class StudentOperatorImpl2 implements StudentOperator{
@Override
public void printAllInfo(ArrayList<Student> students) {
System.out.println("==========全班全部学生信息如下==========");
int count1 = 0;
int count2 = 0;
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println("姓名:" + s.getName() + ",性别:" + s.getSex() + ",成绩:" + s.getScore());
if (s.getSex() == '男'){
count1++;
}else {
count2++;
}
}
System.out.println("男生人数为:" + count1 + ",女生人数为:" + count2);
System.out.println("班级总人数为:" + students.size());
System.out.println("=============================");
}
@Override
public void printAverage(ArrayList<Student> students) {
double sum = 0.0;
double max = students.get(0).getScore();
double min = students.get(0).getScore();
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
sum += s.getScore();
if (s.getScore() > max){
max = s.getScore();
}else if (s.getScore() < min){
min = s.getScore();
}
}
System.out.println("学生的最高分是:" + max);
System.out.println("学生的最低分是:" + min);
System.out.println("全班平均分为:" + (sum-max-min)/(students.size()-2));
}
}
Test类
public class Test {
public static void main(String[] args) {
// 完成班级学生信息管理案例
ClassManage classManage = new ClassManage();
classManage.printInfo();
classManage.printScore();
}
}
一个接口可以同时继承多个接口