Generics cast issue

后端 未结 6 1164
悲哀的现实
悲哀的现实 2021-01-01 17:00

Here\'s my issue: given these classes

class A {}
class B extends A {}

This code compiles:

    List

        
6条回答
  •  耶瑟儿~
    2021-01-01 18:00

    Just to add, we can rewrite the code as below

    List> asListA = Arrays.asList(B.class, A.class);
    List> asListB = Arrays.asList(B.class);
    
    List> numbers = asListA;
    List> numbers2 = asListB; // it will fail here
    

    And @Inerdia explained the details already.

提交回复
热议问题