Java基础——对象比较方法 equals、 compareTo、 compare、clone

目录

equals方法的使用 

comparaTo的使用

clone方法的使用

compare的使用


equals方法的使用 

equals方法比较两者是否相等,一个类如果没有写明继承某一个类,则它默认继承Object类,在Object中,equals的比较方式是等号比较(s1.equals( s2)等价于 s1==s2),

定义一个Student类,定义私有属性name,age,score,重写equals方法,比较两个对象之间的成绩,若相等,返回true,不等则返回false。

public class Student{
   private String name;
   private int age;
   private int score;

   public Student(String name,int age,int score){
      this.name=name;
      this.age=age;
      this.score=score;
   }
   @Override
   public boolean equals(Object obj){     
      if(this == obj){    // 当前对象 this  比较对象  obj
         return true;
      }
      if(obj instanceof Student){
          Student student =(Student)obj;
          if(this.score == student.score){
             return true;
          }
      }return false;
   }
}

在TestDome中写测试类,通过equals方法调用来比较两个对象的成绩是否并列。

public class TestDome{
   public static void main(String[] args){
      Student s1 = new Student("zs",12,98);
      Student s2 = new Student("lisi",13,100);
      if(s1.equals(s2)){
         System.out.println("成绩并列");
      }else{
         System.out.println("成绩不并列");
      }
   }
}

comparaTo的使用

compareTo是Comparable接口中的方法,compareTo的返回值类型是String,String类型底层是以char数据类型进行存储的,比较大小的过程为,根据ASCII码值或Unicode编码等编码方式判断大小两者的第一个字符先进行比较,若相同,则第二个字符比较,以此类推,若有一方字符全部比较完后,仍相同,则比较字符长度。

若前者大于后者,则结果为正数,前者等于后者,结果为0,前者小于后者,结果为负数。

同上述例子,Student类继承父类Object 实现接口Comparable,重写Comparable中的compareTo方法,并在testDome中进行s1.compareTo(s2)。

public class Student implements Comparable<Student>{   //<>中填入当前比较对象类型

    private String name;
    private int age;
    private int score;

    public Student(String name,int age,int score){
        this.name=name;
        this.age=age;
        this.score=score;
    }

    @Override
    public int compareTo(Student o) {
        if(this.score == o.score){
            return 0;
        }else if(this.score > o.score){
            return 1;
        }else{
            return -1;
        }
    }
}
public class testDome {
    public static void main(String[] args){
        Student s1 = new Student("zs",12,98);
        Student s2 = new Student("lisi",13,100);
        System.out.println(s1.compareTo(s2));
    }
}

clone方法的使用

clone()方法是Cloneable接口中的方法,clone方法是使用c++实现的。

1.数组的克隆

int[] brr = arr.clone();

2.Object 中的clone方法只能在在子类中使用,无法在外部进行调用,需要在当前类中进行重写。

如果在类中要用到clone方法,必须实现Cloneable接口。即implements  Cloneable<>

定义一个Student类实现Cloneable接口,先克隆当前对象的堆上的内容,在克隆当前对象的地址,再将克隆的地址给到新对象,最后返回新对象,即完成了深拷贝(不共享同一片内存)

public class Student implements Cloneable {

    Address address;
    private String name;
    private int age;
    private int score;

    public Student(int home_id, String name, int age, int score) {

        address = new Address(1);
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Object newobject = super.clone();
        Student newstudent = (Student) newobject;
        newstudent.address = (Address) this.address.clone();
        return newstudent;
    }
}

定义一个Address类实现Cloneable接口

public class Address implements Cloneable {
    private int home_id;

    public Address(int home_id) {
        this.home_id = home_id;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

在TestDome中进行打印,比较两者的地址,运行结果是false

public class TestDome {

    public static void main(String[] args) throws CloneNotSupportedException {
        Student s1 = new Student(1,"zs",12,98);
        Student s2 =(Student) s1.clone();
        System.out.println(s1.address==s2.address);
    }

compare的使用

定义一个Com实现Comparator接口,<>中是要比较的两个对象的类型。在Com类中实现对compare方法的重写。o1是Student1类,与Com类没有关系,若要调用Student1中的私有属性,则需要使用方法getScore(getScore可直接在Student1快捷键生成:Alt+ins,选择getter,选择要生成的方法即可)

class Com implements Comparator<Student1>{
    @Override
    public int compare(Student1 o1, Student1 o2) {
        if(o1.getScore() >o2.getScore()){
            return 1;
        }if(o1.getScore() == o2.getScore()){
            return 0;
        }else{
            return -1;
        }
    }
}
public class Student1 {
    private String name;
    private int age;
    private int score;

    public Student1(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public int getScore() {
        return score;
    }
}

在main方法中进行测试运行结果为-1,与预期一致

public static void main(String[] args) {
        Student1 S1=new Student1("zs",13,90);
        Student1 S2=new Student1("lisi",13,98);
        Com com =new Com();
        System.out.println(com.compare(S1,S2));
    }

应用匿名内部类使用Comparator接口中的compare方法:

public class Student1 {
    private String name;
    private int age;
    private int score;

    public Student1(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public int getScore() {
        return score;
    }
}
public static void main(String[] args) {
        Student1 S1=new Student1("zs",13,90);
        Student1 S2=new Student1("lisi",13,98);
        //比较器对象
        Comparator<Student1> comparator = new Comparator<Student1>() {
            @Override
            public int compare(Student1 o1, Student1 o2) {
                if (o1.getScore() > o2.getScore()) {
                    return 1;
                }
                if (o1.getScore() == o2.getScore()) {
                    return 0;
                } else {
                    return -1;
                }
            }
        };   //使用匿名内部类重写compare方法
    System.out.println(comparator.compare(S1,S2));
}

 匿名内部类:https://mp-new.csdn.net/mp_blog/creation/editor/117373623

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值