2017-09-24 109 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