java每日一题——班级学生信息管理模块的开发(面向接口编程的练习)

前言:

基于面向对象编程(oop)的基础知识巩固,学会接口使用。打好基础,daydayup!

接口的概念可以看这篇:

 java的面向对象编程(oop)——认识接口

需求如下: 

java每日一题——班级学生信息管理模块的开发(面向接口编程的练习)_第1张图片

思路:

运用面向对象编程:1, 创建学生数据;2,输出全班信息和平均成绩;

注意:由于要使用多套方案,所以使用接口来进行编程。

1,创建实体类: 

创建一个实体类,用来记录姓名、性别,成绩等学生信息。

public class Student {
    private String name;
    private char sex;
    private double scord;

    public Student(String name, char sex, double scord) {
        this.name = name;
        this.sex = sex;
        this.scord = scord;
    }

    public Student() {
    }

    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 getScord() {
        return scord;
    }

    public void setScord(double scord) {
        this.scord = scord;
    }
}

2,创建班级类:

创建一个集合用来存储学生信息,在people方法里添加学生信息并在构造器中返回people;随后创建两个方法一个用来列出学生信息,一个用来记录平均分。

public class ClassManager {
    private  ArrayList Students = new ArrayList<>();
 
    public ClassManager(){
     people();
    }
    public void people(){
        Students.add(new Student("lala",'女',100));
        Students.add(new Student("lals",'女',90));
        Students.add(new Student("lald",'男',80));
        Students.add(new Student("lalf",'男',70));
    }

    public void printManager(){
       
    }
    public void printAverage(){
      
    }
}

3,创建接口 :

由于需求是要求两种方案,所以使用接口来进行编程

public interface StudentOperator {
     void printManager(ArrayList Students);

     void printAverage(ArrayList Students);
}

4,编写方案一 :

 实现接口,重写两个方法即可

public class StudentOperatorlmpl1 implements StudentOperator{

    @Override
    public void printManager(ArrayList Students) {
        System.out.println("-----------------");
        for (int i = 0; i < Students.size(); i++) {
           Student s1 = Students.get(i);
            System.out.println("第"+(i+1)+"位的姓名为:"+s1.getName()+",性别为"+s1.getSex()+",成绩为"+s1.getScord());

        }
        System.out.println("--------------------------");
    }

    @Override
    public void printAverage(ArrayList Students) {
      double sc = 0.0;
        for (int i = 0; i < Students.size(); i++) {
            Student s1 = Students.get(i);
            sc += s1.getScord();
        }
        System.out.println("平均分为:"+ sc/Students.size());
    }
}

5,编写方案二: 

 实现接口,在方案1中稍作修改即可

public class StudentOperatorlmpl2 implements StudentOperator{
    @Override
    public void printManager(ArrayList Students) {
        System.out.println("-----------------");
        double nan = 0.0;
        double nv  = 0.0;
        for (int i = 0; i < Students.size(); i++) {
            Student s1 = Students.get(i);
            System.out.println("第"+(i+1)+"位的姓名为:"+s1.getName()+",性别为"+s1.getSex()+",成绩为"+s1.getScord());
            if (Students.get(i).getSex() == '男'){nan ++;}else if (Students.get(i).getSex() == '女'){nv++;}
        }
        System.out.println("男性有"+nan +"人" );
        System.out.println("女性有"+nv +"人" );
        System.out.println("--------------------------");
    }

    @Override
    public void printAverage(ArrayList Students) {
        double sc = 0.0;
        double max = Students.get(0).getScord();
        double min = Students.get(0).getScord();
        for (int i = 0; i < Students.size(); i++) {
            Student s1 = Students.get(i);
            sc += s1.getScord();
            if (s1.getScord()>max){max = s1.getScord();}
            if (s1.getScord()

6,修改班级类:

在类中加入接口对象,并在方法里实现指向。(修改方案只需要把第三行的StudentOperatorlmpl2修改成StudentOperatorlmpl1,便可实现方案转换)

public class ClassManager {
    private  ArrayList Students = new ArrayList<>();
    private StudentOperator classoperator = new StudentOperatorlmpl2();
    public ClassManager(){
     people();
    }
    public void people(){
        Students.add(new Student("lala",'女',100));
        Students.add(new Student("lals",'女',90));
        Students.add(new Student("lald",'男',80));
        Students.add(new Student("lalf",'男',70));
    }

    public void printManager(){
        classoperator.printManager(Students);
    }
    public void printAverage(){
        classoperator.printAverage(Students);
    }
}

7,测试: 

新建一个类,创建班级对象,可以实现信息遍历

java每日一题——班级学生信息管理模块的开发(面向接口编程的练习)_第2张图片

结语: 

代码比较粗糙,目的是为了练习接口功能的使用,接口确实是一种实现多功能切换的便利的方法,需要熟练掌握。

完结,撒花!!

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