Java lower bound wildcards

后端 未结 4 1604
孤街浪徒
孤街浪徒 2020-12-14 09:59

I\'m struggling to get my head around this and was wondering if someone could explain the reasons for this.

I have three classes:

class Angel {}
clas         


        
4条回答
  •  一整个雨季
    2020-12-14 10:44

    There is a severe understanding as I always did, List is introduced to ensure List and List can both be passed in as an argument which is the flexibility upper bounded wildcard provides.

    But within the method, in your case only Person or subtype of Person can be inserted into the list which ensures the list will always contain valid instances.

    E.g. if you are passing in a list of Person, then only Employee and Person can be inserted while Employee will be auto-type-casted to Person.

提交回复
热议问题