Java,对象编程:获取返回0值的方法

时间:2015-10-12 14:11:07

标签: java object

主要类

    import javax.swing.JOptionPane;

    public class Shapes {

        static String shape;
        static String circleInput;
        static String c;
        static String r;
        static String t;
        static double radius;

        public static void main(String[] args) {
            // Creation of all three shape objects
            Circle circleSolution = new Circle();
            Rectangle solution2 = new Rectangle(shape, r);
            Triangle solution3 = new Triangle(shape, t);

            String choice = "y";
                // Creation of a while loop which continues when 'choice' is 'y'
            while (choice.equalsIgnoreCase("y")) {
                // Prompts user for a shape or the interger 1 to end the program.
            shape=
                JOptionPane.showInputDialog ("Enter one of the following shapes:"
                    + " circle, rectangle or triangle.");
                    // Converts the String 'shape' into an interger
                if (shape.equalsIgnoreCase("circle")) {
                    shape = c;
                    circleInput = JOptionPane.showInputDialog ("Enter the radius.");
                    radius = Math.abs(Integer.parseInt(circleInput));

                    JOptionPane.showMessageDialog ( 
                    null, "The area of the circle is " + circleSolution.getCircleArea(), "Results", 
                    JOptionPane.PLAIN_MESSAGE);

                    JOptionPane.showMessageDialog ( 
                    null, "The perimeter of the circle is " + circleSolution.getCirclePerimeter(), "Results", 
                    JOptionPane.PLAIN_MESSAGE); 
                }
                else if (shape.equalsIgnoreCase("rectangle")) {
                    shape = r;
                    // Prompts user for pertinent data
                }
                else if(shape.equalsIgnoreCase("triangle")) {
                    shape = t;
                }
            }

圈子对象类

public class Circle { 
    // Circle object created
    Shapes circle = new Shapes();
    // Defines variables used in class
    private static String shape;
    private static double radius;
    private static String circleInput;
    private static String c;
    // Constructor for circles, with arguments
    Circle() {
        shape = Shapes.shape;
        radius = Shapes.radius;
        circleInput = Shapes.circleInput;
        c = Shapes.c;
    }
     // Instance method used to return the shape
    public String getShape() {
        return shape;
    }
     // Instance method used to return the value of the circle's radius
    public double getRadius() {
        return radius;
    }
    // Instance method used return the string, circleInput
    public String getCircleInput() {
        return circleInput;
    }
     // Instance method used to return the specific shape
    public String getC() {
       return c;
    }
    double getCircleArea() {
        double circleArea = Math.PI * radius * radius;
        return circleArea;
    }
    double getCirclePerimeter() {
        double circlePerimeter = 2 * Math.PI * radius;
        return circlePerimeter; 
    }   
}

对于圆的面积和周长,我得到0.0的结果。显然,这是不可取的。

如果您有任何疑问,请务必询问。

非常感谢任何和所有帮助/建议,提前谢谢!

3 个答案:

答案 0 :(得分:0)

您的问题是您没有在Circle对象实例中指定半径值。您获得输入,解析它,然后调用getCircleArea()

看来你正试图让Circle的构造函数使用主类中的静态半径变量作为半径。

我不会讨论为什么这是坏事(完全违背OOP范例),但这不会起作用,因为你已经创建了对象后分配了静态变量。

 if (shape.equalsIgnoreCase("circle")) {
                    shape = c;
                    circleInput = JOptionPane.showInputDialog ("Enter the radius.");
                    radius = Math.abs(Integer.parseInt(circleInput));
                    circleInput.setRadius(radius); //DO THIS                        

                    JOptionPane.showMessageDialog ( 
                    null, "The area of the circle is " + circleSolution.getCircleArea(), "Results", 
                    JOptionPane.PLAIN_MESSAGE);

答案 1 :(得分:0)

类的静态成员是默认初始化的

Sub InsertRow()

Dim time As Integer
Dim y As Integer
Dim colA As String
Dim colB As String
Dim colC As String

time = "FindTime"
colA = "Name1"
colB = "Value1"
colC = "Time1"

y = Application.Match(time, Columns(3), 0)
Rows(y).Insert Shift:=xlDown
Range(Cells(y - 1, "A"), Cells(y - 1, "C")).Borders(xlEdgeBottom).LineStyle = xlDouble

Cells(y, 1) = colA
Cells(y, 2) = colB
Cells(y, 3) = colC

End Sub

在这种情况下,static double radius; 被初始化为零。这就是用于计算面积和周长的内容。

额外注意事项:实例化包含应用程序主方法的类是非常不寻常的。

答案 2 :(得分:0)

嗯,您的代码有几个问题。可能是因为你误解了static和一些面向对象概念的使用。

您的问题:

  1. 您的构造函数为空。当您只有默认构造函数时,您如何期望实例化对象具有不同的属性?
  2. 参数化您的构造函数:

    如果你没有使用setter来设置对象的值,你至少需要一个参数化的构造函数来构造对象:

    // Constructor for circles, with arguments
    Circle(double radius)
    {
        radius = this.radius;
        //Your other variables such as c & circleInput variable is not supposed to be here    
    }
    
    1. 几乎所有物品都是'属性声明为静态。静态变量是"共享"通过同一个类的所有对象,这意味着如果设置了值,它将反映在所有对象中。 (确切地说,静态变量属于类而不是单个对象。)当您希望每个Circle拥有自己的半径但声明为静态时,它没有任何意义。
    2. 删除所有不必要的静态修饰符:

      class Circle{
          private double radius;
      }
      
      public class Shapes {
          private double radius;
      }
      
      1. 凭直觉,你的Circle类应该是Shape的子类,你没有扩展它。
      2. 将您的课程置于适当的等级制度下:

        class Circle extends Shapes{
            //radius inherited from Shapes now
        }
        
        public class Shapes {
            private double radius;
        }
        
        1. 您正在将本地使用的变量(如circleInput)与其他实例变量(如radius)混合。你也在你的形状类中写了你的主要方法,所有的变量混合在一起。您的学校可能希望您将它们分开。
        2. 将变量写入应声明的位置:

          class ShapesRunner(){  //Have a designated class for testing
              public static void main(String[] args){
                  String circleInput, c, r, t;
                  //Your testing for the created shapes.
              }
          }
          
            

          Java,对象编程:获取返回0值的方法

          确保使用setter(如果有的话)或通过其构造函数设置圆的半径。

          示例:

          //receive radius from input
          circle c = new Circle(radius);
          //print c's area and parameter