假设我有以下情况:
public abstract class Vehicle {
public void turnOn() { ... }
}
public interface Flier {
public void fly();
}
有没有办法可以保证任何实现的类也Flier
必须扩展Vehicle
?我不想创建Flier
一个抽象类,因为我希望能够以类似的方式混合其他一些接口。
例如:
// I also want to guarantee any class that implements Car must also implement Vehicle
public interface Car {
public void honk();
}
// I want the compiler to either give me an error saying
// MySpecialMachine must extend Vehicle, or implicitly make
// it a subclass of Vehicle. Either way, I want it to be
// impossible to implement Car or Flier without also being
// a subclass of Vehicle.
public class MySpecialMachine implements Car, Flier {
public void honk() { ... }
public void fly() { ... }
}