Generic type in inner class

拟墨画扇 提交于 2021-02-05 10:46:06

问题


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

private Node<T>

as opposed to

private Node

or does it not make any difference?


回答1:


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;
}


来源:https://stackoverflow.com/questions/42817415/generic-type-in-inner-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!