【问题标题】:non-static variable cannot be referenced from a static context in println statement不能从 println 语句中的静态上下文引用非静态变量
【发布时间】:2015-02-09 05:18:42
【问题描述】:

我知道这类问题经常被问到,而且我已经阅读过 this onethis one 但由于某种原因,我仍然无法理解我当前程序遇到的问题。

我正在尝试创建一组定义一系列 3-D 形状的类,这些形状可以存储其大小,并提供更改数据的访问权限。它还应该能够计算周长、面积和体积。我只到了我正在做我的第一个选择的形状(一个球体)的地步,我在 if 语句中的 println 中遇到了这个错误。

    import java.util.Scanner;
public class Shapes
{
    public static void main(String[] args)
    {
        String input;
        double Radius;

        Scanner scan = new Scanner(System.in);

        System.out.println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
        input = scan.next();

        if (input.equals("S"))
        {
            System.out.println("Enter radius: ");
            Radius = scan.nextDouble();
            Sphere calcCircumference;
            Sphere calcArea;
            Sphere calcVolume;
            System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");
        }

    }
}

public class Sphere
{
    // instance variables
    protected double radius;
    protected double circumference;
    protected double area;
    protected double volume;
    Scanner scan = new Scanner(System.in);

    /**
     * Constructor for objects of class Sphere
     */
    public Sphere()
    {
        // initialise instance variables
        radius = 0;
        circumference = 0;
        area = 0;
        volume = 0;
    }

    /**
     *Gets user entered radius
     */
    public double getRadius(double Radius)
    {
        radius = radius;
        return radius;
    }

    public double calcCircumference()
    {
        circumference = 2*Math.PI*radius;
        return circumference;
    }

    public double calcArea()
    {
        area = 4*Math.PI*Math.pow(radius,2);
        return area;
    }

    public double calcVolume()
    {
        volume = (4*Math.PI*Math.pow(radius,3))/3;
        return volume;
    }
}

任何帮助或指导将不胜感激。

【问题讨论】:

  • Sphere.circumference - 哪个领域?
  • 您需要创建一个 Sphere 对象来访问圆周或将它们定义为静态,为什么要创建 Sphere 的 calcCircumference calcArea calcVolume 对象?还是您想像这样调用这些函数?

标签: java variables non-static


【解决方案1】:
System.out.println("The circumference is "+Sphere.circumference+", the area is "+Sphere.area+", the volume is "+Sphere.volume+".");

您需要先创建Sphere 的实例,然后才能访问它的字段。

Sphere sphere = new Sphere();

然后您需要提供对象所需的信息

sphere.radius = Radius; // I'd prefer a setter method

那么你就可以利用这个实例提供的信息了……

System.out.println("The circumference is "+sphere.calcCircumference()+", the area is "+Sphere.calcArea()+", the volume is "+Sphere.calcVolume()+".");

circumferenceareavolume不需要跟踪,它们是计算字段,你只需要调用你需要的方法就可以得到计算结果。

字段circumferenceareavolumeradius 被称为实例字段,它们需要类的实例才能使用它们。这意味着您可以拥有多个 Sphere 实例,每个实例都有自己的 unquie 值

你可能想仔细看看Classes and Objects

【讨论】:

  • 哇。这实际上比我读过的任何其他评论和我的书都更有意义!谢谢!!!
  • @Elliot 记住,OO 很有趣,尤其是在凌晨 2 点喝一壶咖啡...
【解决方案2】:

我稍微修改了你的代码,现在它可以工作了(可能不是最有效的方法):

import java.util.Scanner;

public class Shapes {
    public static void main(String[] args) {
        String input;
        double Radius;

        Scanner scan = new Scanner(System.in);

        System.out
                .println("Choose a shape. 'S' for sphere, 'P' for pyramid, 'C' for cone: ");
        input = scan.next();

        if (input.equals("S")) {
            System.out.println("Enter radius: ");
            Radius = scan.nextDouble();
            Sphere calcCircumference;
            Sphere calcArea;
            Sphere calcVolume;
            System.out.println("The circumference is " + Sphere.circumference
                    + ", the area is " + Sphere.area + ", the volume is "
                    + Sphere.volume + ".");
        }

    }

    public static class Sphere {
        // instance variables
        protected double radius;
        protected static double circumference;
        protected static double area;
        protected static double volume;
        Scanner scan = new Scanner(System.in);

        /**
         * Constructor for objects of class Sphere
         */
        public Sphere() {
            // initialise instance variables
            radius = 0;
            circumference = 0;
            area = 0;
            volume = 0;
        }

        /**
         * Gets user entered radius
         */
        public double getRadius(double Radius) {
            radius = radius;
            return radius;
        }

        public double calcCircumference() {
            circumference = 2 * Math.PI * radius;
            return circumference;
        }

        public double calcArea() {
            area = 4 * Math.PI * Math.pow(radius, 2);
            return area;
        }

        public double calcVolume() {
            volume = (4 * Math.PI * Math.pow(radius, 3)) / 3;
            return volume;
        }
    }
}

【讨论】:

  • 当 OP 创建一个更多 Sphere 的实例时,他们将有很多具有相同值的作业,基于其中一个调用关联的 calcXxx 方法...@ 987654324@ 是邪恶的,应该避免(恕我直言)
  • 并使变量变为静态不需要在构造函数中初始化为默认值,您可以省略这些行。
猜你喜欢
  • 2019-03-17
  • 2011-11-30
  • 1970-01-01
相关资源
最近更新 更多