I'd never heard about wildcars until a few days ago and after reading my teacher's Java book, I'm still not sure about what's it for and why would I need to use it.
Let's say I have a super class Animal
and few sub classes like Dog
, Cat
, Parrot
, etc... Now I need to have a list of animals, my first thought would be something like:
List<Animal> listAnimals
Instead, my colleagues are recommending something like:
List<? extends Animal> listAnimals
Why should I use wildcards instead of simple generics?
Let's say I need to have a get/set method, should I use the former or the later? How are they so different?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…