简体   繁体   English

无法对非静态方法进行静态引用

[英]cannot make a static reference to a non static method

So far I have the following code: 到目前为止,我有以下代码:

import java.util.Scanner;

public class HallLanceMemoryCalculator {
private double currentValue;

public static int displayMenu(){

    Scanner input=new Scanner(System.in);

    int choice=0;

    while(choice<1||choice>5){      
    System.out.println("1.Add");
    System.out.println("2.Subtract");
    System.out.println("3.Multiply");
    System.out.println("4.Divide");
    System.out.println("5.Clear");

    System.out.println("What would you like to do?");
    choice=input.nextInt();
    }
    return choice;
}

public static double getOperand(String prompt){
    Scanner input=new Scanner(System.in);
    System.out.println("What is the second number?");
    double secondNumber=input.nextDouble();
    return secondNumber;
}

public  double getCurrentValue(){
    return currentValue;
}

public void add(double operand2){
    currentValue+=operand2;
}

public void subtract(double operand2){
    currentValue-=operand2;
}

public void multiply(double operand2){
    currentValue*=operand2;
}

public void divide(double operand2){
    currentValue/=operand2;
}

public void clear(){
    currentValue=0;
}

public static void main(String[] args) {
    double value=getCurrentValue(); 
}

} }

When I try to set double value=getCurrentValue(); 当我尝试设置double value=getCurrentValue(); at the end, I get an error message "Cannot make a static reference to the non-static method." 最后,我收到一条错误消息“无法对非静态方法进行静态引用”。 It says the fix is to make the getCurrentValue() method static as well, but I was told not to make that field static by my professor. 它说修复是为了使getCurrentValue()方法也是静态的,但我被告知不要让我的教授将该字段设为静态。 Is there a simple solution to this that I am just missing? 有一个简单的解决方案,我只是缺少?

A static method belongs to the class, a non-static method belongs to an instance of the class. 静态方法属于该类,非静态方法属于该类的实例

When you call getCurrentValue() from main , you get an error because main isn't associated with any instance. main调用getCurrentValue()时,会出现错误,因为main与任何实例都没有关联。

You need to create an instance of the class: 您需要创建该类的实例:

HallLanceMemoryCalculator me = new HallLanceMemoryCalculator();

Then you can call the instance's getCurrentValue() : 然后你可以调用实例的getCurrentValue()

double value = me.getCurrentValue();

Static means there is one for an entire class, whereas if it is non-static there is one for each instance of a class (object). 静态意味着整个类有一个,而如果它是非静态的,则每个类(对象)的实例都有一个。 In order to reference a non-static method from a static context, you need to first create an object for that method to be a part of. 为了从静态上下文引用非静态方法,您需要首先为该方法创建一个对象作为其一部分。 So, in your main method (the static context), you need to create a new HallLanceMemoryCalculator object. 因此,在main方法(静态上下文)中,您需要创建一个新的HallLanceMemoryCalculator对象。 Once you have the object, you can use the object's methods. 获得对象后,可以使用对象的方法。

The reason your professor does not want it to be static, is so that you have the ability to have multiple HallLanceMemoryCalculator instances, that each keep track of their own value. 你的教授不希望它是静态的原因是你有能力拥有多个HallLanceMemoryCalculator实例,每个实例都跟踪它们自己的值。

Note, I'm not including any code, because I'm sure your professor would want you to figure out that part on your own. 请注意,我没有包含任何代码,因为我相信你的教授会希望你自己找出那个部分。

The Method getCurrentValue() is defined as an ordniary (non-static) method of the class. 方法getCurrentValue()被定义为类的ordniary(非静态)方法。 In order to execute it, you need an instance of HallLanceMemoryCalculator. 为了执行它,您需要一个HallLanceMemoryCalculator实例。

So try to instantiate HallLanceMemoryCalculator first: 因此,首先尝试实例化HallLanceMemoryCalculator:

    HallLanceMemoryCalculator calc = new HallLanceMemoryCalculator();
    double value = calc.getValue();

In order to make some sense, the example should have a constructor for storing the initial value. 为了说明一点,该示例应该有一个用于存储初始值的构造函数。 Eg 例如

    public HallLanceMemoryCalculator(double initial) {
        this.currentValue = initial;
    }

In doing so, you can use the following main code: 这样做,您可以使用以下主要代码:

    HallLanceMemoryCalculator calc = new HallLanceMemoryCalculator(10);
    int choice = displayMenu();

    // some code to get the second operand (you don't need the string as param)
    double operand = getOperand("");

    // some switch statement to handle the choice
    ...

    double result = calc.getCurrentValue();

创建HallLanceMemoryCalculator的实例,然后调用getCurrentValue()或使getCurrentValue()静态。

如果要在不设置static属性的情况下调用getter函数,则应使用类的实例而不是类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM