0

好吧,是的,标题有点混乱。但这是我想要完成的:

我想返回一个包含C类型元素的列表。我希望该方法接收R类型的变量。C必须是实现接口的类,即C_interface,R必须是实现另一个接口的类,即R_interface。

在我的世界里,这个方法头应该可以工作:

public <C implements C_interface, R implements R_interface> List<C> method_name(R r)

但事实并非如此。我在 Eclipse 中收到以下错误:

Multiple markers at this line
    - Syntax error on token "implements", , 
     expected
    - R cannot be resolved to a type
    - Syntax error on token "implements", , 
     expected
    - C cannot be resolved to a type

如果我删除 implements 接口部分,如下所示:

public <C, R> List<C> method_name(R r)

一切正常。而且我想我可以检查方法中的类型。但是如果第一种方法是可能的,那就更好了。

4

1 回答 1

1

您应该使用extends而不是工具。这有效:

public class DoubleParamGeneric {

    public <C extends CInterface, R extends RInterface> List<C>  m(R r) {

        List<C> result = null; 

        // Process here

        return result;

    }
}

public interface CInterface {

}

public interface RInterface {

}
于 2015-04-27T16:00:38.080 回答