我正在尝试创建一个使用?值的函数。用于变量类型的类型。我该怎么写?

interface MyInterface<TYPE extends Collection> {
    TYPE getResult();
    void useResult( TYPE inResult );
}

class SomeOtherClass {
    static void moveObject( MyInterface<?> inObj ) {
        //I'm using the wrong syntax on the next line, but I'm not sure
        // what I should use here.
        <?> result =  inObj.getResult();
        inObj.useResult(result);
    }
}

最佳答案

<T>static之间添加void

import java.util.List;

interface MyInterface<T extends List<Integer>> {
    T getResult();

    void useResult(T inResult);
}

class SomeOtherClass {
    static <T extends List<Integer>> void moveObject(MyInterface<T> inObj) {
        T result = inObj.getResult();
        inObj.useResult(result);
    }
}

10-06 06:53