Generic extend can't add the same type

前端 未结 3 2033
甜味超标
甜味超标 2021-01-25 11:45

I\'m new to these generic types. In the below code, I created a method that accepts a List of items that extends \"String\".

My Question? - When the list can be assigned

3条回答
  •  没有蜡笔的小新
    2021-01-25 12:27

    There's a subtle difference. It takes a list that contains one type of thing (a thing that extends string). This list may be a subclass of String and therefore not be a String iyswim. See http://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html Upper bounded wildcards.

    If it was

         public void takeList(List list){
    

    Then you could add strings to it, because the list is guaranteed to be able to accept Strings.

提交回复
热议问题