可以说我有两个类似的课程:
public class First {
public static final String ONE = "1";
public static final String TWO = "2";
public static final String THREE = "3";
}
public class Second {
public static final String ONE = "one";
public static final String TWO = "two";
public static final String THREE = "three";
}
现在在其他班级我使用其中之一:
public class Third {
//....
@Override
public String toString()
{
System.out.println( First.ONE );
}
}
但是我现在要做的是制作一种选择器,可以说类 Third 的构造函数获取布尔值并基于它选择使用哪个类,但我不想在任何地方都制作 if (..) 语句,因为它简直太多了。
所以抽象地说,我想要这样:
public class Third {
//some global var with reference? to static class
public Third(boolean first)
{
if( first ) {
//set class First as source of static fields
} else {
//set class Second
}
}
//....
@Override
public String toString()
{
System.out.println( globalVariableWithReference.ONE );
}
}
不考虑这些课程是否有可能?