2つの類似したクラスがあるとしましょう:
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";
}
今、他のクラスでそれらの1つを使用しているIm:
public class Third {
//....
@Override
public String toString()
{
System.out.println( First.ONE );
}
}
しかし、私が今やろうとしているのは、一種のセレクターを作成することです。たとえば、クラスThirdのコンストラクターがブール値を取得し、それに基づいて使用するクラスを選択します。単に多すぎます。
要約すると、私はこれが欲しいです:
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 );
}
}
これらのクラスのインスタンスを作成せずにそれは可能ですか?