gpt4 book ai didi

Java - 为什么将具体实现作为参数发送到通用接口(interface)不编译?

转载 作者:行者123 更新时间:2023-11-30 08:16:42 24 4
gpt4 key购买 nike

为什么 doSomething2()doSomething3() 不能在 Java 中编译?

(Toyota extends Car and Car implements ICar)

 public static void main(String[] args) {
Map<String, Toyota> toyotas = new HashMap<>();
doSomething0(toyotas);
doSomething1(toyotas);
doSomething2(toyotas);
doSomething3(toyotas);
}

private static void doSomething0(Map<String, ? extends Car> cars) {}
private static void doSomething1(Map<String, ? extends ICar> cars) {}
private static void doSomething2(Map<String, ICar> cars) {}
private static void doSomething3(Map<String, Car> cars) {}

最佳答案

因为:

public static void main(String[] args) {
Map<String, Toyota> toyotas = new HashMap<>();
doSomething4(toyotas);

Toyota t = toyotas.get("hello"); // boom
}

private static void doSomething4(Map<String, Car> cars) {
cars.put("Hello", new Honda());
}

关于Java - 为什么将具体实现作为参数发送到通用接口(interface)不编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28186926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com