Java Generics - >函数返回类型

LPD*_*LPD 11 java generics

我有这样的情况:

我有一个类看起来像:

public class TestClass<T> {
   // class body here...
}
Run Code Online (Sandbox Code Playgroud)

我有一个看起来像这样的方法:

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;
     }
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个返回类型对象的工厂方法 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>();
}
Run Code Online (Sandbox Code Playgroud)

但我不能使用该方法将参数传递给我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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,如果tc1tc2来自创建这些对象的工厂,我想知道什么是创建实例的正确方法AnotherClass

Dan*_*den 5

你的问题是这个方法:

public TestClass<?> sampleFactory(int i) {
Run Code Online (Sandbox Code Playgroud)

?通配符类型是指"某种类型的,但我不知道是什么." 所以你可以得到一个类型的值TestClass<?>,但它对你没用,因为你不能有意义地与类型交互?- 你不能创建类型的值?(除了null)你不能调用类型的方法?(方法除外java.lang.Object).

你真正想要的是:

public <T> TestClass<T> sampleFactory(TypeToken<T> typeToken) {
Run Code Online (Sandbox Code Playgroud)

也就是说,如果你希望你的工厂给你返回由不同类型参数化的值,你需要给它一些东西,告诉它你想要什么类型.不幸的是,int还不够 - 可能知道这i==1意味着类型Integer,但编译器不知道.

你对你的问题的描述对我来说有点太模糊,无法理解你真正想要达到的目标,但我的猜测是你真正需要的是超类型代币或类似番石榴的东西ClassToInstanceMap.


Ria*_*iaD 2

一种可能的解决方案是使用原始AnotherTestClass类型

public class A {
    public static void main(String[] args) {
        TestClass<?> tc = new TestClass<Integer>();
        AnotherTestClass atc = new AnotherTestClass();
        atc.testMethod(tc);
    }
}

class TestClass<T> {

// class body here...

}

class AnotherTestClass<K> {

    public void testMethod(TestClass<K> param) {
    }
}
Run Code Online (Sandbox Code Playgroud)

编译得很好。但在一般情况下使用原始类型并不是一个好主意