试图通过在子类中扩展类来向类添加方法?

亚历克斯·G

(TL;DR:跳到粗体。)

我试图建立一个修改过的九头蛇的计算机模拟——在这个版本中,每个九头蛇头可以有更多的九头蛇头从中出来。我觉得这很像节点,所以我首先构建了一个通用Node类。每个Node对象都有一个ArrayList<Node>of children每个对象(也是一个Node)都可以有自己的children

Hydra 的头部结构与 相同Node,但行为不同。(例如。ANode应该能够简单地移除它的孩子,而从 Hydra 中移除一个头还需要重新长出一些头。)所以我构建HydraNode extends Node并添加了类似的方法cutHead()(它移除了一个头(节点),然后添加了克隆)。AHydra是“身体”,有HydraNodes 代表头部。

问题是,因为所有的子节点都存储为一个ArrayList<Node>,我可以做

Hydra hydra = new Hydra(1,1,1) // Makes a Hydra with 3 levels (1 head, which has 1 head, which has 1 head).
Node nodeAtLevel1 = hydra.getChildren.get(0); // Has to be declared as Node, not HydraNode, because getChildren() is a Node method, and returns an ArrayList<Node> of children.

,但它的每个子节点实际上都是节点。这导致了 中的问题main(),我尝试运行nodeAtLevel1.cutHead()但不能,因为cutHead()是一种HydraNode方法。

在对象包含自身的情况下,如何向类添加功能?将对象扩展到 asubclass似乎不起作用,因为检索包含的对象将返回类型为 的对象superclass,而不是subclass我不能把它向下抛。我能做什么?

节点.java

public class Node {
    // Member variables
    private ArrayList<Node> children = new ArrayList<>(); // Holds "children" nodes.
    private int hierarchyLevel; // Where in the hierarchy is it?
    private int childCount = 0; //How many "child" nodes does this node have?

    // Constructors
    public Node(int hierarchyLevel) {this.hierarchyLevel = hierarchyLevel}
    public Node(int hierarchyLevel, int... nodesPerLevel) {this(hierarchyLevel;} //Adds children to this node, eg. {1,2,1} adds 1 child node at lvl 1, 2 children at lvl 2, each with 1 child of their own at level 3.

    // Methods
    public ArrayList<Node> getChildren() {return children;}
    public void addChild() {} // Adds a child directly to this node
    public void removeChild(int i) {}

    public Node getCopy() {} //Returns a clone of this Node and all its child nodes.

    public String toString() {} // Eg. Node at Level ___ has ____ children.

}

HydraNode.java(头部)

public class HydraNode extends Node {
    // Constructors
    public HydraNode(int hierarchyLevel) { // Just call super, bc this is essentially a Node structure that just acts a little differently.
        super(hierarchyLevel);
    }
    public HydraNode(int hierarchyLevel, int... nodesPerLevel) {
        super(hierarchyLevel, nodesPerLevel);

    // Methods
    public void cutHead() {} // Cutting a Level 1 head, which is attached to the body (level 0), does not regrow. However, any other cut will multiply that branch's parent x3.
}

九头蛇

public class Hydra {
    // MAIN method
    public static void main(String[] args) {
        Hydra hydra = new Hydra(1,1,1);
        Node head2 = hydra.body.getChildren().get(0).getChildren().get(0);
        System.out.println(head2.toString()); // >> Node at Level 2 has 1 child node.
        //head2.cutHead(); // Doesn't work because cutHead() is a HydraNode method, not a Node method.
    }

    // Member Variables
    public static int regrowFactor = 2; // Every time a head is cut off, the hydra clones the remaining branches. In the original video, the hydra forms two new clones.
    HydraNode body;

    // Constructors
    public Hydra() {
        body = new HydraNode(0); // the body is just a new Node at level 0
    }
    public Hydra(int... headsPerLevel) {
        body = new HydraNode(0, headsPerLevel);
    }
}
迪利乌斯

我们提出的演员建议都失败了,因为:

public HydraNode(int hierarchyLevel, int... nodesPerLevel) {
    super(hierarchyLevel, nodesPerLevel);

当您从 Hydra 构造 HydraNodes 时,您正在调用super()构造函数来链接它。这意味着你最终得到:

HydraNode -> Node -> Node

你应该打电话给:

    this(hierarchyLevel, nodesPerLevel);

这样创建链总是会产生更多的 HydraNode。

HydraNode -> HydraNode -> HydraNode

然后,您将能够从 Node -> HydraNode 进行转换并cutHead按照许多响应中的指定进行调用

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何让子类扩展python中超类的方法

来自分类Dev

jQuery通过单击其他div来向div添加和删除类

来自分类Dev

从子类调用基类的扩展方法

来自分类Dev

Swift中的类扩展与子类化?

来自分类Dev

扩展超类,在Python中调用子类

来自分类Dev

无法通过子类访问父类方法

来自分类Dev

C#通过添加属性扩展类

来自分类Dev

如何通过父类指针使用未在父类中定义的子类方法?

来自分类Dev

通过更改类来向下滑动动画

来自分类Dev

向XText中的现有类添加扩展方法

来自分类Dev

通过超类实例调用子类类的方法

来自分类Dev

修改扩展类中的方法

来自分类Dev

创建子类vc扩展类

来自分类Dev

C ++子类扩展了父类

来自分类Dev

使用类中的扩展方法进行扩展

来自分类Dev

使用类中的扩展方法进行扩展

来自分类Dev

具有基类和子类的扩展方法

来自分类Dev

在调用方法时获取扩展超类的子类实例

来自分类Dev

在调用方法时获取扩展超类的子类实例

来自分类Dev

如何为许多类添加扩展方法?

来自分类Dev

Ruby-类方法中的引用子类

来自分类Dev

Ruby-类方法中的引用子类

来自分类Dev

如何访问子类中的父类方法

来自分类Dev

在子类中从父类调用方法

来自分类Dev

我想调用父类方法,该方法在Python中通过子类对象在子类中被覆盖

来自分类Dev

当我在子类中添加方法时,自动填充类中的列表

来自分类Dev

当我在子类中添加方法时,自动填充类中的列表

来自分类Dev

在Java中,允许子类通过公共setter方法更改超类私有字段是否正确?

来自分类Dev

如何在Java中通过超类实例调用子类方法?