为什么我不能使用实现接口的枚举列表调用方法?

发布于 2024-11-27 14:13:03 字数 762 浏览 2 评论 0原文

我正在尝试调用一个方法,该方法采用带有枚举列表(实现接口)的接口列表。这会给出以下编译错误:

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 实现了该接口。

I am trying to call a method which takes a list of Interface with a list of Enum (which implements Interface). This gives the following compile error:

The method method(List<Interface>) in the type Class is not applicable for the arguments (List<Enum>)

This is the interface:

public interface Interface {
}

This is the enum that implements the interface:

public enum Enum implements Interface {
}

This is the calling class:

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.
    }
}

Why is there a compile error? To me it seems that it should work because the Enum implements that interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥枳 2024-12-04 14:13:04

这是因为即使 Car 扩展 VehicleList扩展 List;。如果是这样,您可以执行以下操作:

List<Car> listOfCars = new ArrayList<Car>();
List<Vehicle> listOfVehicles = listOfCars;
listOfVehicles.add(new Bicycle());
// and now the list of cars contains a bicycle: not pretty.

This is because even is Car extends Vehicle, List<Car> does not extend List<Vehicle>. If it did, you could do the following:

List<Car> listOfCars = new ArrayList<Car>();
List<Vehicle> listOfVehicles = listOfCars;
listOfVehicles.add(new Bicycle());
// and now the list of cars contains a bicycle: not pretty.
写给空气的情书 2024-12-04 14:13:04

因为 List 不是 List

您应该将变量更改为 List,或者更改方法签名以采用 List

Because List<Enum> is-not-a List<Interface>.

You should either change the variable to List<Interface>, or change the method signature to take List<? extends Interface>

牛↙奶布丁 2024-12-04 14:13:04

虽然 Enum 实现了 Interface,但 List<枚举>不是 List < 的子类型界面>.
如果将方法签名修改为以下将起作用。

method(List<? extends Interface> list)

有关更多详细信息,请参阅文档
http://download.oracle.com/javase/tutorial/java/generics /通配符.html

Though the Enum implements the Interface, List< Enum > is not subtype of List < Interface >.
If you modify the method signature to following will work.

method(List<? extends Interface> list)

For further details go through the documentation
http://download.oracle.com/javase/tutorial/java/generics/wildcards.html

故笙诉离歌 2024-12-04 14:13:03
public static void method(List<? extends Interface> list){
}
public static void method(List<? extends Interface> list){
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文