【JavaSE基础】对象的比较方法

对象的比较方法

当我们编写的代码为简单的基本数据类型类型时,可以用 == 或者 equals 来进行对象的比较,而当随着代码难度的提高,我们使用基本数据类型的情况也随之减少,大部分情况下我们使用的都是引用数据类型,在这种情况下,我们就不能简单的使用 == 或者 equals 来进行比较了,在这里简单介绍四种进行对象比较的方法。

1、重写基类equals方法

例如:

class Student{stu_id,score,name}
s1 = new Student("zs",100,1);
s2 = new Student("lisi",100,2);
打印:s1.equals(s2); true
class Student{
    private String name;
    private int score;
    private int stu_id;
    public Student(String name,int score,int stu_id){
        this.name = name;
        this.score = score;
        this.stu_id = stu_id;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }
        if(obj instanceof Student){
            Student student = (Student)obj;
            if(this.score == student.score){
                return true;
            }
        }
        return false;
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Student s1 = new Student("zs",100,1);
        Student s2 = new Student("lisi",100,2);
        System.out.println(s1.equals(s2));
    }
}

运行结果:

在这里插入图片描述
需要注意的是,我们在重写equals方法后,最好是将hashCode方法一起重写,原因可以参考:https://www.cnblogs.com/yuxiaole/p/9570850.html

2、实现接口中的方法

Comparable -> compareTo() 内比较器

class Student implements Comparable<Student>{
    private String name;
    private int score;
    private int stu_id;
    public Student(String name, int score, int stu_id) {
        this.name = name;
        this.score = score;
        this.stu_id = stu_id;
    }

    @Override
    public int compareTo(Student o) {
        if(this.score > o.score){
            return 1;
        }else if(this.score == o.score){
            return 0;
        }else {
            return -1;
        }
    }

    @Override
    public String toString() {
        return name;
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Student s1 = new Student("zs",100,1);
        Student s2 = new Student("lisi",90,2);
        if(s1.compareTo(s2) > 0){
            System.out.println(s1.toString()+"排名靠前");
        }else if(s1.compareTo(s2) == 0){
            System.out.println("并列");
        }else {
            System.out.println(s2.toString()+"排名靠前");
        }
    }
}

运行结果:

在这里插入图片描述

3、Comparator -> Compara()方法

外比较器

这种方法有两种写法,运行结果相同(其原理为匿名内部类)

写法一:

class People{
    private String name;
    private int age;
    public People(String name,int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}
class Compara implements Comparator<People> {
    @Override
    public int compare(People o1, People o2) {
        if(o1.getAge() > o2.getAge()){
            return 1;
        }else if(o1.getAge() == o2.getAge()){
            return 0;
        }else {
            return -1;
        }
    }
}
public class TestDemo{
    public static void main(String[] args) {
        Compara compara = new Compara();
        People people1 = new People("zs",25);
        People people2 = new People("lisi",26);
        int result = compara.compare(people1,people2);
        if(result > 0){
            System.out.println("zs");
        }else if(result == 0){
            System.out.println("相等");
        }else {
            System.out.println("lisi");
        }
    }
}

写法二:

class People{
    private String name;
    private int age;
    public People(String name,int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }
}
public class TestDemo{
    public static void main(String[] args) {
        Comparator<People> comparator = new Comparator<People>() {
            @Override
            public int compare(People o1, People o2) {
                if(o1.getAge() > o2.getAge()){
                    return 1;
                }else if(o1.getAge() == o2.getAge()){
                    return 0;
                }else {
                    return -1;
                }
            }
        };
        People people1 = new People("zs",25);
        People people2 = new People("lisi",26);
        int result = comparator.compare(people1,people2);
        if(result > 0){
            System.out.println("zs");
        }else if(result == 0){
            System.out.println("相等");
        }else {
            System.out.println("lisi");
        }
    }
}

运行结果:

在这里插入图片描述

Eg:

class People implements Comparable<People>{
    static class Company{
        private String ComName;
        private int salary;
        public Company(String ComName,int salary){
            this.ComName = ComName;
            this.salary = salary;
        }
    }
    private Company company;
    private String name;
    private int age;
    public People(String name,int age,String ComName,int salary){
        this.name = name;
        this.age = age;
        company = new Company(ComName,salary);
    }
    @Override
    public int compareTo(People o) {
        if(this.company.salary > o.company.salary){
            return 1;
        }
        else if(this.company.salary == o.company.salary){
            if(this.age > o.age){
                return 1;
            }
            else if(this.age == o.age){
                return 0;
            }
            else {
                return -1;
            }
        }
        else{
            return -1;
        }
    }

    @Override
    public String toString() {
        return name;
    }
}
public class TestDemo{
    public static void main(String[] args) {
        People people1 = new People("zs",25,"abc",20000);
        People people2 = new People("lisi",26,"abc",20000);
        if(people1.compareTo(people2) > 0){
            System.out.println(people1+"工资高");
        }else if(people1.compareTo(people2) == 0){
            System.out.println("工资相同");
        }else {
            System.out.println(people2+"工资高");
        }
    }
}

运行结果:

在这里插入图片描述

4、对象克隆(对象拷贝):Cloneable -> clone()方法
  • 重写clone()方法
  • 如果当前类中存在引用数据类型,修改该类中clone方法(单独对引用数据类型进行克隆)
  • 内存是否共享
    • 深拷贝:没有内存共享
    • 浅拷贝:有内存共享
class Address implements Cloneable{
    int home_id;
    public Address(int home_id) {
        this.home_id = home_id;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class People implements Cloneable{
    String name;
    int age;
    Address address;
    public People(String name, int age,int home_id) {
        this.name = name;
        this.age = age;
        address = new Address(home_id);
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        People o = (People)super.clone();
        o.address = (Address)this.address.clone();
        return o;
    }
}
public class TestDemo{
    public static void main(String[] args) {
        People p1 = new People("zs",10,111);
        People p2 = null;
        try {
            p2 = (People)p1.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println(p1.age+" "+p2.age);
    }
}

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值