从子类调用时,为什么超级类别的字段有所不同?

发布于 2025-01-30 01:40:01 字数 1078 浏览 3 评论 0原文

为什么从子类调用时返回不同大小的字段节点

不应该子类访问其超级类字段?

这是可重现的示例:

manager.java

import java.util.HashMap;

public class Manager {
    protected HashMap<Integer,Node> nodes = new HashMap<>();

    public HashMap<Integer, Node> getNodes() { return nodes; }

    public void addNode(Node node){
        nodes.put(nodes.size(),node);
    }

    public static void main(String[] args) {
        Manager manager = new Manager();
        manager.addNode(new Node());
        System.out.println("(from Manager) manager.nodes.size() = " + manager.nodes.size());
        manager.getNodes().get(0).printNodesSize();
    }
}

node.java

public class Node extends Manager {
    public void printNodesSize() {
        System.out.println("(from Node) manager.nodes.size() = " + super.nodes.size());
    }
}

system.out:

(from Manager) manager.nodes.size() = 1
(from instanced Node) manager.nodes.size() = 0

Process finished with exit code 0

Why is field nodes returning different size when called from subclass

shouldn't subclasses have access to their super classes fields?

Here is reproducible example:

Manager.java

import java.util.HashMap;

public class Manager {
    protected HashMap<Integer,Node> nodes = new HashMap<>();

    public HashMap<Integer, Node> getNodes() { return nodes; }

    public void addNode(Node node){
        nodes.put(nodes.size(),node);
    }

    public static void main(String[] args) {
        Manager manager = new Manager();
        manager.addNode(new Node());
        System.out.println("(from Manager) manager.nodes.size() = " + manager.nodes.size());
        manager.getNodes().get(0).printNodesSize();
    }
}

Node.java

public class Node extends Manager {
    public void printNodesSize() {
        System.out.println("(from Node) manager.nodes.size() = " + super.nodes.size());
    }
}

System.out:

(from Manager) manager.nodes.size() = 1
(from instanced Node) manager.nodes.size() = 0

Process finished with exit code 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2025-02-06 01:40:01

您所遇到的问题对继承一无所知。呼叫:

manager.getNodes()

将返回map&lt; integer,node&gt;(在类Manager中检查您自己的方法定义),该定义仅包含一个带有index 1 (因为是您将密钥定义为nodes.size()+1)。

因此,当您调用manager.getNodes()。get(0)时,您会得到null element(该地图不包含任何元素0 < /代码>)。

如果.get(0) .get(1)将起作用。

旁注:您的设计不是很干净。类Manager(parent)包含节点 s的映射,该图是其自己的孩子。孩子(node)应该知道其父级(Manager),但反之亦然。

编辑

super.nodes.size()的打印为0,因为您没有指向Manager您认为自己'的实例重新指出。

当您进行new Node()(您添加到Manager)的节点时,您正在创建一个new Manager()默认情况下一个空的地图。

因此,当您调用super.nodes.size()时,您的super不是Manager,而是的另一个实例Manager您在执行new Node()时创建的,您从未添加任何内容。

同样,我建议您在上面检查我的旁注并重新考虑您的设计。

The issue you're having has nothing to see with inheritance. The call:

manager.getNodes()

will return a Map<Integer, Node> (check your own method definition in the class Manager) that contains only one element with index 1 (because it's you defining the key as nodes.size()+1).

Hence when you call manager.getNodes().get(0), you get a null element (the map doesn't contain any element with key 0).

If you replace .get(0) by .get(1), it will work.

Side note: your design is not very clean. The class Manager (parent) contains a map of Nodes which is its own child. The child (Node) is supposed to know its parent (Manager), but not vice versa.

Edit

The print of super.nodes.size() is 0 because you're not pointing to the instance of Manager that you think you're pointing to.

When you do new Node() (the node that you add to your manager), you're creating a new Manager() which has an empty map by default.

So when you call super.nodes.size(), your super is not manager as you think, but the other instance of Manager that you created when doing new Node(), to which you never added anything.

Again, I suggest you check my side-note above and re-consider your design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文