11

次のような状況があります。

次のようなクラスがあります。

public class TestClass<T> {
   // class body here...
}

そして、私は次のようなメソッドを持っています:

public class AnotherTestClass<K> {
     private TestClass<K> testClass;

     public AnotherTestClass(TestClass<K> testClass) {
         this.testClass = testClass;
     }

     public K testMethod() {
         //call methods on param object and pass a value of the same type as testClass.
         K returnVal = this.testClass.doSomething();
         return returnVal;
     }
}

今、私は型のオブジェクトを返すファクトリメソッドを持っていますTestClass<?>

public TestClass<?> sampleFactory(int i) {
       if( i==1 ) 
           return new TestClass<Integer>();
       if( i==2 ) 
           return new TestClass<Double>();
       if( i==3 ) 
           return new TestClass<String>();
}

しかし、そのメソッドを使用してパラメーターを my に渡すことはできませんtestMethod。これに対する解決策は何ですか?

現在if else、正しいインスタンスを取得するためにチェーン ブロックを作成しています。if else上記のような複数のパラメーターがある場合、ブロックを書き込むことは実際的ではないため、正しくないことはわかっています。

これのためのエレガントな方法を提案してください。

編集: 使用例:

package my;

import java.util.ArrayList;
import java.util.List;

public class GenericsSpike {
    public static void main( String[] args ) {
        TestClass1< ? > tc1 = new TestClass1<Integer>( 123 );
        TestClass2< ? > tc2 = new TestClass2<Integer>( 123 );
        AnotherTestClass< ? > atc = new AnotherTestClass<Integer>( tc1, tc2 );
        atc.testMethod();
    }
}

class TestClass1<T> {
    private T value;

    TestClass1( T val ) {
        value = val;
    }

    // class body here...

    public T getValue() {
        return value;
    }
}

class TestClass2<T> {
    private T value;

    TestClass2( T val ) {
        value = val;
    }

    // class body here...

    public T getValue() {
        return value;
    }
}

class AnotherTestClass<K> {
    public TestClass1<K> testClass1, testClass2;

    public AnotherTestClass( TestClass1<K> testClass, TestClass2<K> testClass2 ) {
        this.testClass1 = testClass;
    }

    public K testMethod() {
        //Any logic can come here.
        System.out.println( testClass1.getValue() );
        System.out.println( testClass2.getValue() );
        return testClass1.getValue();
    }
}

この場合、tc1tc2がこれらのオブジェクトを作成するファクトリから来ている場合、インスタンスを作成する適切な方法を知りたいAnotherClass

4

4 に答える 4

0

これを使用すると、パラメータをに渡すことができますtestMethod

package stackoverflow;

public class Func {

    static class TestClass<T> {
    }

    static class AnotherTestClass {

        public <K> TestClass<K> testMethod(TestClass<K> param) {
            return param;
        }
    }

    static class Factory {

        public static <E> TestClass<E> sampleFactory(int i) {
            if (i == 1)
                return (TestClass<E>) new TestClass<Integer>();
            if (i == 2)
                return (TestClass<E>) new TestClass<Double>();
            if (i == 3)
                return (TestClass<E>) new TestClass<String>();
            throw new IllegalArgumentException();
        }
    }

    public static void main(String[] args) {
        new AnotherTestClass().testMethod(Factory.sampleFactory(1));
    }

}
于 2013-08-10T14:57:04.170 に答える