最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaSE的三大接口:Comparator,Comparable和Cloneable詳解

 更新時間:2021年10月14日 10:05:15   作者:飛人01_01  
這篇文章主要介紹了詳解JavaSE中Comparator,Comparable和Cloneable接口的區(qū)別的相關(guān)資料,希望通過本文大家能徹底掌握這部分內(nèi)容,需要的朋友可以參考下

進(jìn)階JavaSE-三大接口:Comparator、Comparable和Cloneable。

Comparable和Comparator這兩個接口很相似,都是用于比較大小的接口。在我們寫一些數(shù)據(jù)結(jié)構(gòu)的算法題時,用的比較多,具體是怎么用的,我們接著往下看。

Comparator接口:

public interface Comparator<T> {
    public int compare(T o1, T o2); //比較方法
}

Comparable接口:

public interface Comparable<T> {
   public int compareTo(T o);
}

在具體實(shí)現(xiàn)的類中,實(shí)現(xiàn)Comparable接口,然后在類里面重新compareTo方法,就能這個類具有可比較的能力,在添加完數(shù)據(jù)后,可以直接調(diào)用Collections.sort() 或者Arrays.sort() 方法,就能對裝有這個類的對象的集合進(jìn)行排序。

Comparator接口,是不需要在被排序?qū)ο蟮念愔袑?shí)現(xiàn)這個接口的,這個接口是自己單獨(dú)實(shí)現(xiàn)一個類,實(shí)現(xiàn)這個接口。然后調(diào)用Collections.sort(),或者其他方法,就可以將被排序的集合和這個接口一起傳過去,就能實(shí)現(xiàn)排序。

Comparator接口:自己單獨(dú)實(shí)現(xiàn)排序類。不需要在被排序的類中實(shí)現(xiàn)。

Comparable接口:必須在被排序的類中實(shí)現(xiàn)這個接口。

上訴兩個接口實(shí)現(xiàn)后,都需要重寫相應(yīng)的比較方法。

具體實(shí)例:

//Comparator接口實(shí)例
class Student {
    public String name;
    public int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return this.name + " " + this.age;
    }
}
public class Demo {
    //實(shí)現(xiàn)Comparator接口的類
    private static class AgeCompare implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            return o1.age - o2.age; //以年齡進(jìn)行排序
        }
    }
    public static void main(String[] args) {
        Student student1 = new Student("小明", 10);
        Student student2 = new Student("小剛", 5);
        Student student3 = new Student("彭于晏", 28);
        Student student4 = new Student("胡歌", 26);
        Student[] array = new Student[4];
        array[0] = student1;
        array[1] = student2;
        array[2] = student3;
        array[3] = student4;
        System.out.println("排序前: " + Arrays.toString(array));
        Arrays.sort(array, new AgeCompare()); //以年齡進(jìn)行排序
        System.out.println("排序后: " + Arrays.toString(array));
    }
}

最后輸出的結(jié)果:

image-20211012152708784

Comparable接口示例:

//Comparable接口示例
class Student implements Comparable<Student>{
    public String name;
    public int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return this.name + " " + this.age;
    }
    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name); //以姓名進(jìn)行排序
    }
}
public class Demo2 {
    public static void main(String[] args) {
        Student student1 = new Student("Tom", 10);
        Student student2 = new Student("Emma", 5);
        Student student3 = new Student("Alice", 20);
        Student student4 = new Student("Kate", 30);
        Student[] array = new Student[4];
        array[0] = student1;
        array[1] = student2;
        array[2] = student3;
        array[3] = student4;
        System.out.println("排序前:" + Arrays.toString(array));
        Arrays.sort(array); //以姓名進(jìn)行排序
        System.out.println("排序后:" + Arrays.toString(array));
    }
}

最終輸出結(jié)果:

image-20211012153701172

注:String類里面的CompareTo方法,是按照字典序的大小進(jìn)行比較。簡單點(diǎn)說,每個字符都有相應(yīng)的ASCII碼值,這個方法會從頭開始比較每個字符,如果前者小于后者,返回-1,相等返回0,大于就返回1。具體的注釋可以查看幫助文檔。

以上就是兩個比較接口的使用,這兩接口通常也叫做比較器。

Cloneable接口:用于克隆的。

image-20211012154540768

也就是說,一個類要想實(shí)現(xiàn)克隆的功能,需要實(shí)現(xiàn)Cloneable接口,實(shí)現(xiàn)這個接口后,還必須自己手動的書寫Object的clone方法。

切記:在沒有實(shí)現(xiàn)接口的情況下,調(diào)用克隆方法,會拋出異常。

Cloneable示例:

image-20211012164332692

通過person對象,調(diào)用克隆方法,就能實(shí)現(xiàn)克隆。那么問題來了,這是深拷貝還是淺拷貝?關(guān)于深拷貝淺拷貝,我前面有一篇文章講過

深拷貝與淺拷貝。

我們來看下面這一段代碼,可能你就會更好理解這個Cloneable接口:

image-20211012170301410

上訴代碼所對應(yīng)的內(nèi)存圖如下:

image-20211012170736798

此時如果我們通過person1來改變money里面的值,那么person對象里面的也會被修改。因?yàn)楸举|(zhì)上這兩個對象的money值是指向同一塊空間的。這也就是淺拷貝。

那么要實(shí)現(xiàn)深拷貝,該如何?

那就將money對象,再拷貝一份出來,讓person1的money值指向新的空間即可。

class Money implements Cloneable {
    public int number;
    public Money(int number) {
        this.number = number;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable {
    public String name = "Tom";
    public Money money = new Money(100);
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person clone = (Person) super.clone();
        clone.money = (Money) this.money.clone();
        return clone;
    }
}
public class Demo3 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person1 = (Person)person.clone(); //調(diào)用person對象的克隆方法
        System.out.println(person.name + " " + person.money.number);
        System.out.println("===========");
        person1.money.number = 20;
        System.out.println(person1.name + " " + person1.money.number);
    }
}

運(yùn)行結(jié)果:

image-20211012171345288

注意一下,Person類里面克隆方法的修改,并且Money類也是需要實(shí)現(xiàn)Cloneable接口的。

image-20211012171507311

切記,Cloneable接口是一個空的接口,也叫標(biāo)記接口。這個接口的存在,只是為了證明當(dāng)前這個類是有克隆方法。如果不寫這個接口,調(diào)用克隆方法,會報異常。

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評論

花垣县| 四子王旗| 郑州市| 且末县| 清镇市| 新乡市| 高邑县| 云安县| 东乌珠穆沁旗| 德清县| 溧阳市| 兴义市| 平定县| 信宜市| 镇沅| 浪卡子县| 公主岭市| 湄潭县| 印江| 彩票| 江都市| 姜堰市| 清流县| 扎赉特旗| 梅河口市| 旅游| 连州市| 富川| 新巴尔虎左旗| 申扎县| 龙岩市| 安多县| 铁力市| 定日县| 涿鹿县| 阳西县| 连南| 独山县| 集贤县| 东乡| 虎林市|