Java Generics Puzzler, extending a class and using wildcards

前端 未结 6 1857
不知归路
不知归路 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条回答
  •  萌比男神i
    2021-01-31 02:28

    OK, here's the answer:

    import java.util.*;
    
    class Tbin extends ArrayList {}
    class TbinList extends ArrayList> {}
    
    class Base {}
    class Derived extends Base {}
    
    public class Test {
      public static void main(String[] args) {
    
        TbinList test3 = new TbinList<>();
        test3.add(new Tbin());
    
      }
    }
    

    As I expected, kind of obvious once I saw it. But a lot of thrashing around to get here. Java generics seem simple if you only look at working code.

    Thanks, everyone, for being a sounding board.

提交回复
热议问题