A generic class TypeA<TypeB>
is a different type from TypeA
. You can't pass in a parameter of type TypeA<TypeB>
where the method expects a TypeA
. Also TypeA<TypeB>
is a different type from TypeA<TypeC>
, so the same constraints apply.
The classic example (from Effective Java, 2nd Ed. AFAIR) is: we have containers for animals (Container<Animal>
) and as subclasses of Animal
we have Lion
and Butterfly
. Now, if you have a method
void func(Animal animal);
it will accept both lions and butterflies. However, this function
void func(Container<Animal> animalContainer);
will not accept a Container<Lion>
, neither a Container<Butterfly>
. Do realize that a strong cage useful for keeping lions safely would not stop butterflies from flying away, and vice versa a thick but light net to hold butterflies would not stand a chance against a lion.
If you really are sure that any kind of animal container suits you, declare your function like this:
void func(Container<? extends Animal> animalContainer);
Back to your case, I guess the only method to accept both List<TypeA>
and List<TypeA<TypeB>>
would be something like this:
void doSomething(List<?> list);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…