有没有办法"继承"进口?
例:
常用枚举:
public enum Constant{ ONE, TWO, THREE }
Run Code Online (Sandbox Code Playgroud)
使用此枚举的基类:
public class Base {
protected void register(Constant c, String t) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
需要导入的子类使用方便的枚举常量(没有枚举名称):
import static Constant.*; // want to avoid this line!
public Sub extends Base {
public Sub() {
register(TWO, "blabla"); // without import: Constant.TWO
}
}
Run Code Online (Sandbox Code Playgroud)
和另一个具有相同导入的类......
import static Constant.*; // want to avoid this line!
public AnotherSub extends Base {
...
}
Run Code Online (Sandbox Code Playgroud)
我可以使用经典的静态最终常量,但也许有一种方法可以使用具有相同方便性的常用枚举.
可能重复:
为什么Java编译器不支持导入继承?
我是否可以执行以下操作:假设我有一个A扩展类的类B.B有导入类C,它有自己的方法B使用.可以A使用那些相同的方法C而不C直接导入,因为A扩展B?