【问题标题】:Java: non-static method insertValue(int,java.lang.String) cannot be referenced from a static contextJava:不能从静态上下文引用非静态方法 insertValue(int,java.lang.String)
【发布时间】:2020-11-26 18:30:14
【问题描述】:

我正在尝试实现一棵二叉树,更具体地说,我正在尝试编写在树中插入节点的方法。

一个节点由一个整数键(用于节点的位置)和一个String 值(节点内部的值)组成。

下面的代码是针对节点类的:

public class Node {
    private int key;
    private String value;
    private Node left;
    private Node right;
    public Node(int key, String value){
        this.key = key;
        this.value = value;
        this.left = null;
        this.right = null;
    }
    public void setLeftChild(Node child) {
        left = child;
    }
    public void setRightChild(Node child) {
        right = child;
    }
    public int getValue() { return key; }
    public Node getLeftChild() { return left; }
    public Node getRightChild() { return right; }
    public int getKey(){ return key; }
}

这是树类:

public class BinaryTree {
    private static Node root;
    public BinaryTree(){
        root = null;
    }
    public BinaryTree(int key, String string) {
        root = new Node(key,string);
    }
    public void insertValue(int key, String value) {
        insert(key, value, root);
    }
    public void insert(int key, String value, Node currentNode) {
        //base case
        if (currentNode == null) {
            currentNode = new Node(key, value);
        } else {
            if ( key <= currentNode.getKey())
                insert(key, value, currentNode.getLeftChild());
            else {
                if ( key > currentNode.getKey())
                    insert(key, value, currentNode.getRightChild());
            }
        }
    }
    public static void main(String[] args){
        BinaryTree alpha = new BinaryTree(5,"Bella");
        insertValue();
    }
}

我从 Java 收到以下错误:

错误:(32, 9) java: 不能从静态上下文引用非静态方法 insertValue(int,java.lang.String)

我不明白为什么。有人可以向我解释为什么它不起作用吗?想法正确吗?递归调用正确吗?

【问题讨论】:

  • 很明显——你在哪个对象上调用insertValue 方法?
  • 是的,对不起,无论如何,如果我插入 insertValue(3,"Alpha") 我有错误
  • 很抱歉,但这并不能回答我的问题。即使您认为可以使用insertValue(3, "Alpha") 之类的表达式调用该方法,但您不能——您想在哪个对象上调用该方法?
  • 抱歉,我读的问题太快了。我还没有调用对象的方法。是一个愚蠢的错误。

标签: java recursion binary-tree nodes non-static


【解决方案1】:

不要在private static Node root;这一行中将Node root声明为static,而是应该这样做:

private Node root; // remove the "static" keyword

你在main method又犯了一个错误

// insertValue(); // it is wrong
alpha.insertValue(); // call it like this

你会没事的......

为什么会发生这种情况

要了解为什么会发生这种情况,您必须了解staticjava 中的工作原理。使用static 声明的任何内容,可能是method/variable,在classall objects 之间是shared。这意味着,所有对象都只有一个变量实例,或者您可能会说所有对象将共享一个变量。现在,这就是说,没有non-static variable/method 可以是used/called 在声明为methodstaticstatic method。简单地说,您不能在 static 方法中使用 non-static variable/method。因为static method属于类作用域,this指针没有传入static method,所以不能使用non-static变量/方法。

如果你已经理解了静态的概念,那么现在让我解释一下你的代码存在的问题:

第一个问题 - 为什么static root 是错误的

因为,如果您将root 声明为static,那么您创建的所有Binary Tree 都会有only one root。但是,当然,你会期望不同的二叉树有不同的根。

第二个问题 - 为什么只是 insertValue(); 调用是错误的

您在main() method,请注意main()static。但是insertValue()not static。但是,您已经知道,您不能从 static 方法调用 non-static 方法。

希望这能清除您的理解。如果您还有其他问题,请告诉我...

【讨论】:

  • 调用 alpha.insertValue();不是插入值();在主要方法中。我还编辑了答案...
  • 虽然这是一个很好的答案,但请不要使用代码标记来强调,而是使用 bolditalic 来强调。否则,屏幕阅读器将很难阅读您的帖子。
【解决方案2】:

在main方法中尝试

alpha.insertValue(1,"Something");

【讨论】:

  • 哦,它有效!但我不明白为什么。我必须创建一个树的实例??
  • @Kalewi_98 你明白static 关键字的作用吗?您了解 class 和作为类的 instanceobject 之间的区别吗?如果你把所有这些基础知识放在一起,很明显会发生什么。
  • 感谢@Polygnome,可能我还没有完全理解静态的作用。我会再看一遍
猜你喜欢
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
相关资源
最近更新 更多