How do I get an inner class to inherit enclosing class' generic type?

后端 未结 5 1417
旧时难觅i
旧时难觅i 2020-12-14 01:44

I\'m using Java 6.

I\'m having trouble getting my inner class to use the same generic class as its enclosing class. Currently I have

public class          


        
5条回答
  •  旧巷少年郎
    2020-12-14 02:13

    I suspect what you want is something like:

    class Tree {
       Node head;
    
       static class Node {
          List> relatives = new ArrayList>();
          T value;
       }
    }
    

    Here, a tree's head node has the same T as the tree itself, and every relative node has the same T as the parent node, so all the nodes in the tree will have the same value type as the tree itself.

    I used an ArrayList here because arrays cannot have generic types.

提交回复
热议问题