【发布时间】:2018-02-07 19:50:33
【问题描述】:
我有一个接口,该接口有一个自身的方法,例如
public interface Vehicle {
void bump(Vehicle other);
}
现在,我想实现这个接口,使 Vehicle 只会撞到它自己类型的 Vehicles。也就是说,我想要诸如
之类的东西public class BumperCar implements Vehicle {
public void bump(BumperCar other){
System.out.println("They bounce off harmlessly and continue going.")
}
}
public class Train implements Vehicle {
public void bump(Train other){
System.out.println("Breaking news: Dozens die in horrible train on train collision.")
}
}
但是 BumperCars 和 Trains 之间的颠簸没有任何作用,即使两个类都必须实现颠簸(Vehicle)。实现这一目标的最佳方法是什么?
【问题讨论】:
-
您没有覆盖该方法。它有不同的签名。
-
您是否希望不同类型的车辆相互碰撞。?此外,在实际使它们碰撞的地方添加代码。
标签: java methods types parameters interface