gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:01:35 24 4
gpt4 key购买 nike

(TL;DR:跳至粗体。)

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

虽然九头蛇头部的结构与 Node 相同,行为不同。 (例如,A Node 应该能够简单地删除它的 child ,而从 Hydra 中删除头部还需要重新长出一些头。)所以我构建了 HydraNode extends Node并添加了类似 cutHead() 的方法(删除头(节点),但随后添加克隆)。一个Hydra是“ body ”并且有 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方法。

如果对象包含自身,如何向类添加功能?将对象扩展为 subclass似乎不起作用,因为检索包含的对象将返回 superclass 类型的对象,不是subclass 。而且我无法将其向下转换。我能做什么?

Node.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 -> 节点 -> 节点

您应该调用:

    this(hierarchyLevel, nodesPerLevel);

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

HydraNode -> HydraNode -> HydraNode

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com