Generic type in inner class

后端 未结 1 550
暗喜
暗喜 2021-01-28 23:59

I have an outer class (LinkedStack) that has an inner node class. Is it necessary to declare the inner Node class with the same generic like



        
相关标签:
1条回答
  • 2021-01-29 00:50

    If the inner class is a static class then yes, otherwise no.

    I.e.:

    class LinkedStack<T> {
        // references to T refer to LinkedStack's T.
    
        static class Node<T> {
            // references to T refer to Node's T.
            T data;
        }
    
        // ...
        Node<T> node;
    }
    

    or:

    class LinkedStack<T> {
        // references to T refer to LinkedStack's T.
    
        class Node {
            // references to T refer to LinkedStack's T.
            T data;
        }
    
        // ...
        Node node;
    }
    
    0 讨论(0)
提交回复
热议问题