Java 构造函数标识符不适用于三个对象

Java 构造函数标识符不适用于三个对象,java,object,constructor,compare,equals,Java,Object,Constructor,Compare,Equals,在这段代码中,我接受两个对象,因为我想比较三个不同的对象(1和2,1和3,2和3),因为某种原因,它无法识别对象2和3是相等的 public boolean equals(Person num1, Person num2) { if ((this.name.equals(num1.name))&&(this.address.equals(num1.address))&& (this.age==num1.age)&&(this

在这段代码中,我接受两个对象,因为我想比较三个不同的对象(1和2,1和3,2和3),因为某种原因,它无法识别对象2和3是相等的

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

}
下面是演示类

public class PersonDemo {

public static void main(String[] args) {

    //num1, num2, and num3 represent three people
    Person num1, num2, num3;
    num1=new Person("Allison", "6600 Crescent Ave", 32, 4231421);
    num2=new Person("George", "5251 Lakewood St", 24, 4489216);
    num3=new Person("George", "5251 Lakewood St", 24, 4489216);

    //name, address, age and phoneNumber are the parameters used to
    //describe each object (num1, num2, and num3)

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

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

    System.out.println("\nInformation of person 3: ");
    System.out.println(num3);

    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.");
    if (num1.equals(num3))
        System.out.println("\nPerson 1 and person 3 are identical.");
    else 
        System.out.println("\nPerson 1 and person 3 are not identical.");
    if (num2.equals(num3))
        System.out.println("\nPerson 2 and person 3 are identical.");
    else 
        System.out.println("\nPerson 2 and person 3 are not identical.");
}
} 输出: --------------------配置:--------------------

第1人的信息: 姓名:艾莉森 地址:新月大道6600号 年龄:32 电话号码:4231421

第2人的信息: 姓名:乔治 地址:莱克伍德街5251号 年龄:24 电话号码:4489216

第3人的资料: 姓名:乔治 地址:莱克伍德街5251号 年龄:24 电话号码:4489216

人员1和人员2不相同

人员1和人员3不相同

第二个人和第三个人不一样


进程已完成。

似乎要覆盖该方法

但是你的签名不同。应该是:

@Override
public boolean equals(Object o) {
    if(!(o instanceof Person))
        return false;
    Person num1 = (Person) o;
    if ((this.name.equals(num1.name))&&(this.address.equals(num1.address))&&
            (this.age==num1.age)&&(this.phoneNumber==num1.phoneNumber))
        return true;
}
  • 如果要重写
    对象.equals
    ,则必须有一个类型为
    对象的参数
  • 使用
    @Override
    确保您的方法确实覆盖了某些内容
    不是重复的,正如您所看到的,我添加了“”标记,这是一个不同的问题。基本上,我很困惑,为什么它不能将2和3识别为具有相同参数的对象。@Nambari它不是重复的。OP有不同的错误。Gucci你需要通过一个好的入门教程或一本书…@user2503916当然它会编译,但是它将是重载而不是重写。像我这样的小程序员有没有更简单的方法?如果我只是删除我的类文件中的另一个类编码,它会起作用另一个方法叫做在编码前学习基础知识。。。。这是基本的方法签名。。。。您正在使用两个参数创建一个方法,并使用一个参数调用一个方法。。。。
    @Override
    public boolean equals(Object o) {
        if(!(o instanceof Person))
            return false;
        Person num1 = (Person) o;
        if ((this.name.equals(num1.name))&&(this.address.equals(num1.address))&&
                (this.age==num1.age)&&(this.phoneNumber==num1.phoneNumber))
            return true;
    }