有没有办法保证接口在Java中扩展类?
2022-09-01 08:48:32
假设我有以下情况:
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() { ... }
}