繁体   English   中英

Java:无法从静态上下文引用非静态变量

[英]Java: non-static variable cannot be referenced from a static context

  package lab10;

  import java.util.*;
  public class Lab10 {

  class QuadraticEquation{

   double a,b,c;

   QuadraticEquation(){
     Random number1 = new Random();
     a = (int) (number1.nextDouble() * 8 + 1.0);

     Random number2 = new Random();
     b = (int) (number2.nextDouble() * 8 + 1.0);

     Random number3 = new Random();
     c = (int) (number3.nextDouble() * 8 + 1.0);


   }
   double getA(){

       return a;
   }
   double getB(){

       return b;
   }
   double getC(){      

       return c;
   }
   double getDiscriminant(){
       double discriminant;

       discriminant = (Math.pow(b, 2)- 4 *(a *c));
       return discriminant;
     }
   }

public static void main(String[] args) {

    QuadraticEquation equation = new QuadraticEquation(); //<---- error

    System.out.println("Three randomized coefficients are: ");
    System.out.println("a = " + equation.getA());
    System.out.println("b = " + equation.getB());
    System.out.println("c = " + equation.getC());

}
  }

我已经在stackoverflow上阅读了几篇有关此错误的文章,但是即使阅读了这些文章,我似乎也不太明白为什么会得到它。 我正在引用以前的项目,但我不明白为什么我不能声明QuadraticEquation equation = new QuadraticEquation()

这是我正在引用的项目:

   package InClass05;

   public class TestSimpleSquare {

   public static void main(String[] args) {

    SimpleSquare square1 = new SimpleSquare();
    System.out.println("The area of the square of side lengths " + square1.length + " is " + square1.getArea());
    System.out.println("The perimeter of the square of side lengths " + square1.length + " is " + square1.getPerimeter());
    System.out.println("The diagonal of the square of side lengths " + square1.length + " is " + square1.getDiagonal());

    System.out.println("");
    SimpleSquare square2 = new SimpleSquare(25);
    System.out.println("The area of the square of side lengths " + square2.length + " is " + square2.getArea());
    System.out.println("The perimeter of the square of side lengths " + square2.length + " is " + square2.getPerimeter());
    System.out.println("The diagonal of the square of side lengths " + square2.length + " is " + square2.getDiagonal());

    System.out.println("");
    square2.length = 500;
    System.out.println("The area of the square of side lengths " + square2.length + " is " + square2.getArea());
    System.out.println("The perimeter of the square of side lengths " + square2.length + " is " + square2.getPerimeter());
    System.out.println("The diagonal of the square of side lengths " + square2.length + " is " + square2.getDiagonal());


}
  }
  class SimpleSquare{
  double length;

   SimpleSquare(){
    length = 10;
}

SimpleSquare(double newlength){
    length = newlength;
}

double getArea(){
    return length * length;
}

double getPerimeter(){
    return length + length + length + length;
}

double getDiagonal(){
    return Math.sqrt(Math.pow(length, 2) + Math.pow(length, 2));
}
void setlength(double newlength){
    length = newlength;
}
 }

在这个例子中,我可以声明SimpleSquare square1 = new SimpleSquare(); 我不明白为什么我现在无法完成的项目无法做到这一点。 任何帮助,将不胜感激。 谢谢。

它应该是:

QuadraticEquation equation = new Lab10().new QuadraticEquation();

因为QuadraticEquation是内部类

您可以添加单词static来使您的类静态:

static class QuadraticEquation

没有该类,您的类将位于Lab10内部,因此应将其附加到封闭的实例。 通常,内部类是在类的成员内部创建的。

它在此之后编译:

Three randomized coefficients are: 
a = 5.0
b = 7.0
c = 3.0

QuadraticEquation被定义为Lab10类中的一个内部类。 这样,除非在外部类的实例中创建它,否则不能创建它,除非您将其更改为静态内部类:

static class QuadraticEquation{
...
}

或者,如果您需要它是非静态的,则可以在创建内部类时创建外部类的实例:

QuadraticEquation equation = new Lab10().new QuadraticEquation();

经过一时的困惑之后,我发现在您的其他情况下,类SimpleSquare并不是内部类。 这是顶层的另一类。

Java允许您在同一文件中的同一级别上定义多个类,只要其中一个是公共的即可。

区别在于(可能是偶然的)末端支架的位置不同!

暂无
暂无

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

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