接口回调

##接口的好处:##
1、降低程序的耦合度,分离使用者与实现
2、更自然的使用工具,更加关注行为
3、设计与实现完全分离,更容易搭建框架,更换实现

##回调接口:##
先有使用者后有实现方
可以理解为使用者用接口做参数作为工具类,调用接口方法但并不去实现,
而是留给其他类去实现方法补充给工具类,实现后谁调用工具谁给工具传参数

##已下为学生类数组排序:##
import java.util.Arrays; //导入数组
import java.lang.Comparable; //导入比较工具
public class TestSort {
public static void main(String[] args) {
Student[] stu = new Student[] {
new Student(“zs” , 90.0),
new Student(“ls” , 95.0),
new Student(“xm” , 88.5),
new Student(“xh” , 99.5)
};
Arrays.sort(stu); //调用并传参
for(int i = 0 ; i < stu.length ; i++) {
System.out.println(stu[i].getName() + “\t” + stu[i].getScore());
}
}
}
//实现比较工具
class Student implements Comparable{
private String name;
private double score;
public Student(String name , double score) {
this.name = name;
this.score = score;
}
public String getName() {
return this.name = name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return this.score = score;
}
public void setScore(double score) {
this.score = score;
}
public int compareTo(Student stus) {
if(score > stus.score) {
return 1;
}else if(score < stus.score){
return -1;
}
return 0;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值