2017-09-24 90 views
1

class Tree我得到錯誤消息:調用由實例名實例方法爲字符串在Java

方法removeparent()是未定義String類型。

我想轉換字符串「Grandchild3」反對哪個實例MyTreeNode類的話,我可以用removep("Grandchild3")通話這樣Grandchild3.removeparent()的方法。
我該怎麼做?

這裏的類MyTreeNode:

public class MyTreeNode<T>{ 
     private T data = null; 
     private List<MyTreeNode> children = new ArrayList<>(); 
     private MyTreeNode parent = null; 

     public MyTreeNode(T data) { 
      this.data = data; 
     } 

     public void addChild(MyTreeNode child) { 
      child.setParent(this); 
      this.children.add(child); 
     } 

     public void addChild(T data) { 
      MyTreeNode<T> newChild = new MyTreeNode<>(data); 
      newChild.setParent(this); 
      children.add(newChild); 
     } 

     public void addChildren(List<MyTreeNode> children) { 
      for(MyTreeNode t : children) { 
       t.setParent(this); 
      } 
      this.children.addAll(children); 
     } 

     public List<MyTreeNode> getChildren() { 
      return children; 
     } 

     public T getData() { 
      return data; 
     } 

     public void setData(T data) { 
      this.data = data; 
     } 

     private void setParent(MyTreeNode parent) { 
      this.parent = parent; 
     } 

     public MyTreeNode getParent() { 
      return parent; 
     } 

     public void removeparent() { 
      this.parent = null; 
     } 
     public void removeChild(MyTreeNode<T> child) 
     { 
      this.children.remove(child); 
     } 

    } 

這裏的類樹:

public class Tree { 

    public static void main(String[] args) throws ClassNotFoundException { 
     // TODO Auto-generated method stub 
     MyTreeNode<String> root = new MyTreeNode<>("Root"); 

     MyTreeNode<String> child1 = new MyTreeNode<>("Child1"); 
     child1.addChild("Grandchild1"); 
     child1.addChild("Grandchild2"); 

     MyTreeNode<String> child2 = new MyTreeNode<>("Child2"); 
     child2.addChild("Grandchild3"); 

     root.addChild(child1); 
     root.addChild(child2); 
     root.addChild("Child3"); 

     root.addChildren(Arrays.asList(
       new MyTreeNode<>("Child4"), 
       new MyTreeNode<>("Child5"), 
       new MyTreeNode<>("Child6") 
     )); 

     for(MyTreeNode<String> node : root.getChildren()) { 
      System.out.println(node.getData()); 
     } 

     printTree(root, " "); 

     removep("Grandchild3"); //error message"The method removeparent() is undefined for the type String" 

     printTree(root, " "); 

    } 

    private static void printTree(MyTreeNode<String> node, String appender) { 
     System.out.println(appender+node.getData()); 
     for (MyTreeNode each : node.getChildren()){ 
      printTree(each, appender + appender); 
     } 
    } 

    public static void removep(MyTreeNode<String> node) 
     { 
     node.getParent().removeChild(node); 
     node.removeparent(); 

     } 

} 
+0

'removep'有一個'MyTreeNode '類型的參數。你在'removep(「Grandchild3」);''行中傳遞一個字符串。 'String'與'MyTreeNode '不一樣。 –

+0

@TT是的,這是我的問題。如何弄清楚? – lvanna9786

+0

只是猜測:調用'removep(new MyTreeNode <>(「Grandchild3」));'? –

回答

0

所以基本上你想要的是一個字符串轉換爲一個類的對象。現在可能有幾種方法可以完成,但在這裏使用的上下文中,似乎可以簡單地傳遞適當的對象而不是用字符串搞亂。

所以在你的情況下,最合適的方法是創建一個方法,該方法接收一個字符串名稱和樹的頂部節點,然後遍歷該樹來查找具有給定名稱的節點。找到該方法時返回該節點。

然後,您可以使用該方法從名稱獲取節點,然後調用removep

+0

謝謝,那我試試吧! – lvanna9786