【问题标题】:Invoking static methods from another class从另一个类调用静态方法
【发布时间】:2016-02-24 14:28:06
【问题描述】:

所以我过去几天一直在研究这个程序,但遇到了障碍。我正在尝试制作一个计算器,它使用带有用户输入的特定程序来计算多个形状的表面积或体积。您可能会认为这是 Horstmann java 概念书中的项目 8.5。

我不知道如何从一个文件调用方法到另一个文件,需要弄清楚。

代码如下:

import java.util.Scanner;

public class p85SantoCharlie{


  public static double sphereVolume(double r){

   Scanner in = new Scanner(System.in);

   System.out.println("Please enter your Radius");
   r = in.nextDouble();

   double volume = (4/3) * Math.PI * r * r * r;

   System.out.println(volume);

   return volume;

   }

  public static double sphereSurface(double r){

   Scanner in = new Scanner(System.in);

   System.out.println("Please enter your Radius");
   r = in.nextDouble();

   double surface = 4 * Math.PI * r * r ;

   System.out.println(surface);

   return surface;

   }

  public static double cyliderVolume(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double volume =  Math.PI * r * r * h ;

System.out.println(volume);

return volume;

   }

  public static double cylinderSurface(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double surface = 2 * Math.PI * r * r * h ;

System.out.println(surface);

return surface;

   }  

  public static double coneVolume(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double volume = Math.PI * r * r * h / 3 ;

System.out.println(volume);

return volume;

    }

  public static double coneSurface(double r, double h){

Scanner in = new Scanner(System.in);
Scanner out = new Scanner(System.in);

System.out.println("Please enter your Radius");
r = in.nextDouble();

System.out.println("Please enter your Height");
h = out.nextDouble();

double surface = Math.PI * r * (r + Math.pow(( r * r + h * h), .5));

System.out.println(surface);

return surface;
   }
}

这里是主文件:

    import java.util.Scanner;

public class p85SantoCharlieMain{

   public static void main(String[] args){

   p85SantoCharlie mainProgram = new p85SantoCharlie();

   Scanner in = new Scanner(System.in);

   System.out.println("Please select a shape");

   System.out.println("your choices are: Sphere, Cylinder, and Cone");

   String answer1 = in.next();

   String answer1Caps = answer1.toUpperCase();

   System.out.println("Fantastic! now select a formula");

   System.out.println("your choices are: surface area or volume");

   String answer2 = in.next();

   String answer2Caps = answer2.toUpperCase();



   if (answer1Caps.equals("SPHERE")&& answer2Caps.equals("SURFACE AREA")){

      mainProgram.sphereSurface();

   }



   } 

}

谢谢!

【问题讨论】:

  • 我不明白你的问题。您已经在调用其他“程序”(但是缺少参数):mainProgram.sphereSurface();

标签: java math calculator


【解决方案1】:

如果方法是静态的,您可以使用类名调用它们

p85SantoCharlie.sphereVolume(1.1);

如果它们不是静态的,则初始化新的类实例并使用它来调用

p85SantoCharlie p = new p85SantoCharlie();
p.sphereVolume(1.1);

【讨论】:

    【解决方案2】:

    三个问题:

    首先,你在p85SantoCharlie中的所有计算方法都是static。您不需要 p85SantoCharlie 的实例来调用它们。你应该摆脱你做new p85SantoCharlie() 的行并调用你的方法,比如

    p85SantoCharlie.sphereVolume(r);
    

    其次,您已将计算方法声明为带参数,但在调用它们时您不知道这些参数。例如,sphereVolume() 被声明为采用double r。但是你不知道r 的值,直到你在sphereVolume() 方法中读入它。所以这行不通。您需要更改您的 main 方法以请求半径,然后将其传递给 sphereVolume,如上所示。

    第三,所有Scanners!除了main 中的那个之外,将它们全部删除。当您调用计算方法时,您将传入 r(或其他)的值。只需使用传入的内容即可。

    【讨论】:

      【解决方案3】:

      丑陋、不可读的代码。

      Java 是一种面向对象的语言。我希望你从Shape 接口开始:

      public interface Shape {
          double surfaceArea();
          double volume();
      }
      

      我希望看到不同类型的子类可以适当地进行计算。这是一个 Sphere 实现:

      public class Sphere implements Shape {
          private final double radius;
      
          public Sphere(double radius) {
              if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
              this.radius = radius;
          }
      
          public double surfaceArea() {
              return 4.0*Math.PI*this.radius*this.radius;
          }
      
          public double volume() {
              return 4.0*Math.PI*this.radius*this.radius*this.radius/3.0;
          }
      }
      

      这是一个 Cylinder 实现:

      public class Cylinder implements Shape {
          private final double radius;
          private final double height;
      
          public Cylinder(double radius, double height) {
              if (radius <= 0.0) throw new IllegalArgumentException("radius must be positive");
              if (height <= 0.0) throw new IllegalArgumentException("height must be positive");
              this.radius = radius;
              this.height = height;
          }
      
          public double surfaceArea() {
              return 2.0*Math.PI*this.radius*this.radius + this.radius*this.height;
          }
      
          public double volume() {
              return Math.PI*this.radius*this.radius*this.height;
          }
      }
      

      我希望看到您的驱动程序类与 Shape 对象交互以解决问题。

      您需要为Shape 考虑一个虚拟构造函数/工厂,因为您不想让if/else 构造使您的代码混乱。

      我的观点:发明面向对象编程是为了解决两个问题:

      1. 将状态和行为封装到一个抽象数据类型中。
      2. 继承和多态性可消除 if/else 和 switch 语句。

      不要用“if sphere and area”类型的决策来混淆您的代码。让多态为您处理。

      新程序员往往会花太多时间担心用户界面。专注于功能并首先使其工作。不要创建一堆文本问题/答案代码,而将问题的核心部分搁置一旁。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 2016-06-26
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 2021-10-11
        相关资源
        最近更新 更多