Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中编写泛型函数时遇到问题_Java_Generics - Fatal编程技术网

在Java中编写泛型函数时遇到问题

在Java中编写泛型函数时遇到问题,java,generics,Java,Generics,我试图创建一个函数,它使用的值是?用于变量类型的类型。我该怎么写呢 interface MyInterface<TYPE extends Collection> { TYPE getResult(); void useResult( TYPE inResult ); } class SomeOtherClass { static void moveObject( MyInterface<?> inObj ) { //I'm usi

我试图创建一个函数,它使用的值是?用于变量类型的类型。我该怎么写呢

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);
    }
}
接口MyInterface{
键入getResult();
无效用户结果(输入结果);
}
给别人上课{
静态void moveObject(MyInterface inObj){
//我在下一行使用了错误的语法,但我不确定
//我应该在这里用什么。
result=inObj.getResult();
inObj.useResult(结果);
}
}
静态
无效
之间添加一个

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);
    }
}
import java.util.List;
接口MyInterface{
T getResult();
无效用户结果(T inResult);
}
给别人上课{
静态void moveObject(MyInterface inObj){
T result=inObj.getResult();
inObj.useResult(结果);
}
}

我认为以下方法应该有效(未经测试且内存不足):

class-SomeOtherClass{
静态void moveObject(MyInterface inObj){
T result=inObj.getResult();
inObj.useResult(结果);
}
}
像这样试试

static <T> void moveObject(MyInterface<T> inObj) {
    T result = inObj.getResult();
    ...
}
静态void移动对象(MyInterface inObj){
T result=inObj.getResult();
...
}
给其他人上课{
静态void moveObject(MyInterface inObj){
T result=inObj.getResult();
inObj.useResult(结果);
}
}

就像其他人所说的那样,它要记住的唯一方法是引入一个类型变量,即您得到的是要放回的正确类型。一旦一件东西变成了
,它就失去了对
的所有意义。(同一问题的一个常见示例是,如果您试图编写一个实用程序方法来交换列表中的两个元素。)


你能添加解释吗?你想让我解释什么?我知道不是返回类型,但对于像我这样从未需要超越基本泛型的人来说,这将帮助我理解它是什么:)
声明指定给定的方法是一个带一个类型参数的泛型方法,
T
,它被限制为
List
的子类。如果您根本不需要对
t
进行任何限制,您可以编写
。但是在大多数实际情况下,你需要对T有一些限制,以便能够用它做任何事情。
static <T> void moveObject(MyInterface<T> inObj) {
    T result = inObj.getResult();
    ...
}
class SomeOtherClass {
    static <T> void moveObject( MyInterface<T> inObj ) {
        T result =  inObj.getResult();
        inObj.useResult(result);
    }
}
class SomeOtherClass {
    static <T extends Collection> void moveObject( MyInterface<T> inObj ) {
        T result =  inObj.getResult();
        inObj.useResult(result);
    }
}
class SomeOtherClass {
    static void moveObject( MyInterface<?> inObj ) {
        moveObjectHelper(inObj);
    }
    private static <T extends Collection> void moveObjectHelper( MyInterface<T> inObj ) {
        T result =  inObj.getResult();
        inObj.useResult(result);
    }
}