我有一个位于包 A 中的类 Vehicle 和一个位于包 B 中的类 Car,我想使用 equals 方法并通过使用 super() 来利用继承,但我不知道该怎么做。
当我尝试在 main 中运行文件时,我得到了这个:
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)
这是代码:
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;
}
}