在java中将实例名称作为String调用实例方法

时间:2017-09-24 05:28:30

标签: java string type-conversion

class Tree我收到错误消息:

  

对于String类型,方法removeparent()未定义。

我想将字符串“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();

        }

}

1 个答案:

答案 0 :(得分:0)

所以基本上你想要的是将字符串转换为类对象。现在可能有几种方法可以完成,但在这里使用的上下文中,似乎你可以简单地传递适当的对象,而不是乱用字符串。

因此,在您的情况下,最合适的方法是创建一个方法,该方法接收字符串名称和树的顶部节点,并在树上迭代以查找具有给定名称的节点。找到后,该方法返回节点。

然后,您可以使用该方法从名称中获取节点,然后调用removep