【发布时间】:2011-08-02 09:36:28
【问题描述】:
我正在尝试调用一个方法,该方法采用一个接口列表和一个枚举列表(实现接口)。这会产生以下编译错误:
The method method(List<Interface>) in the type Class is not applicable for the arguments (List<Enum>)
这是界面:
public interface Interface {
}
这是实现接口的枚举:
public enum Enum implements Interface {
}
这是调用类:
import java.util.ArrayList;
import java.util.List;
public class Class {
public static void method(List<Interface> list){
}
public static void main(String[] args) {
List <Enum> enumList = new ArrayList<Enum>();
method(enumList); //This line gives the compile error.
}
}
为什么会出现编译错误?在我看来,它应该可以工作,因为 Enum 实现了该接口。
【问题讨论】:
-
Generics in Java的可能重复
-
是的,这是重复的。我之前找的时候找不到那个。