JAVA初窥-DAY16

JAVA初窥-DAY16

    • 接口的扩展
    • 排序接口
        • Comparable接口
        • comparator接口
    • 克隆接口
        • Cloneable接口

接口的扩展

排序接口

若某个自建类型中有多个对象且需要排序,则让此类型实现排序所需的接口,那么通过数组来存储多个对象,并对其进行排序。

Comparable接口

comparable接口中 comparaTo方法需要重写,comparaTo接收的参数类型为要比较的对象。
返回值为int类型
0:位置不变
1(>0): 位置交换
-1(<0):位置不变

class Stu implements Comparable<Stu>{
     //为将要比较的数据类型
    public int age;
    public String name;
    public Stu(int age,String name){
     
        this.name = name;
        this.age = age;
    }
    public String toString() {
     
        return "Stu{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
    public int compareTo(Stu o) {
     
        return this.name.compareTo(o.name);//此时通过name来给stus数组排序
    }
    /* public int compareTo(Stu o){//接口中方法的重写
        return this.age-o.age;//此时通过年龄来给stus数组排序
    }*/
}
public class Test2{
     
    public static void main(String[] args){
     
        Stu[] stus = new Stu[3];
        stus[0] = new Stu(10,"qqq");
        stus[1] = new Stu(40,"www");
        stus[2] = new Stu(30,"eee");
        Arrays.sort(stus);//通过sort方法来调用Arrays类实现的接口comparable
        System.out.println(Arrays.toString(stus));
    }

comparable接口一旦写死一种比较方式,那么就不能轻易修改了。

comparator接口

comparator比较器更方便,你想使用什么来比较进行操作,就自己实现一个相关的比较器如:

class Stu {
     
    public int age;
    public String name;
    public Stu(int age,String name){
     
        this.name = name;
        this.age = age;
    }
    public String toString() {
     
        return "Stu{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
class AgeComparator implements Comparator<Stu>{
     
    public int compare(Stu o1,Stu o2) {
     //通过年龄来排序
        return o1.age-o2.age;
    }
}
class NameComparator implements Comparator<Stu>{
     
    public int compare(Stu o1, Stu o2) {
     //通过名字来排序
        return o1.name.compareTo(o2.name);
    }
}
public class Test{
     
    public static void main(String[] args){
     
        Stu[] stus = new Stu[3];
        stus[0] = new Stu(10,"qqq");
        stus[1] = new Stu(40,"www");
        stus[2] = new Stu(30,"eee");
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(stus,ageComparator);//调用年龄的重写compare方法
        System.out.println(Arrays.toString(stus));
        System.out.println("========================");
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(stus,nameComparator);//调用名称的重写compare方法
        System.out.println(Arrays.toString(stus));
    }
}

克隆接口

同理,某个自建类型需要用到克隆时,则让自建类型实现克隆相关接口即可。

Cloneable接口

这个接口里面什么也没有(属性和方法),则说这个是空接口或标识接口,如果有一个类,实现了此接口,则证明这个类是可以被克隆的。但此时还是不能克隆,需要重写父类Object中的clone方法,如:

class Car{
     
    public int have=0;

    public Car(int have) {
     
        this.have = have;
    }
}
class Worker implements Cloneable{
     
    public int id;
    public String name;
    public Car car;
    public Worker(int id, String name) {
     
        this.id = id;
        this.name = name;
        this.car = new Car(1);
    }
    @Override
    public String toString() {
     
        return "Worker{" + "id=" + id + ", name='" + name + '\'' + ", car=" + car.have + '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
     
        return super.clone();
    }
}
public class Demo01 {
     
    public static void main(String[] args) throws CloneNotSupportedException {
     
        Worker wor1 = new Worker(18,"lit");
        Worker wor2 = (Worker) wor1.clone();
        System.out.println(wor1);
        System.out.println(wor2);
        System.out.println("===================================");
        wor1.car.have=5;
        wor1.name = "ttt";
        System.out.println(wor1);
        System.out.println(wor2);
    }
}

此时,虽然Worker类型克隆成功,但是经过测试,发现克隆为浅克隆。则需:

package Demo0421.Demo01;

class Car implements Cloneable{
     
    public int have=0;
    public Car(int have) {
     
        this.have = have;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
     
        return super.clone();
    }
}
class Worker implements Cloneable{
     
    public int id;
    public String name;
    public Car car;
    public Worker(int id, String name) {
     
        this.id = id;
        this.name = name;
        this.car = new Car(1);
    }
    @Override
    public String toString() {
     
        return "Worker{" + "id=" + id + ", name='" + name + '\'' + ", car=" + car.have + '}';
    }
    @Override
    protected Worker clone() throws CloneNotSupportedException {
     
        Worker c = (Worker)super.clone();
        c.car = (Car) c.car.clone();
        return c;
    }
}
public class Demo01 {
     
    public static void main(String[] args) throws CloneNotSupportedException {
     
        Worker wor1 = new Worker(18,"lit");
        Worker wor2 =  wor1.clone();
        System.out.println(wor1);
        System.out.println(wor2);
        System.out.println("===========================================");
        wor1.car.have=5;
        wor1.name = "ttt";
        System.out.println(wor1);
        System.out.println(wor2);
    }
}

此时就为深拷贝。

你可能感兴趣的:(JAVA初窥)