2015-10-12 48 views
-1

主類Java中,對象編程:get方法返回值爲0

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; 
       } 
      } 

Circle對象類

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的結果。顯然,這是不希望的。

如果您有任何疑問,請一定要問。

任何和所有幫助/建議將不勝感激,在此先感謝!

+2

我的魔球是說,你做了一些錯誤;) –

+1

爲什麼要使用靜態字段? –

+0

你是如何通過半徑的價值。要麼你必須通過方法參數或重新定義你的構造函數採取它。 – yogidilip

回答

0

你的問題是你沒有在你的Circle對象實例中分配半徑值。你得到輸入,解析它,然後調用getCircleArea()

看起來您正在試圖讓Circle的構造函數在您的主類中使用靜態半徑變量作爲半徑。

我不會進入爲什麼這是壞的(完全違背OOP範例),但是這不起作用,因爲您已經在創建對象後分配了靜態變量AFTER。類

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); 
+0

感謝您的幫助,基本上重寫了代碼以刪除所有靜態變量。但是,仍然遇到問題。我在我的Circle類中創建了一個setRadius方法,但是,我得到的錯誤是它無法在我的主方法中找到該符號。 public double setRadius(雙半徑){ return radius; } – Goddard

+0

它應該是'public void setRadius(double radius){this.radius = radius; }'你正在爲類成員'radius'賦值,而不是檢索它 –

+0

非常感謝!當然,我真的需要去關注這個和練習。它現在正在工作:) – Goddard

0

靜態成員是缺省初始化

static double radius; 

在這種情況下radius被初始化爲零。這是用來計算面積和周長的。

額外的注意事項:實例化一個包含應用程序主要方法的類是很常見的。

0

那麼,你的代碼有幾個問題。可能是因爲你誤解了使用static和一些面向對象的概念。

您的問題:

  1. 你的構造函數是空的。當你只有一個默認構造函數時,你如何期望實例化對象具有不同的屬性?

參數化構造函數:

如果你不使用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  
} 
  • 幾乎所有對象的屬性都聲明爲靜態。靜態變量由同一類的所有對象「共享」,這意味着如果設置該值,它將反映在所有對象中。 (更準確地說,靜態變量屬於類而不是單個對象。)當你想讓每個圓都有自己的半徑時,它就沒有任何意義,但是將它聲明爲靜態。
  • 刪除所有不必要的靜態修飾符:

    class Circle{ 
        private double radius; 
    } 
    
    public class Shapes { 
        private double radius; 
    } 
    
  • 憑直覺,你的圈子類應該是Shape的一個子類,你沒有對它進行擴展。
  • 將適當的層次結構下類:

    class Circle extends Shapes{ 
        //radius inherited from Shapes now 
    } 
    
    public class Shapes { 
        private double radius; 
    } 
    
  • 你混合本地使用的變量,如circleInput與其他實例變量,例如半徑。你也在寫你的形狀類的主要方法,所有他們的變化都混在一起。你的學校可能會希望你將它們分開。在它應該被宣佈爲
  • 寫變量:

    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中,對象編程:get方法返回值爲0

    確保您設置的圓的半徑使用setter(如果有的話)或通過其構造函數。

    例子:

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

    謝謝!真的內容豐富,樂於助人。絕對幫助我解決了一些與我的代碼有關的可怕問題。 – Goddard