Unboxing issues

前端 未结 2 1807
旧巷少年郎
旧巷少年郎 2021-01-06 18:20

I have a class that extends the LinkedList class. Here\'s an excerpt of the code:

class SortedList extends LinkedList {
      i         


        
2条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 19:04

    This is because you have after SortedList.

    Usually you use T for type parameters: class SortedList, but you used Integer instead. That is, you made Integer a type parameter (which shadows the java.lang.Integer).

    Your class, as it stands, is equivalent to

    class SortedList extends LinkedList {
          int intMethod(T integerObject){
              return integerObject;         // <--  "Cannot convert from T to int"
          }
    }
    

    Remove the type parameter and it works just fine:

    class SortedList extends LinkedList {
          int intMethod(Integer integerObject){
              return integerObject;
          }
    }
    

提交回复
热议问题