如何在此框架中正确使用inheritence

时间:2018-03-01 20:13:43

标签: java inheritance

我有一个抽象类ChildNode

public abstract class ChildNode {
    public abstract boolean activate()
    public abstract void execute();
}

和一个抽象类ParrentNode

public abstract class ParentNode extends ChildNode {
    public ArrayList<ChildNode> nodes;
    public void execute(){
        for ( ChildNode node : nodes) {
            if (node.activate()) {
                node.execute();
                break;
            }
        }
    }
}

然后我会运行这个

ArrayList<ParentNode> masterNodeArray = null;
//add and create a bunch of nodes implementing different activate methods for both Child/Parent Nodes, and execute methods for child nodes
for (ParentNode node : masterNodeArray) {
        if (node.activate()) {
            node.execute();
            break;
        }
}

我的问题是,我希望父节点和子节点能够成为ParentNode - &gt;的一部分。 nodes数组。因此,当它遍历nodes中execute函数中的ParentNode数组时,它将处理数组中的父节点和子节点。因此,如果它到达ParentNode,它将作为ParentNode处理它,运行预定义的执行函数并知道它有一个nodes成员循环,如果它被执行,如果它到达ChildNode,它将运行定义的执行/激活方法。

2 个答案:

答案 0 :(得分:1)

您的问题是break execute ChildNodeParentNode个孩子列表中的public abstract class ChildNode { private boolean activated = true; public final boolean isActivated() { return activated; } public final void setActivated(boolean activated) { this.activated = activated; } public abstract void execute(); } public class ParentNode extends ChildNode { private List<ChildNode> children = new ArrayList<>(); @Override public final void execute() { for (ChildNode node : children) { if (node.isActivated()) { node.execute(); // DON'T BREAK HERE } } } public final List<ChildNode> getChildren() { return children; } } public class ConcreteChildNode extends ChildNode { private final String name; public ConcreteChildNode(String name) { this.name = name; } @Override public void execute() { System.out.println("ConcreteChildNode-" + name); } } public class Test { public static void main(String[] args) throws IOException { ParentNode parent1 = new ParentNode(); parent1.getChildren().add(new ConcreteChildNode("1")); parent1.getChildren().add(new ConcreteChildNode("2")); ParentNode parent2 = new ParentNode(); parent2.getChildren().add(new ConcreteChildNode("3-1")); parent2.getChildren().add(new ConcreteChildNode("3-2")); parent1.getChildren().add(parent2); parent1.getChildren().add(new ConcreteChildNode("4")); parent1.execute(); // prints // ConcreteChildNode-1 // ConcreteChildNode-2 // ConcreteChildNode-3-1 // ConcreteChildNode-3-2 // ConcreteChildNode-4 } } 循环 public void ResetAnchor() { WorldAnchor currentAnchor = objectToAnchor.GetComponent<WorldAnchor>(); if (currentAnchor != null) { DestroyImmediate(currentAnchor); } AnchorName = ""; CreateAnchor(); } >

let price = ItemDataSource.sharedInstance.items[indexPath.row].price!

答案 1 :(得分:0)

如果你正在制作一个机器人,它看起来像这样......就像这样做

public interface Action {
    boolean active()
    boolean execute();
}

public abstract class Controller implements Action {
    public List<Action> nodes;
    public void execute(){
        for (Action node : nodes) {
            if (node.activate()) {
                node.execute();
                break;
            }
        }
    }
}


public class Operator {
    public List<Controller> nodes;
    public void execute(){
        for (Controller node : nodes) {
            if (node.activate()) {
                node.execute();
                break;
            }
        }
    }
}