Java的三个接口Comparable,Comparator,Cloneable(浅拷贝与深拷贝)
创始人
2025-01-11 06:33:56
0

Comparable

当我们要进行对象的比较的时候,我们是不能直接用>、< 这些符号直接进行比较的。

由于这是引用类型变量也是自定义类型变量,直接进行比较的时候,我们是通过对象的地址进行比较的我们可以使用==、!= 进行两个对象的地址是否相等,但是不能直接使用 >、< 进行比较,>、< 可以使用在基本的数据类型的比较中,因此 >、< 是不能用于地址的比较的

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在Object 类中,我们知道可以使用equals方法来进行对象的比较,返回值是布尔值。如果我们要求返回值是整型的话,我们就要使用到Comparable接口

使用

class Student implements Comparable{     public String name;     public int age;      public Student(String name, int age) {         this.name = name;         this.age = age;     }      @Override     public int compareTo(Student o) {         return this.age - o.age;     } } 

我们需要写上Comparable 接口,后面的<> 里面的内容写你要比较的对象的类型,这里包含泛型的知识,会在数据结构中讲解~~

我们先来看一下Comparable接口:

在这里插入图片描述

Comparable接口中包含 compareTo,因此我们需要重写这个方法,根据不同的比较需求来写不同的比较代码:
在这里插入图片描述
这里是实现age比较,如果是name比较,我们该怎么实现?

由于name是String类,String类有实现Comparable接口的,所以我们直接调用即可~~

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

多个同类型比较

如果我们有很多个学生需要进行比较排序,我们第一时间会想到使用数组来存放,然后通过数组排序(Array.sort())来进行比较排序。

那Array.sort 是怎么进行排序的呢?

事实上Array.sort 是根据待排序的对象中的compareTo方法进行比较的


现在这个类没有Comparable接口:
在这里插入图片描述
我们一运行就会发生异常:

在这里插入图片描述
这是是Student类不能转化为Comparable,说明Array.sort的排序需要该类实现Comparable接口。

Array.sort 会调用 compareTo 方法进行比较。


模拟实现Array.sort(冒泡排序法)

    public static void mysort(Comparable[] comparables) {         int flag = 1;         for (int i = 0; flag == 1 && i < comparables.length - 1; i++) {             flag = 0;             for (int j = 0; j < comparables.length - 1 - i; j++) {                 if(comparables[j].compareTo(comparables[j+1]) > 0) {                     Comparable tmp = comparables[j];                     comparables[j] = comparables[j+1];                     comparables[j+1] = tmp;                     flag = 1;                 }             }         }     } 

局限

由于compareTo 方法只能重写一次,实现不了重载,因为参数就是所在类的类型(也就意味着这是固定的参数),所以它的局限性就是只能进行一种数值的比较,不能进行多种数值的比较,因此我们一般用在固定的比较,用在默认的比较上,如果要实现不同的数值的比较我们会用到比较器Comparator

Comparator

我们可以使用Comparator实现不同属性比较的类,这里还是以学生类(包括姓名和年龄)作为例子:

public class NameComparator implements Comparator {     @Override     public int compare(Student o1, Student o2) {         return o1.name.compareTo(o2.name);     } }  public class AgeComparator implements Comparator {     @Override     public int compare(Student o1, Student o2) {         return o1.age - o2.age;     } } 

Comparator 后面也需要加上<>,里面填比较的类
还需要重写Comparator里面的compara方法~~

之后我们就可以使用这些比较类的方法了,和类的使用是一样的,先创建对象,再使用里面的方法:

    public static void main(String[] args) {         Student stu1 = new Student("zhangsan",10);         Student stu2 =new Student("lisi",20);          AgeComparator ageComparator = new AgeComparator();         int ret = ageComparator.compare(stu1,stu2);         System.out.println(ret);          NameComparator nameComparator = new NameComparator();         ret = nameComparator.compare(stu1,stu2);         System.out.println(ret);     } 

如果你需要使用Array.sort的话,只需要再传比较类就可以了:

        AgeComparator ageComparator = new AgeComparator();         Arrays.sort(students,ageComparator);                  NameComparator nameComparator = new NameComparator();         Arrays.sort(students,nameComparator); 

Cloneable

当我们需要进行对象的克隆(复制)的时候,我们可以使用clone的接口,这是Object类的,在上一篇文章我们就提到其中的三个方法,现在我们就来将克隆方法(clone)

在这里插入图片描述

使用

在这里插入图片描述

我们需要重写clone方法
在这里插入图片描述
因为clone方法这是protected修饰的,只能在同一个包下访问或者子类自己能访问,在Test类下是无法访问的,所以我们只能通过重写clone方法。

快捷键如下:
在这里插入图片描述
在这里插入图片描述

编译器会帮我们生成如下的代码:

    @Override     protected Object clone() throws CloneNotSupportedException {         return super.clone();     } 

在这里插入图片描述

强制类型转换,因为clone方法的放回值是Object类型的,我们需要强制类型转换为Student

Student stu2 = (Student)stu1.clone(); 

在这里插入图片描述

处理异常,异常我会在后续的文章中讲解,我们需要在调用clone方法的方法旁边写上throws CloneNotSupportedException

public static void main(String[] args) throws CloneNotSupportedException{         Student stu1 = new Student("zhangsan",14);         Student stu2 = (Student)stu1.clone();     } 

但是当我们运行的时候会发现下面的异常:
在这里插入图片描述

这里写的是不支持clone,这时候我们就需要写上Cloneable接口,这是一个空的接口,目的就是来标记这个类是支持克隆的~~

在这里插入图片描述

public class Student implements Cloneable 

