【问题标题】:cannot make a static reference to a non static method不能对非静态方法进行静态引用
【发布时间】:2014-02-17 21:00:40
【问题描述】:

到目前为止,我有以下代码:

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(); 
}

}

当我尝试在末尾设置 double value=getCurrentValue(); 时,我收到一条错误消息“无法对非静态方法进行静态引用”。它说解决方法是使getCurrentValue() 方法也静态,但是我的教授告诉我不要使该字段静态。有没有我只是想念的简单解决方案?

【问题讨论】:

  • but I was told not to make that field static by my professor 因此,替代方案是......?
  • 有关静态引用和非静态成员的信息,请查看本页右侧“相关”下的内容。
  • 创建一个实例。我有点喜欢把它命名为meMyClass me = new MyClass(); double value = me.getCurrentValue();
  • @user3221816 - 在任何人为您回答这个问题之前,他们需要知道 - 您知道“静态”的实际含义吗?如果你这样做,答案应该是显而易见的。如果您不这样做,那么我建议您阅读您的课程笔记或在线 Java 教程。
  • 我同意@DavidWallace。创建一个实例并使用该对象从静态上下文访问非静态方法或变量。 HallLanceMemoryCalculator c = new HallLanceMemoryCalculator(); double value=c.getCurrentValue();

标签: java static-methods


【解决方案1】:

静态方法属于类,非静态方法属于类的实例

当您从 main 调用 getCurrentValue() 时,您会收到错误消息,因为 main 未与任何实例关联。

你需要创建一个类的实例:

HallLanceMemoryCalculator me = new HallLanceMemoryCalculator();

然后就可以调用实例的getCurrentValue()

double value = me.getCurrentValue();

【讨论】:

    【解决方案2】:

    静态意味着整个类都有一个,而如果它是非静态的,则类(对象)的每个实例都有一个。为了从静态上下文中引用非静态方法,您需要首先为该方法创建一个对象,使其成为其中的一部分。因此,在您的 main 方法(静态上下文)中,您需要创建一个新的 HallLanceMemoryCalculator 对象。拥有对象后,就可以使用对象的方法了。

    您的教授不希望它是静态的,原因是您能够拥有多个 HallLanceMemoryCalculator 实例,每个实例都跟踪自己的值。

    注意,我没有包含任何代码,因为我确信你的教授会希望你自己弄清楚这部分。

    【讨论】:

      【解决方案3】:

      方法 getCurrentValue() 被定义为类的普通(非静态)方法。为了执行它,您需要一个 HallLanceMemoryCalculator 实例。

      所以先尝试实例化 HallLanceMemoryCalculator:

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

      为了有一定的意义,这个例子应该有一个构造函数来存储初始值。例如。

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

      这样做,您可以使用以下主要代码:

          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();
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          如果你想调用你的getter函数而不设置属性static,你应该使用类的实例而不是类。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-06-25
            • 2022-12-18
            • 2023-04-02
            相关资源
            最近更新 更多