Java 接口继承 练习

Java 接口继承 练习_第1张图片

package com.zhong.moremodel.day02;

import java.util.ArrayList;
import java.util.Comparator;


public class Test {
    public static void main(String[] args) {
        ClassManager studentInfo = new ClassManager();
        studentInfo.printInfo();
        studentInfo.printScore();
    }
}

class StudentOperatorImpl1 extends Student implements task {

    @Override
    public void printAllInfo(ArrayList<Student> s) {
        for (Student student : s) {
            System.out.println(student);
        }
    }

    @Override
    public void printAverageScore(ArrayList<Student> s) {
        computerAverage(s);

    }
}

class StudentOperatorImpl2 extends Student implements task {
    @Override
    public void printAllInfo(ArrayList<Student> s) {
        int sexMan = 0;
        int sexWan = 0;
        for (Student student : s) {
            if ("男".equals(student.getSex())) {
                sexMan += 1;
            } else {
                sexWan += 1;
            }
        }
        for (Student student : s) {
            System.out.println(student);
        }
        System.out.printf("男生人数 %d \n", sexMan);
        System.out.printf("女生人数 %d \n", sexWan);
    }

    @Override
    public void printAverageScore(ArrayList<Student> s) {
        Student studentMax = s.stream()
                .max(Comparator.comparing(Student::getScore))
                .get();
        Student studentMin = s.stream()
                .min(Comparator.comparing(Student::getScore))
                .get();
        s.removeIf(x -> x == studentMax || x == studentMin);
        computerAverage(s);
    }
}

interface task {
    void printAllInfo(ArrayList<Student> s);

    void printAverageScore(ArrayList<Student> s);

}

class ClassManager {
    private ArrayList<Student> s = new ArrayList<>();
    //    task studentInfo = new StudentOperatorImpl1();
    private task studentInfo = new StudentOperatorImpl2();

    public ClassManager() {
        s.add(new Student("张三", "女", 78.1));
        s.add(new Student("李四", "男", 18.1));
        s.add(new Student("王五", "女", 38.1));
        s.add(new Student("赵六", "女", 48.1));
        s.add(new Student("小钟", "男", 98.1));
        s.add(new Student("小王", "女", 299.1));
    }

    public final void printInfo() {
        studentInfo.printAllInfo(s);
    }

    public final void printScore() {
        studentInfo.printAverageScore(s);
    }
}

class Student {
    String name;
    String sex;
    Double score;

    public static void computerAverage(ArrayList<Student> s) {
        double sum = 0;
        for (Student student : s) {
            sum += student.getScore();
        }
        if (!s.isEmpty()) {
            System.out.printf("班级平均分为: %.2f\n", sum / s.size());
        }
    }

    public Student() {
    }

    public Student(String name, String 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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", score=" + score +
                '}';
    }
}


你可能感兴趣的:(Java,java,开发语言)