Java Generics Puzzler, extending a class and using wildcards

前端 未结 6 1799
不知归路
不知归路 2021-01-31 02:02

I\'ve been beating my head against this one for awhile and thought that maybe some fresh eyes will see the issue; thanks for your time.

import java.util.*;

clas         


        
6条回答
  •  日久生厌
    2021-01-31 02:45

    you cannot add any objects to TbinList ,it is not guaranteed what objects you are inserting into the list. It is supposed to read data from test2 when you use wildcard extends

    If you declared as TbinList which means you it is any subclass of the class Base or class Base itself, and when you initialize it you use diamond other than concrete class name, it makes your test2 not obvious which makes it harder to tell what objects can be inserted. My suggestion is that avoid such declaration it is dangerous, it may not have compile errors but it is horrible code, you might add something, but you also might add the WRONG thing which will break your code.

提交回复
热议问题