java: Use of Raw type as method parameter errases all parametrized type information in parameter members

后端 未结 4 1138
孤街浪徒
孤街浪徒 2021-01-13 11:47

Please explain me why if I use the raw type A in the method test() , the get() method on my typed list returns an Object and not a B.:

public class test
{
           


        
4条回答
  •  误落风尘
    2021-01-13 12:04

    When you decalare your argument pa in the method test, no parameter is included, (including wildcard ). Therefore the list is held in a variable which contains a list of objects, so when you try to extract an element from the list you get a object, so you would have to cast it back to the required type, in this case B.

    When using a wildcard the compliler is told not promote the list to a varibale containing an list holding Object. It is told that the contents are of the list are of 'unknown type' and is to be left alone. It is then upto the programmer to ensure that when extracting an element it is assigned to a suitable varibale, without using a cast.

提交回复
热议问题