面向对象——多态,抽象类,接口(二)-2

简介: 面向对象——多态,抽象类,接口(二)

面向对象——多态,抽象类,接口(二)-1

https://developer.aliyun.com/article/1504154


接口常用实例

4.1 comparable接口

在java中,我们在数组中就学过了Arrays.sort()对一个整数类型的数组进行排序,那么对于在实际处理业务,有些时候排序需要我们指定一个指标进行排序,这个时候就要用到我们java中自带的comparable接口

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;
    //这里的this是谁调用了compareTo方法谁就是
    @Override
    public int compareTo(Student o) {
        //return this.age-o.age;//通过年龄进行比较
        //return (int)(this.score-o.score);//通过分数进行比较
        return this.name.compareTo(o.name);//甚至可以通过名字进行排序
    }
    
    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
       @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}
public class demo {
    public static void main(String[] args) {
        Student student=new Student("haha",24,55);
        Student student1=new Student("heihei",20,89);
        System.out.println(student.compareTo(student1));
    }
}

我们甚至可以利用这个接口对更多个数组排序,实现这个接口之后,利用Arrays.sort()进行排序

 public static void main(String[] args) {
        Student[] students =new Student[3];
        students[0]=new Student("zhan",21,98);
        students[1]=new Student("hehe",17,87);
        students[2]=new Student("keke",34,99);
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));
    }

这个类存在一定的缺点,对于类的侵害性大,所以下面我们在介绍comparator接口

4.2 comparator接口

先创立一个学生类

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

创立一个年龄比较器

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

创立一个分数比较器

//成绩比较器
class ScoreComrarator 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);
    }
}

做完上面几步之后,我们基本就可以实现一个(年龄,成绩,名字)来排序

比较分数

public class demo {
    public static void main1(String[] args) {
        Student student=new Student("haha",24,55);
        Student student1=new Student("heihei",20,89);
        ScoreComrarator scoreComrarator=new ScoreComrarator();
        scoreComrarator.compare(student1,student);
    }
}

比较多个数组的一个成绩

public class demo {
    public static void main(String[] args) {
        Student[] students =new Student[3];
        students[0]=new Student("zhan",21,98);
        students[1]=new Student("hehe",17,87);
        students[2]=new Student("keke",34,99);
        ScoreComrarator scoreComrarator=new ScoreComrarator();
        Arrays.sort(students,scoreComrarator);
        System.out.println(Arrays.toString(students));
    }
}

4.3 clonable接口和深拷贝

4.3.1 浅拷贝

class people{
    public int age;
}
public class apple {
    public static void main(String[] args) {
        people people=new people();
        people people1=people;
        System.out.println(people);
        System.out.println(people1);
    }
}

 也就是所浅拷贝就是简单来理解就是会有两个引用公用了一个对象。

4.3.2 深拷贝与clonable接口

class people implements Cloneable {
    public int age;
 
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class apple {
    public static void main(String[] args)throws CloneNotSupportedException {
        people people=new people();
        people people1=(people) people.clone();
        System.out.println(people);
        System.out.println(people1);
    }
}

所谓的深拷贝简单理解就是一个引用对应单独一个对象

4.3.3 clonable使用要点:

1 clonable接口源码是不包含任何抽象方法的,是一个空接口,对于这个空接口的作用就是表示当前类是可以被克隆的。


2 对于clone方法结合clonable我们必须要在实现接口中重写方法。


3 由于clone源码是一个object类,所以这里我们必须进行类型强转。


4 实现clone接口中需要我们抛异常,这个在后续的学习中,我们会讲解异常。


浅拷贝与深拷贝的详细理解参考这篇文章:


Java深入理解深拷贝和浅拷贝区别_riemann_的博客-CSDN博客_java深拷贝和浅拷贝的区别

目录
相关文章
|
5天前
|
安全 Java 程序员
面向对象——多态,抽象类,接口(二)-1
面向对象——多态,抽象类,接口(二)
14 1
【面向对象】抽象类和接口
【面向对象】抽象类和接口
|
8月前
|
设计模式 数据安全/隐私保护
面向对象编程基础:封装、继承、多态与抽象的全面解析
面向对象编程基础:封装、继承、多态与抽象的全面解析
32 0
|
Java
Java面向对象—抽象类和接口
Java面向对象—抽象类和接口
81 0
|
Java
Java面向对象之抽象类与接口
抽象类的使用原则如下: (1)抽象方法必须为public或者protected(因为如果为private,则不能被子类继承,子类便无法实现该方法),默认为public; (2)抽象类也有构造器 (3)抽象类不能直接实例化,需要依靠子类采用向上转型的方式处理; (4)外部抽象类不允许使用static声明,而内部的抽象类运行使用static声明。使用static声明的内部抽象类相当于一个外部抽象类,继承的时候使用“外部类.内部类”的形式表示类名称 (5)有时候由于抽象类中只需要一个特定的系统子类操作,所以可以忽略掉外部子类。这样的设计在系统类库中会比较常见,目的是对用户隐藏不需要知道的子类
73 0
面向对象的封装,继承,多态(一)
面向对象的封装,继承,多态。
64 0
面向对象的封装,继承,多态(一)
|
编译器
面向对象编程的组合、多态、抽象类的基本介绍(二)
面向对象编程的组合、多态、抽象类的基本介绍
面向对象编程的组合、多态、抽象类的基本介绍(二)
|
Java 程序员 编译器
面向对象编程的组合、多态、抽象类的基本介绍(一)
面向对象编程的组合、多态、抽象类的基本介绍
面向对象编程的组合、多态、抽象类的基本介绍(一)
笔记12-多态&抽象类&接口
笔记12-多态&抽象类&接口