编译器是否从另一个中找出一个通用参数列表?
Does compiler figure out one of the generic argument list from the other?
摘自 "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<>();
摘自 "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<>();