如何在Java中从main调用非静态方法



我一直在为Comp Sci入门课程的实验室工作,遇到了这个问题:

根据说明,我必须拥有每个回答特定问题的方法(例如:问题5.9,方法是fivePointNine(),并且必须由另一个类(Assignment6.main)在main中调用。我的问题是main是静态的,我需要为实验室调用的最后一个方法是非静态的,所以它不允许我编译和运行。我的三个错误与multiSphere(x)有关。具体错误是"无法从类型Assignment6对非静态方法"multiSphere(double)"进行静态引用。"

public class Assignment6
{
public Sphere newSphere = new Sphere();
public Assignment6 A6 = new Assignment6();
public static int x;
public static void main(String[] args)
{
       x = 2 + (int)(Math.random() * ((10 - 2) + 1));
   multiSphere(x);
   x = 2 + (int)(Math.random() * ((10 - 2) + 1));
   multiSphere(x);
   x = 2 + (int)(Math.random() * ((10 - 2) + 1));
   multiSphere(x);
    }
    public void multiSphere(double diameter)
    {
    Sphere.inputDiameter(diameter);
    Sphere.volumeCalc(diameter);
    Sphere.surfaceAreaCalc(diameter);
    toString();
}
public String toString()
{
   return "The diameter of this sphere is " + Sphere.inputDiameter(x) + ", the volume      is " + Sphere.volumeCalc(x) + ", and the surface area is " + Sphere.surfaceAreaCalc(x) + ". ";
}

我的第二个类叫Sphere,看起来像这样:

public class Sphere 
{
public Assignment6 newA6 = new Assignment6();
public static double volume;
public static double surfaceArea;
public static double inputDiameter(double diameter)
{
    return diameter;
}
public static double volumeCalc(double diameter)
{
    // Volume = (4.0/3.0)(pi)(r)^3
    volume = (4.0/3.0) * (Math.PI) * Math.pow((diameter/2.0), 3);
    return volume;
}
public static double surfaceAreaCalc(double diameter)
{
    // Surface area = (4)(pi)(r)^2
    surfaceArea = (4) * (Math.PI) * (Math.pow((diameter/2.0), 2));
    return surfaceArea;
}
}

我不知道如何在主方法中调用multiSphere(x),而不会遇到我一直遇到的错误。我觉得我错过了这么简单的东西。

这里有很多需要尽快纠正,尤其是在决赛即将到来的情况下。

首先,您需要了解Sphere类应该如何工作。Sphere的每个实例都是它自己的东西,并且知道如何做事。Sphere所需要给出的只是它的直径(或半径),有了它,它就可以计算出关于它自己所需要的一切。它可以计算其体积和表面积,并可以在toString中打印出来。

所以你想让Sphere看起来像这样:

public class Sphere {
    public double diameter;
    public Sphere(double diameter) {
        this.diameter = diameter;
    }
    public double volume() {
        return (4.0 / 3.0) * (Math.PI) * Math.pow((diameter / 2.0), 3);
    }
    public double surfaceArea() {
        return (4) * (Math.PI) * (Math.pow((diameter / 2.0), 2));
    }
    public String toString()
    {
       return "The diameter of this sphere is " + diameter + ", the volume is " + volume() + ", and the surface area is " + surfaceArea() + ". ";
    }
}

注意Sphere是如何具有构造函数的,这就是我们创建Sphere的新实例的方式。我们只使用直径。一旦有了它,Sphere的实例就可以做Sphere所做的一切

请注意,没有static。我不知道你为什么那么爱static

你应该知道static意味着类的每个实例都共享它。这不适用于Sphere,因为每个实例都有自己的直径。这不像是一群Sphere共用一个直径。有些直径可能相等,但它们仍然是自己的。这就像你和我可以开宾利(一厢情愿),它们在各个方面都是一样的,但它们仍然是两辆不同的车。当然,static事物不能访问非static事物,因为非static事物需要是可能不可用的对象实例的一部分。

最后,在您的主类Assignment6中,您只需执行以下操作:

Sphere s = new Sphere(5);

以创建直径为5的CCD_ 19的实例。现在s可以通过toString告诉我们它的体积、表面积和描述。

我真的不确定你的任务是什么,但希望这能帮助你。

关于

如何在Java中从main调用非静态方法?

简短回答:你不会,至少不会直接回答。规范的答案是用该方法创建一个类的实例,然后在该实例上调用该方法,但在您的情况下,这将毫无价值,因为该类只有静态字段和方法。这使得toString方法在一个充满静态内容的类中毫无意义。要么使方法为静态的(它将不是真正的toString()重写),要么创建一个具有非静态字段和方法的真正类。


正确的做法是从Sphere类(这里的关键类)中去掉所有静态的所有东西,然后给它一个像样的public String toString()方法覆盖。在您发布的两个类中,它是唯一一个toString()方法有意义的类,但同样,前提是它是一个具有非静态方法和字段的真正面向对象的类。


接下来,您需要给出有意义的Spheregetter和setter方法。此:

public static double inputDiameter(double diameter)
{
    return diameter;
}

既不鱼也不臭。去掉它,创建真正的getter和setter,getter不接受参数但返回值,setter接受参数并声明返回void。


接下来,在静态主方法中,您将创建一个Sphere变量,并为其指定一个新的Sphere实例。然后可以调用Sphere的适当方法,包括它的toString()方法。


简而言之,废弃所有代码,重新开始。从最重要的一点开始——创建一个像样的Sphere类。然后创建带有main方法的Assignment类,并创建Sphere实例。

如果你仍然感到困惑,请张贴你的实际任务说明,而不是你对它们的解释或转述,这样我们就可以准确地看到你应该做什么。


例如,使用一个与您相似但不相同的相关示例:

CircleTest.java

public class CircleTest {
   public static void main(String[] args) {
      Circle circle = new Circle(10.0);
      System.out.println(circle); // calls toString()
   }
}

Circle.java

public class Circle {
   private double diameter;
   public Circle(double diameter) {
      this.diameter = diameter;
   }
   public double getDiameter() {
      return diameter;
   }
   public double calculateArea() {
      return Math.PI * diameter * diameter / 4.0;
   }
   public double calculateCircumference() {
      return Math.PI * diameter;
   }
   @Override
   public String toString() {
      String result = String.format("Circle with diameter %.2f, area %.2f, "
            + "circumference %.2f", 
            diameter, calculateArea(), calculateCircumference());
      return result;
   }
}

您可以使用新实例来调用如下方法:

public static void main(String[] args)
{
    Assignment6 assignment6 = new Assignment6();
    ....
    assignment6.multiSphere(x);
  ....
    assignment6.multiSphere(x);
     ....
    assignment6.multiSphere(x);
}

但是程序有一些错误,请检查。

首先,将变量(newSphere、A6和x)设为私有变量。全局变量几乎总是私有的
就你的问题而言——与其让主方法调用multiSphere,不如创建一个对象,并在此基础上调用它。

public static void main(String[] args) {
    ...
    Assignment6 newAssig = new Assignment6();
    ...
    newAssig.multiSphere();
 }

最新更新