考えがある。
のtestVariable
型Object
(またはDummyType
を拡張するクラスObject
) を作成します。次に、システム変数から読み取った内容に基づいて、プリミティブ ラッパー クラスを使用して、任意のデータを変数にロードできます。
そう:
public class Test {
Object testVariable;
{
String whichType = null;
//Logic to read your system variable from wherever and store it in whichType
if(whichType.equals("int")) {
testVariable = new Integer(intVal);
}
else if(whichType.equals("double")) {
testVariable = new Double(doubleVal);
}
//etc.
}
もちろん、これは Java が必要に応じてコンパイル時にどの型であるかを「把握」するわけではありません (Test
オブジェクトが作成されたときに、実行時に割り当てが行われます)。別。
testVariable
また、当然のことながら、初期化時にの値を適切に設定することもできます。
または、入力を (システム変数から読み取って) として受け取りString
、プリミティブ型の適切なラッパー クラスで返す次のようなメソッドを作成することもできます。
public Object getPrimitiveValue(String type, String value) {
if(type.equals("int")) {
return new Integer(Integer.parseInt(value));
}
else if(type.equals("double")) {
return new Double(Double.parseDouble(value));
}
//etc.
}