(TL;DR:跳到粗体。)
我试图建立一个修改过的九头蛇的计算机模拟——在这个版本中,每个九头蛇头可以有更多的九头蛇头从中出来。我觉得这很像节点,所以我首先构建了一个通用Node
类。每个Node
对象都有一个ArrayList<Node>
of children
,每个对象(也是一个Node
)都可以有自己的children
。
Hydra 的头部结构与 相同Node
,但行为不同。(例如。ANode
应该能够简单地移除它的孩子,而从 Hydra 中移除一个头还需要重新长出一些头。)所以我构建HydraNode extends Node
并添加了类似的方法cutHead()
(它移除了一个头(节点),然后添加了克隆)。AHydra
是“身体”,有HydraNode
s 代表头部。
问题是,因为所有的子节点都存储为一个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] 删除。
我来说两句