Java - 抽象类、equals() 和两个子类 [英] Java - abstract class, equals(), and two subclasses

查看:26
本文介绍了Java - 抽象类、equals() 和两个子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Xpto 的抽象类和两个扩展它的子类,名为 PersonCar.我还有一个名为 Test 的类,带有 main() 和一个方法 foo(),用于验证两个人或汽车(或扩展 Xpto 的类的任何对象)是否是等于.因此,我在 Person 和 Car 类中重新定义了 equals().两个人同名时相等,两辆车同名时相等.

I have an abstract class named Xpto and two subclasses that extend it named Person and Car. I have also a class named Test with main() and a method foo() that verifies if two persons or cars (or any object of a class that extends Xpto) are equals. Thus, I redefined equals() in both Person and Car classes. Two persons are equal when they have the same name and two cars are equal when they have the same registration.

但是,当我在 Test 类中调用 foo() 时,我总是得到false".我明白为什么:equals() 没有在 Xpto 抽象类中重新定义.那么...如何在 foo() 方法中比较两个人或汽车(或扩展 Xpto 的类的任何对象)?

However, when I call foo() in the Test class I always get "false". I understand why: the equals() is not redefined in Xpto abstract class. So... how can I compare two persons or cars (or any object of a class that extends Xpto) in that foo() method?

总而言之,这是我拥有的代码:

In summary, this is the code I have:

public  abstract class Xpto {


}

public class Person extends Xpto{

        protected String name;

        public Person(String name){
                this.name = name;
        }

        public boolean equals(Person p){
                System.out.println("Person equals()?");
                return this.name.compareTo(p.name) == 0 ? true : false;
        }
}

public class Car extends Xpto{
        protected String registration;

        public Car(String registration){
                this.registration = registration;
        }

        public boolean equals(Car car){
                System.out.println("Car equals()?");
                return this.registration.compareTo(car.registration) == 0 ? true : false;
        }
}

public class Teste {

        public static void foo(Xpto xpto1, Xpto xpto2){
                if(xpto1.equals(xpto2))
                        System.out.println("xpto1.equals(xpto2) -> true");
                else
                        System.out.println("xpto1.equals(xpto2) -> false");

        }

        public static void main(String argv[]){
                Car c1 = new Car("ABC");
                Car c2 = new Car("DEF");
                Person p1 = new Person("Manel");
                Person p2 = new Person("Manel");

                foo(p1,p2);
        }
}

推荐答案

正如其他人所说,您覆盖的方法的签名必须完全相同.重写方法时,为确保重写,请在函数上方使用 @Override 注释,这样 Eclipse 等 IDE 会在您更改方法时发出警告.

As the others say, the signature of the method you override must be exactly the same. When overriding methods, to make sure you are overriding, use the @Override annotation above the function, so IDEs like Eclipse will warn you if you changed the method.

这是它的样子:

@Override
public boolean equals(Object obj){
...Your code here...
}

我建议也覆盖 hashCode() 因为当将项目插入列表、集合、hastables 等时......为了相等(和执行)hashCode() 被使用(有时 equals() 不是!)

I would suggest to override hashCode() as well because when inserting items into lists, sets, hastables, etc... for equality (and performande) hashCode() is used (and sometimes equals() is not!)

所以你的最终代码是:

@Override
public boolean equals(Object obj){
...Your code here...
}

@Override
public int hashCode(){
...Your code here...
}

javadoc

这篇关于Java - 抽象类、equals() 和两个子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