(TL; DR:跳到粗体。)

我正在尝试为修改后的九头蛇建立计算机模拟-在此版本中,每个九头蛇头都可以有更多的九头蛇头出来。我认为这非常类似于节点,因此我首先构建了一个通用的Node类。每个Node对象都有一个ArrayList<Node> children,每个(也都是Node)都可以有自己的children

九头蛇的头的结构与Node相同,但行为不同。 (例如,一个Node应该能够简单地删除其子对象,而从九头蛇中删除一个头部也需要重新生成一些头部。)因此,我构建了HydraNode extends Node并添加了cutHead()这样的方法(该方法删除了一个头部(节点),然后添加克隆)。 Hydra是“身体”,头部带有HydraNode

问题是,因为所有子节点都存储为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方法。

在对象包含自身的情况下,如何为类添加功能?将对象扩展为subclass似乎不起作用,因为检索包含的对象将返回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.
}


Hydra.java

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构造HydraNode时,您正在调用super()构造函数将其链接。这意味着您最终得到:

HydraNode -> Node -> Node

您应该打电话给:

    this(hierarchyLevel, nodesPerLevel);


因此,创建链始终会产生更多的HydraNode。

HydraNode -> HydraNode -> HydraNode

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

关于java - 试图通过在子类中扩展方法来向类添加方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46799728/

10-13 06:54