为什么我不能使这些函数成为静态的?

问题描述

为什么我不能只制作 inorder 并插入静态并在主函数中传递对象的根? 而不是创建具有 void 返回类型的相同 naem 的临时函数,然后在主函数调用它们

public TreeNode insert(TreeNode root,int value){ - make this static
        if (root == null){
            root = new TreeNode(value);
            return root;
        }
        if (value<root.data){
            root.left = insert(root.left,value);
        }else {
            root.right = insert(root.right,value);
        }
        return root;
    }
    public void insert(int value){ i am asking if i can eleminate this function
        root = insert(root,value);
    }
    public void inorder(){ - and this function 
        inorder(root);
    }
    public void inorder(TreeNode temp){ - make this static
        if (temp == null){
            return;
        }
        inorder(temp.left);
        System.out.print(temp.data);
        inorder(temp.right);
    }

在主函数中,我将直接调用 insert(object.root,) 和 inorder(object.root) 我这样做了,但没有显示任何输出

解决方法

你可以,但是你应该调用 object.root = insert(object.root,...) 函数 insert(int value) 修改对象根