面向对象——多态,抽象类,接口(二)-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深拷贝和浅拷贝的区别

目录
相关文章
|
10月前
|
数据采集 Web App开发 iOS开发
使用 User-Agent 模拟浏览器行为的技巧
使用 User-Agent 模拟浏览器行为的技巧
|
架构师 安全 程序员
软考资料-分享
本文提供了计算机软考资源分享,包括高级、中级和初级三个层次的专业课程。高级课程如系统架构师、网络规划设计师等,中级课程如网络工程师、数据库系统工程师等,初级课程如网络管理员、程序员等,覆盖了多种专业方向,适合不同水平的学习者。
6631 0
|
存储 NoSQL MongoDB
06-MongoDB入门
06-MongoDB入门
|
3天前
|
云安全 人工智能 自然语言处理
|
7天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
716 17
|
10天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
750 57
Meta SAM3开源:让图像分割,听懂你的话
|
8天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
329 116