2012-03-08 1 views
0

그래서 내가 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); 
} 

(여기의 일부입니다) 그리고 나는 어떻게 객체 t의의 모든 던져 않는 ASTreeNode에게

public class Operation extends ASTreeNode 
{ 
    char s; 

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

를 확장하는 다른 클래스 동작을 (또한 ASTreeNode를 확장) 자식을 슈퍼 ctor 인수로 확장합니까? 그런 식으로 하드 코딩하지 않고? super (t, t.children)을 시도했지만 생성자가 인수에서 List를 취하지 않고 ASTreeNodes 만 만 가져옵니다.

오,이 클래스 용어

public class Term extends ASTreeNode 
{ 
    char symbol; 

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

되고 다른 노드

답변

1

에 자녀를 보낼 무리 더 클래스는 인수의 같은 목록의 동의를 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); 
    } 
} 
+0

클래스 ASTreeNode를 편집하지 않고도 가능합니까? 우리는 그 파일을 바꾸지 않아야합니다. (숙제로 우리에게 도움을주었습니다.) 내가 그것을 바꿀 수 있는지 다시 한번 확인해 주셔서 감사합니다. – user1256783

+0

'ASTreeNode'에 대한 모든 아이들을 얻을 수있는 방법이 있습니까? –

+0

Ooops 나는 이것을 실제로 할 필요가 없다는 것을 알아 냈습니다. 나는 생각한다. 트리는 이미 생성되었으므로 root = root.getChild (0) 또는 root.getChild (1)과 같은 작업을 수행하여 트리를 거쳐 재귀 또는 루프에 저장해야합니다. 늦게 응답 해 주셔서 감사 드리며 도움을 주셔서 감사합니다. – user1256783