Typ generyczny Java usuwa parametry metody
Dostałem to od gadżetu Google I / O puzzler Joshua Bloch. Oto kod
public class Glommer<T> {
String glom(Collection<?> obj){
String result = "";
for(Object o : obj){
result += o;
}
return result;
}
int glom(List<Integer> ints){
int result = 0;
for(int i : ints){
result += i;
}
return result;
}
public static void main(String args[]){
List<String> strings = Arrays.asList("1", "2", "3");
System.out.println(new Glommer().glom(strings));
}
ta główna metoda zgłasza wyjątek, ponieważnew Glommer
jest typem surowym i stąd wszystkie leki generyczneGlommer
jest usuwane, więc kończy się dzwonieniemint glom(List<Integer> ints)
zamiastString glom(Collection<?> obj)
.
Moje pytanie brzmi, nawet gdybym zadzwoniłglom()
tak jaknew Glommer<Integer>().glom(strings)
nie powinien to nazywaćint glom(List<Integer> ints)
metoda, ponieważ ze względu na wymazywanie typu metoda ta jest skutecznaint glom(List ints)
istrings
jest typuList
nieCollection
?