避免在Java中重复导入:继承导入? [英] Avoid repeated imports in Java: Inherit imports?
本文介绍了避免在Java中重复导入:继承导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法继承进口?
示例:
普通枚举:
public enum Constant{ ONE, TWO, THREE }
使用此枚举的基类:
public class Base {
protected void register(Constant c, String t) {
...
}
}
需要导入的子类使用枚举常量方便(不含枚举名称):
Sub class needing an import to use the enum constants convenient (without enum name):
import static Constant.*; // want to avoid this line!
public Sub extends Base {
public Sub() {
register(TWO, "blabla"); // without import: Constant.TWO
}
}
另一个类同样导入...
and another class with same import ...
import static Constant.*; // want to avoid this line!
public AnotherSub extends Base {
...
}
我可以使用经典的静态最终常量,但也许有一种方法可以使用相同的方便的常用枚举。
I could use classic static final constants but maybe there is a way to use a common enum with the same convenience.
推荐答案
import
只是帮助编译器查找类。它们对单个源文件是活跃的,与Java的OOP机制无关。
import
s are just an aid to the compiler to find classes. They are active for a single source file and have no relation whatsoever to Java's OOP mechanisms.
所以,不,你不能继承 import
s
So, no, you cannot "inherit" import
s
这篇关于避免在Java中重复导入:继承导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文