完成上述步骤我们就可以实现克隆了:

在这里插入图片描述

在这里插入图片描述


当我们需要克隆的对象里面还包含一个对象的时候,如果我们不拷贝这个被包含的对象,那这就是浅拷贝,如果需要拷贝多一份新的被包含的对象时,那就是深拷贝。

以下面的代码为例:
在这里插入图片描述
在这里插入图片描述
我们来克隆一个per1:

Person per1 = new Person("zhagnsan",10); 

浅拷贝示意图:
在这里插入图片描述

深拷贝示意图:
在这里插入图片描述

浅拷贝

浅拷贝的实现和上面普通拷贝的实现是一样的,这里不赘述了,只有深拷贝有一些不一样

通过上面的示意图,我们来做一下题目,说明下面的运行结果:

public class Money {     public double m = 9.99; }  public class Person implements Cloneable{     public String name;     public int age;      public Money money = new Money();      public Person(String name, int age) {         this.name = name;         this.age = age;     }      @Override     public String toString() {         return "Person{" +                 "name='" + name + '\'' +                 ", age=" + age +                 ", money=" + money +                 '}';     }      @Override     protected Object clone() throws CloneNotSupportedException {         return super.clone();     } }  class Test{     public static void main(String[] args) throws CloneNotSupportedException{         Person per1 = new Person("zhagnsan",10);         Person per2 = (Person) per1.clone();          per2.money.m = 6.6;         System.out.println("per1:"+per1.money.m);          System.out.println("per2:"+per2.money.m);      } } 

在这里插入图片描述

在这里插入图片描述

这里显而易见,浅拷贝后per1和per2是共享money的,所以有一个人的money发生改变,另一个人的money也会发生改变。

深拷贝

我们知道深拷贝需要再拷贝多一份全新的被包含的对象,所以我们需要实现被包含的对象的拷贝:

public class Money implements Cloneable{     public double m = 9.99;      @Override     protected Object clone() throws CloneNotSupportedException {         return super.clone();     } } 

这是第一步,下一步我们需要在包含该类的类中的clone方法调用Money中的clone方法,才能实现完整的深拷贝工作:

所以我们要修改Person中的clone方法:

    @Override     protected Object clone() throws CloneNotSupportedException {         Person tmp = (Person) super.clone();         tmp.money= (Money) this.money.clone();         return tmp;     } 

现在再思考一下,下面的代码运行结果是什么?

public class Money implements Cloneable{     public double m = 9.99;      @Override     protected Object clone() throws CloneNotSupportedException {         return super.clone();     } }  public class Person implements Cloneable{     public String name;     public int age;      public Money money = new Money();      public Person(String name, int age) {         this.name = name;         this.age = age;     }      @Override     public String toString() {         return "Person{" +                 "name='" + name + '\'' +                 ", age=" + age +                 ", money=" + money +                 '}';     }      @Override     protected Object clone() throws CloneNotSupportedException {         Person tmp = (Person) super.clone();         tmp.money= (Money) this.money.clone();         return tmp;     } }  class Test{     public static void main(String[] args) throws CloneNotSupportedException{         Person per1 = new Person("zhagnsan",10);         Person per2 = (Person) per1.clone();          per2.money.m = 6.6;         System.out.println("per1:"+per1.money.m);          System.out.println("per2:"+per2.money.m);      } } 

在这里插入图片描述

在这里插入图片描述

深拷贝已经重新将Money拷贝多一份了,所以per2的money改变了并不会影响到per1的money.

相关内容

热门资讯

一分钟内幕!科乐吉林麻将系统发... 一分钟内幕!科乐吉林麻将系统发牌规律,福建大玩家确实真的是有挂,技巧教程(有挂ai代打);所有人都在...
一分钟揭秘!微扑克辅助软件(透... 一分钟揭秘!微扑克辅助软件(透视辅助)确实是有挂(2024已更新)(哔哩哔哩);1、用户打开应用后不...
五分钟发现!广东雀神麻雀怎么赢... 五分钟发现!广东雀神麻雀怎么赢,朋朋棋牌都是是真的有挂,高科技教程(有挂方法)1、广东雀神麻雀怎么赢...
每日必看!人皇大厅吗(透明挂)... 每日必看!人皇大厅吗(透明挂)好像存在有挂(2026已更新)(哔哩哔哩);人皇大厅吗辅助器中分为三种...
重大科普!新华棋牌有挂吗(透视... 重大科普!新华棋牌有挂吗(透视)一直是有挂(2021已更新)(哔哩哔哩)1、完成新华棋牌有挂吗的残局...
二分钟内幕!微信小程序途游辅助... 二分钟内幕!微信小程序途游辅助器,掌中乐游戏中心其实存在有挂,微扑克教程(有挂规律)二分钟内幕!微信...
科技揭秘!jj斗地主系统控牌吗... 科技揭秘!jj斗地主系统控牌吗(透视)本来真的是有挂(2025已更新)(哔哩哔哩)1、科技揭秘!jj...
1分钟普及!哈灵麻将攻略小,微... 1分钟普及!哈灵麻将攻略小,微信小程序十三张好像存在有挂,规律教程(有挂技巧)哈灵麻将攻略小是一种具...
9分钟教程!科乐麻将有挂吗,传... 9分钟教程!科乐麻将有挂吗,传送屋高防版辅助(总是存在有挂)1、完成传送屋高防版辅助透视辅助安装,帮...
每日必看教程!兴动游戏辅助器下... 每日必看教程!兴动游戏辅助器下载(辅助)真是真的有挂(2025已更新)(哔哩哔哩)1、打开软件启动之...