【问题标题】:Java identical recognition of objects [duplicate]Java对对象的相同识别[重复]
【发布时间】:2013-06-28 02:18:52
【问题描述】:

所以看起来我的 java 程序无法识别两个相同的对象,即使我使用的方法与上一个问题中 java 确实将两个对象识别为相同的方法相同。这个程序的唯一区别是我有一个额外的keyboard.nextLine(),这是输入对象二的名称/地址所必需的。

这是类输入

    public boolean equals(Person num1) {
    if ((this.name==num1.name)&&(this.address==num1.address)&&
    (this.age==num1.age)&&(this.phoneNumber==num1.phoneNumber))
        return true;
    else
        return false;
}

这是演示输入

   import java.util.Scanner;
   public class PersonDemo {

   public static void main(String[] args) {

    Person num1, num2;
    num1=new Person();
    num2=new Person();

    String name, address;
    int age; 
    long phoneNumber;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Input the name of person 1:");
    name=keyboard.nextLine();
    System.out.println("Input the address of person 1:");
    address=keyboard.nextLine();
    System.out.println("Input the age of person 1:");
    age=keyboard.nextInt();
    System.out.println("Input the phone number of person 1:");
    phoneNumber=keyboard.nextLong();
    keyboard.nextLine();

    num1.setName(name);
    num1.setAddress(address);
    num1.setAge(age);
    num1.setPhoneNumber(phoneNumber);

    System.out.println("\nInformation of person 1: ");
    System.out.println(num1);

    System.out.println("\nInput the name of person 2:");
    name=keyboard.nextLine();
    System.out.println("Input the address of person 2:");
    address=keyboard.nextLine();
    System.out.println("Input the age of person 2:");
    age=keyboard.nextInt();
    System.out.println("Input the phone number of person 2:");
    phoneNumber=keyboard.nextLong();

    num2.setName(name);
    num2.setAddress(address);
    num2.setAge(age);
    num2.setPhoneNumber(phoneNumber);

    System.out.println("\nInformation of person 2: ");
    System.out.println(num2);

    if (num1.equals(num2))
        System.out.println("\nPerson 1 and person 2 are identical.");
    else 
        System.out.println("\nPerson 1 and person 2 are not identical.");
}


   } 

【问题讨论】:

  • 为什么equals 方法中的if 语句?只需返回布尔值...
  • 如果这些东西匹配,那么我返回 true :O
  • @user2514022 是的,但只返回“这些东西”而不是使用if 语句。这就像说if (something) return true; else return false; 而不仅仅是return something
  • 我现在可以关闭这个问题了,谢谢你

标签: java object compare


【解决方案1】:

对于Object(such as String),需要使用equals方法。 == 只测试这两个 Object 是否是同一个对象(比较内存地址)。在您的情况下,num1.namenum2.name 是不同的对象,尽管它们的内容相同。

【讨论】:

    【解决方案2】:

    问题:

    • 没有正确覆盖 Object.equals 方法
    • 在 equals 方法中,使用 == 而不是 equals 测试相等性
    • 不测试 num1 是否为空

    这样做:

    @Overrides
    public boolean equals(Object num1) {
        boolean result = false;
        if (num1 != null && num1 instanceof Person) {
            Person personNum1 = (Person)num1;
            result = this.name.equals(personNum1.name) &&
                     this.address.equals(personNum1.address) &&
                     this.age.equals(personNum1.age) && 
                     this.phoneNumber.equals(personNum1.phoneNumber);
        }
        return result;
    }
    

    顺便说一句,任何关于 equals 的合理教程都会显示这一点。 :)

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 2013-04-20
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2011-09-13
      • 2016-05-08
      • 2013-06-28
      • 2018-06-17
      相关资源
      最近更新 更多