在不同包中的子类中为equals()使用super() [英] Using super() for equals() in a subclass in a different package

查看:489
本文介绍了在不同包中的子类中为equals()使用super()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Vehicle,它在包A和一个类Car,它在包B,我想使用equals方法,并利用继承的super(),但我不知道如何做到这一点。



当我尝试在main中运行该文件时,我得到:

 线程main中的异常java.lang.NullPointerException 
at vehicle.Vehicle.equals(Vehicle.java:97)
at car.Car.equals(Car.java:104)
Main.main(Main.java:48)

这是代码:

  public boolean equals(Vehicle other){
if(this.type.equals(other.type)){
if(this.year == other.year&& this.price == other.price){
return true;
} else {
return false;
}
} else {
return false;
}
}
//等于in Car
public boolean equals(Car other){
if(this.type.equals(other.type)){
if(this.speed == other.speed&& this.door == other.door){
if(super.equals(other)){
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}


解决方案

code> equals()方法根据 null 合约应传回 false c>作为参数传递


对于任何非空引用值 x x.equals(null)应返回 false


在每个 equals()方法的开头添加:

  if(other == null){
return false;
}

其次,您必须覆写 code>,不重载它:

  public boolean equals(Object other)

最后,你需要 instanceof 和向下转换, >

和BTW这个:

  if(this.speed == other.speed 
{
if(super.equals(other))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}

等效于:

  if(this.speed == other.speed&& this.door == other.door)
{
return super.equals );
}
else
{
return false;
}

,其中可以减少为:

  return this.speed == other.speed&& this.door == other.door&&超级


I have a class Vehicle which is in package A and a class Car which is in package B and I want to use equals method and take advantage of inheritance by using super(), but I don't know how to do this.

When I try to run the file in main I get this:

Exception in thread "main" java.lang.NullPointerException
    at vehicle.Vehicle.equals(Vehicle.java:97)
    at car.Car.equals(Car.java:104)
    at Main.main(Main.java:48)

Here is the code:

public boolean equals(Vehicle other) {
    if (this.type.equals(other.type)) {
        if (this.year == other.year && this.price == other.price) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
//equals in Car
public boolean equals(Car other) {
    if (this.type.equals(other.type)) {
        if (this.speed == other.speed && this.door == other.door) {
            if (super.equals(other)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}

解决方案

equals() method as per the contract should return false when null passed as an argument:

For any non-null reference value x, x.equals(null) should return false.

Add this at the very beginning of every equals() method:

if(other == null) {
  return false;
}

Secondly you have to override equals(), not overload it:

public boolean equals(Object other)

Finally you'll need instanceof and downcasting to make this all work.

And BTW this:

if (this.speed == other.speed && this.door == other.door)
{
    if(super.equals(other))
    {
        return true;
    }
    else
    {
        return false;
    }
}
else
{
    return false;
}

is equivalent to:

if (this.speed == other.speed && this.door == other.door)
{
    return super.equals(other);
}
else
{
    return false;
}

which in turns can be reduced to:

return this.speed == other.speed && this.door == other.door && super.equals(other);

这篇关于在不同包中的子类中为equals()使用super()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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