简体   繁体   English

对链接列表使用私有类的正确方法

[英]Proper way to use private class for a linked list

I'm studying data structure. 我正在研究数据结构。 While studying a linked list, I wonder if this is good practice or not. 在研究链表时,我想知道这是否是一种好习惯。 Here is my code: 这是我的代码:

class LinkedListStack {
    // <-- Node
    private class Node { // I made a private class!
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
        }

        public int getData() {
            return data;
        }
        public void setData(int data) {
            this.data = data;
        }
        public Node getNext() {
            return next;
        }
        public void setNext(Node next) {
            this.next = next;
        }
    }
    // Node -->
    // <-- Logic

    private Node headNode = null;

    public int top() {
        if(headNode == null) {
            System.out.println("Empty stack");
            return 0;
        }
        else {
            return headNode.getData();
        }
    }
 ...

Actually, I usually make the node class outside of the class LinkedListStack - but is it okay to make private class Node ? 实际上,我通常将节点类制作在LinkedListStack类之外-但是可以制作私有类Node吗? The problem I have found is that in the method top() , I can access the variable data and next directly . 我发现的问题是,在方法top()中 ,我可以直接访问变量数据然后直接访问next Is there any way to prevent from accessing the local variables (data, next) of the class Node in the class LinkedListStack? 有什么方法可以防止访问LinkedListStack类中Node类的局部变量(数据,下一个)? Also, I'd like to know 'real' examples of a private class. 另外,我想知道私人课程的“真实”例子。 When do actually people use a private class in the real world? 人们实际上什么时候在现实世界中使用私人课程? And why do we need it? 为什么我们需要它?

The problem I have found is that in the method top() , I can access the variable data and next directly. 我发现的问题是,在方法top() ,我可以直接访问变量数据,然后直接访问。

It's not a problem, it's a feature. 这不是问题,这是一个功能。 Java lets your outer class access private methods and fields of its inner class, even when the class and all its fields are private . Java允许您的外部类访问其内部类的私有方法和字段,即使该类及其所有字段均为private

The rationale for that is that a nested class is part of your own class implementation, so both classes have access to each others' private members. 这样做的理由是,嵌套类是您自己的类实现的一部分,因此两个类都可以访问彼此的私有成员。

I usually make the node class outside of the class LinkedListStack 我通常将节点类制作在LinkedListStack类之外

When a nested class looks like a class that can be defined outside your class, it's a good indication that the class should be made static, like this: 当嵌套类看起来像可以在类外部定义的类时,这很好地表明该类应该被设置为静态,如下所示:

private static class Node {
    ...
}

This is because non-static nested classes have implicit references to an object of their outer class - LinkedListStack in your case, which the Node class does not need. 这是因为非静态嵌套类对它们的外部类的对象(在您的情况下为LinkedListStack具有隐式引用,而Node类则不需要。

If you implement it with an inner class, there is no way to prevent the parent class to see the private fields of it. 如果使用内部类实现它,则无法阻止父类查看其私有字段。 Fortunately, it's no use doing so. 幸运的是,这样做是没有用的。 An inner class is perfectly fine here, see for example the implementation of java.util.HashMap that uses (in JDK8 at least) an inner class called Node used to represent linked lists. 内部类在这里非常好,例如,参见java.util.HashMap的实现,该实现使用(至少在JDK8中)使用一个称为Node的内部类来表示链接列表。

Actually, a Node alone would have no meaning, that's why it is better practice to hide it as a private inner class. 实际上,仅Node就没有任何意义,这就是为什么最好将其隐藏为私有内部类。

From outer class you can access private variable of inner class.Nothing much you can do there. 在外部类中,您可以访问内部类的私有变量。

Private inner class is used when we can be sure from design perspective that the private inner class will not be used outside the enclosing class. 当从设计角度可以确定私有内部类不会在封闭类外部使用时,将使用私有内部类。 For LinkedList Node doesn't seem correct as private because in some other code you would like to reference Node Object in the LinkedList. 对于LinkedList节点,私有似乎并不正确,因为在某些其他代码中,您想引用LinkedList中的Node对象。

As @Dici has suggested in comments that it can be used as private. 正如@Dici在评论中建议的那样,它可以用作私有。 Only if you next() method return the data in the Node and not the Node itself. 仅当next()方法返回Node中的数据而不返回Node本身时。 You can see this private Node class in LinkedList JDK implementation . 您可以在LinkedList JDK实现中看到此私有Node类。 However if you wish to manipulate Node object outside of the enclosing class it should not be private. 但是,如果您希望在封闭类之外操作Node对象,则它不应是私有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM