gpt4 book ai didi

java - 编译器是否能从另一个通用参数列表中找出其中一个?

转载 作者:行者123 更新时间:2023-12-01 17:47:30 25 4
gpt4 key购买 nike

摘自《Thinking in Java》一书:

One of the complaints about generics is that it adds even more text to your code. Consider this :

Map<Person, List<? extends Pet>> petPeople = 
new HashMap<Person, List<? extends Pet>>();

It appears that you are repeating yourself, and that the compiler should figure out one of the generic argument lists from the other. Alas, it cannot, ...

这是否意味着双方的显式类型规范都是由编译器强制执行的?

相反,它似乎对我来说工作得很好(没有明确):

Map<Person, List<? extends Pet>> petPeople = new HashMap();

最佳答案

你读的一本古书副本,自从 Java-7 和添加了菱形运算符以来,它工作得很好:

Map<Person, List<? extends Pet>> petPeople = new HashMap<>();

关于java - 编译器是否能从另一个通用参数列表中找出其中一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53470479/

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