对象比较

对象比较就是两个对象的属性进行比较

对象比较的实现形式一

class Person

{

  private String name ;

  private int age ;

  public Person(String name , int age)

  {

   this.name = name;

   this.age = age; 

  }

  public String getName() {

    return this.name;

  }

  

public int  getAge() {

    return this.age;

  }

}

public class Testdemo{

  public static void main(String args[]){

  Person perA = new Person("张三",20);

  Person perB= new Person("张三",20);

  System.out.println(perA == perB);       //需要根据对象拥有的属性信息来进行比对   false

  if (perA.getName().equals(perB.getName()) && perA.getAge() == perB.getAge()) {

    System.out.println("两个对象相等!");           //   两个对象相等

  }

    else  {

      System.out.println("两个对象不相等!");

      }

  }  

}

原文地址:https://www.cnblogs.com/123talents/p/7469175.html