Comparable、Comparator 和 Cloneable

Comparable:

//理解comparable接口:
class Student implements Comparable<Student>{ //首先要实现 comparable 的接口
    public int age;
    public String name;
    public double score;
    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
    @Override  // 其次要重写 compareTo 的方法
    public int compareTo(Student o) {
        //if (this.age > age){
        //return 1;
        //}else if (this.age == age){
        //    return 0;
        //}else{
        //    return -1;
        //} //这是用if实现的
        //return ths.age - o.age; //age从小到大
        //return o.age - this.age;  //age从大到小
        return this.name.compareTo(o.name);
    }
}

comparable 使用时:

1、要在类中实现 comparable 的接口

2、要在类中重写 compareTo 的方法

comparable 的缺点 :

对类的侵入性非常强,一旦写好不可以再改动。

在此处 compareTo 方法的返回值是 int ,有三种情况:
1、比较者大于被比较者(也就是compareTo方法里面的对象),那么返回整数。
2、比较者等于被比较者,那么返回 0 。
3、比较者小于被比较者,那么返回整数。


comparator:

//比较器
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class ScoreComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return (int) (o1.score - o2.score);//要进行强制类型转换
    }
}
class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);//要进行强制类型转换
    }
}

class Student {
    public int age;
    public String name;
    public double score;
    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}

class Text{
public static void main(String[] args){
//现在是一个类:
            Student[] students = new  Student[3];
            students[0] = new Student(17,"张三",99);
            students[1] = new Student(18,"李四",78);
            students[2] = new Student(12,"王五",100);
            System.out.println(Arrays.toString(students));
            //用比较器比较 (按照年龄)
            System.out.println();
            AgeComparator ageComparator = new AgeComparator();
            Arrays.sort(students,ageComparator);
            System.out.println(Arrays.toString(students));
            //用比较器比较(按照分数)
            ScoreComparator scoreComparator = new ScoreComparator();
            Arrays.sort(students,scoreComparator);
            System.out.println(Arrays.toString(students));
            //用比较器比较(按照名字)
            NameComparator nameComparator = new NameComparator();
            Arrays.sort(students,nameComparator);
            System.out.println(Arrays.toString(students));

}
}

比较器是一种灵活的策略模式,在不更改代码的前提下对代码内的数据进行排序。

在此处 compare 方法(参数有o1 , o2)的返回值是 int ,有三种情况:
1、o1 > 02,返回整数。
2、o1 = o2,返回 0 。
3、o1 < o2,返回整数。


Cloneable:

//理解cloneable:
class Dance implements Cloneable{ //实现被克隆一定要有cloneable接口
    public int times;
    public void act(){
        System.out.println("Move");
    }
    @Override
    public String toString() {
        return "Dance{" +
                "times= " + times +
                '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class Text{
public static void main(String[] args) throws CloneNotSupportedException {//要进行异常抛出
 //理解cloneable:
            Dance dance1 = new Dance();
            dance1.times = 20;
            System.out.println(dance1.toString());
            Dance dance2 = (Dance)dance1.clone();//调用clone
            //1、dance类拥有clone接口
            //2、类内重写clone方法
            //3、主方法抛出clone异常
            System.out.println(dance2);
}
}

Cloneable 使用时:

1、类拥有 Cloneable 接口

2、类内重写 clone 方法

3、主方法抛出 clone 异常

问题:为什么 Cloneable 是一个空接口?有何作用?

答:空接口是一个标记接口,它用来表示一个类拥有某些希望具有的特征。Cloneable 为空接口代表这个类可以被克隆。


浅拷贝 → 拷贝地址 → 副本更改影响实际数据

深拷贝 → 拷贝对象 → 副本更改不影响实际数据

决定浅拷贝还是深拷贝的与采用何方法无关,而取决于代码具体的实现过程。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值