文章目录
- 接口interface
- 好处
- 为什么要用接口
- 接口案例
- 需求
- 思路
- 代码
- Student.java
- ClassManage.java
- StudentOperator 接口
- StudentOperatorImpl1.java
- StudentOperatorImpl2.java
- Test.java
黑马程序员学习笔记
接口interface
接口中:变量默认为常量,方法默认为抽象方法
- 接口不能创建对象;
- 一个累可以实现多个接口,但是只能继承(extends)一个父类
- 实现多个接口要重写全部接口的全部抽象方法,否则定义该实现类为抽象类
- 实现接口用关键字implements。alt+回车重写接口所有方法
好处
- 弥补了单类继承的不足,一个类可以同时实现多个接口
- 让程序可以面向接口编程,这样就可以灵活的切换各种业务的实现
为什么要用接口
看到接口,别人显性地知道你是谁,有什么功能,从而放心地使用
接口案例
需求
思路
代码
Student.java
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-13
*/
public class Student {
private String name;
private String sex;
private double grade;
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(String name, String sex, double grade) {
this.name = name;
this.sex = sex;
this.grade = grade;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", grade=" + grade +
'}';
}
}
ClassManage.java
import java.util.ArrayList;
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-13
*/
public class ClassManage{
private ArrayList<Student> students = new ArrayList<>();
private StudentOperator studentOperator = new StudentOperatorImpl1();
//private StudentOperator studentOperator = new StudentOperatorImpl2();
public ClassManage() {
students.add(new Student("张三", "男", 90));
students.add(new Student("李四", "女", 80));
students.add(new Student("王五", "男", 70));
students.add(new Student("赵六", "女", 60));
}
public void printStudent() {
studentOperator.printAllStudent(students);
}
public void printGrade() {
studentOperator.printAverageGrade(students);
}
}
- 切换方案只需要修改
StudentOperator 接口
import java.util.ArrayList;
public interface StudentOperator {
void printAllStudent(ArrayList<Student> students);
void printAverageGrade(ArrayList<Student> students);
}
StudentOperatorImpl1.java
import java.util.ArrayList;
public class StudentOperatorImpl1 implements StudentOperator {
@Override
public void printAllStudent(ArrayList<Student> students) {
for (Student i : students){
System.out.println(i.toString());
}
}
@Override
public void printAverageGrade(ArrayList<Student> students) {
double sum = 0;
for (Student i : students){
sum += i.getGrade();
}
System.out.println("平均成绩为:" + sum / students.size());
}
}
StudentOperatorImpl2.java
import java.util.ArrayList;
public class StudentOperatorImpl2 implements StudentOperator {
@Override
public void printAllStudent(ArrayList<Student> students) {
int girl = 0;
for (Student i : students) {
if (i.getSex().equals("女")) {
girl++;
System.out.println(i.toString());
}
}
System.out.println("女生数量:" + girl);
System.out.println("男生数量:" + (students.size() - girl));
}
@Override
public void printAverageGrade(ArrayList<Student> students) {
int sum = 0;
double grades = 0;
double max = students.get(0).getGrade();
double min = students.get(0).getGrade();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getGrade() <= max) {
sum++;
grades += students.get(i).getGrade();
min = students.get(i).getGrade();
}
if (students.get(i).getGrade() > min) {
sum++;
grades += students.get(i).getGrade();
max = students.get(i).getGrade();
}
}
System.out.println("平均分:" + (grades - max - min) / (sum - 2));
}
}
Test.java
/**
* @Author: ggdpzhk
* @CreateTime: 2024-08-13
*/
public class Test {
public static void main(String[] args) {
ClassManage classManage = new ClassManage();
classManage.printGrade();
classManage.printStudent();
}
}