导入的常量类字段的简称 [英] Short name for imported constant class fields
问题描述
我有一个具有数百个配置常量的Java程序:
I have a java program with hundreds of configuration constants:
public static final String C1="C1";
public static final String C2="C2";
由于它们太多,因此我将它们放在一个单独的类中,即 MyClassConstants
.现在,我需要在 MyClass
上使用它们:
Since there are so many of them, I've put them into a separate class, MyClassConstants
.
Now, I need to use them on MyClass
:
import mynamespace.MyClassConstants;
myMethod( MyClassConstants.C1, MyClassConstants.C2 );
这变得非常冗长,所以我想知道是否可以通过某种方式直接导入字段:
This gets very verbose very fast, so I was wondering if it was possible to somehow import the fields directly:
import mynamespace.MyClassConstants.*;
myMethod( C1, C2 ); //doesn't work
或者至少要重命名导入:
Or at the very least, rename the import:
import mynamespace.MyClassConstants as C; //javac hates me
myMethod( C.C1, C.C2 );
有没有办法做到这一点,并且对于常量仍然具有有意义的类名?还是应该使用其他方法?
Is there a way to do this and still have a meaningful class name for the constants? Or should I use another approach?
推荐答案
答案是静态导入
,您可以使用它来解决:
The answer is Static import
, you can solve by using it:
导入静态mynamespace.MyClassConstants.*;
另请参见:
这篇关于导入的常量类字段的简称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!