所以我有一个AbstractSyntaxTreeNode.java类(这是其中的一部分)

public abstract class ASTreeNode implements Iterable<ASTreeNode>{

protected List<ASTreeNode> children;

protected ASTreeNode(){
  children = new LinkedList<ASTreeNode>();
}

protected ASTreeNode(ASTreeNode... children){
  this();
  for(ASTreeNode c: children)
{
  this.addChild(c);
}


然后我有另一个扩展ASTreeNode的类Operation

public class Operation extends ASTreeNode
{
  char s;

  private Operation(Term t)
  {
  //QUESTION REGARDING THIS PART
    super(t, t.getChild(0), t.getChild(1));
  }
}


如何将所有对象t的所有子对象(还扩展了ASTreeNode)放入超级参数中?没有这样的硬编码?我尝试了super(t,t.children),但构造函数的参数中没有List,只有ASTreeNodes
被采取。

哦,这是课堂术语

public class Term extends ASTreeNode
{
  char symbol;

private Term(Factor f)
{
  super(f, f.getChild(0));
}
}


还有很多类将他们的孩子送入另一个节点

最佳答案

在ASTreeNode中添加一个接受构造器作为其参数的构造函数。

public abstract class ASTreeNode ... {

    public ASTreeNode(List<? extends ASTreeNode> children) {
        this.children = children;
    }
}

public class Operation extends ASTreeNode {
    char s;

    private Operation(Term t) {
        super(t.getChildren());
        this.addChild(t);
    }
}

07-24 09:25