2016-12-06 57 views
0

我正嘗試使用BlueJ在Java中創建一個類。我的班級名爲汽車。我的目標是能夠使用我的構造函數方法創建具有以下變量的汽車:年份,顏色,品牌,門數,公里數,自動(布爾值),售出(布爾值),說明和一個識別號碼。所有變量都有一個設定的默認值,一個最小值和一個最大值。Java中的Getters,Setter和Constructors - 製作一輛汽車並放養它

我必須爲我的方法使用getVariablename和setVariablename。我的顏色和品牌變量是int,並且我製作了方法來在我的類的表格中檢索它們的String對應項。

我的問題是我不明白在一個方法中設置我的變量並將其置於另一個方法(同時確保它是可接受的值)的原則。另外,一旦我有我的Setter和Getter方法,在創建我的構造函數方法時,我需要寫下什麼?

到現在爲止,我有這樣的:

public class Automobile { 

    private static final String[] COLORS = { "Other", "Noir", "Blanc", "Bleu Nuit", "Bleu Clair", "Vert Pomme", "Vert Bouteille", "Taupe", "Argent", "Sable"}; 

    private static final String[] BRANDS = { "Autre", "Mazda", "Toyota", "Ford", "GM", "Hyunday", "BMW", "SAAB", "Honda"};  


    public static final int COLOR_DEF = 8; 
    public static final int COLOR_MIN = 0; 
    public static final int COLOR_MAX = COULEURS.length - 1; 

    public static final int BRAND_DEF = 4; 
    public static final int BRAND_MIN = 0; 
    public static final int BRAND_MAX = MARQUES.length - 1; 

    public static final double KILO_DEFAULT = 55000; 
    public static final double KILO_MIN = 15000; 
    public static final double KILO_MAX = 140000; 

    public static final int TWO_DOORS = 2; 
    public static final int FOUR_DOORS = 4; 
    public static final int DOORS_DEFAULT = FOUR_DOORS; 

    public static final boolean AUTO_DEF = true; 
    public static final int YEAR_MIN = 1997; 
    public static final int YEAR_MAX = 2016; 
    public static final int YEAR_DEFAUT = 2007; 

    public static final String COMM_DEFAUT = ""; 


    public static String color (int cou) { 

     String chainecolor = ""; 

     if (cou >= COLOR_MIN && cou <= COLOR_MAX) { 
      chainecolor = COLORS[cou]; 
     } 

     return chainecolor; 
     } //This method is to return the String value of a color from its int value using the COLORS table. If invalid it returns an empty chain. 


    public static String brand (int br) { 

     String chainebrand = ""; 

     if (ma >= BRAND_MIN && ma <= BRAND_MAX) { 
      chainebrand = BRANDS[br]; 
     } 
     return chainebrand; 
     } //same thing for the brand 

    public Automobile (int brand, int year, int color, boolean automatic, double kilometers,int nbrDoors, String description, boolean sold){ 

     //To be completed   
    } 

    //here i'm supposed to create getters that return int values for everything but automatic, sold and description 

    public void setYear (int year) { 
     if (year >= YEAR_MIN && YEAR <= YEAR_MAX) { 
     year = year; 
     } 
    } // supposed to be the setter for my year, as long as it's within the accepted values 

    public void setMarque (int brand){ 
     if (brand >= BRAND_MIN && brand <= BRAND_MAX) { 
      brand = brand; 
     } 
    } //same, for the brand 

    public void setColor (int color) { 

     if (color >= COLOR_MIN && color <= COLOR_MAX){ 
      color = color; 
     } 
    }// same for the color 

    public void setNbrDoors (int p) { 

     if (p == TWO_DOORS || p == FOUR_DOORS){ 
      p = p; 
     } 
    } // same for the door. I am forced to use (int p) as the variable for this method, which confuses me as to how I will refer to it from nbrDoors up in the Automobile constructor method 

} // Automobile 

所以我的困難在於:

  • 是我爲這個目的而作出有效制定者的例子嗎?我不明白是否需要p = p或color = color ...

  • 如何創建一個getter方法,該方法能夠從setNbrDoors中獲取變量p,並返回其值並使其具有它用於汽車構造函數中的nbrDoors?

  • 我應該在構造函數方法中寫什麼,比如它能夠從getters中獲取它的值?

這一切都是因爲第二部分是我必須要創建一些代碼來要求用戶輸入的所有變量值,然後創建一個表來炒股用戶創建的汽車。

P.S .:工作原本是法文,所以我翻譯了變量和方法名稱,以便我更好地理解。此外,變量名稱,方法等都是強加的,我強迫這種方式完全按照這種方式。

編輯:同樣,靜態品牌和顏色轉換的使用也被強加。這兩種方法僅用於從int值返回字符串。它們不在構造函數中使用。最後,異常將在第二部分工作中使用單獨的驗證循環進行處理。 Automobile類實際上僅用於處理「汽車」對象的創建。

回答

0

有你的代碼的幾個問題:

(1)您沒有任何適當的實例變量(如yearbrand,等..)汽車

(2)您沒有使用this.設置實例變量(因爲您沒有創建它們)enter code here。請注意,this始終指向當前對象,請參閱here,即當您說this.year= year時,實際上將右手邊year賦值給當前對象的變量(左手邊)year

您可以參考下面的代碼註釋:

public class Automobile { 

     private int year; 
     private int color; 
     private int brand; 

     //add other fields 

     public Automobile (int brand, int year, int color, boolean automatic, double kilometers,int nbrDoors, String description, boolean sold) { 

      if (year >= YEAR_MIN && year <= YEAR_MAX) { 
       this.year = year; 
      } else { 
       new IllegalArgumentException("Invalid Year Passed to construct Automobile"); 
      } 

      //Similarly add other validations for brand, color, etc.. 
     } 

     public void setYear (int year) { 
      if (year >= YEAR_MIN && YEAR <= YEAR_MAX) { 
       //USE 'this.' as shown below' to set the given year to 'this' object's year 
       this.year = year; 
      } 
     } 

     public int getYear() { 
      return year; 
     } 

     //Similarly add setters and getters for year, color, brand, etc... 
} 
+0

所以我必須用這個。在所有安裝者。你能提供一個這個變量的Getter的例子嗎? 此外,品牌和顏色轉換的靜態也是強加的。這兩種方法僅用於從int值返回字符串。它們不在構造函數中使用。最後,異常將在第二部分工作中使用單獨的驗證循環進行處理。汽車類真的只用於製造汽車。 (我會將此添加到我的文章中) –

+0

由於您太棒了,所以更新了上面的代碼,使用getter – developer

+0

javaguy,我還有一個額外的問題。我必須創建一個變量,該變量將作爲我製作的每輛車的唯一標識號,並從1開始(所以第一輛車將是1,第二輛將是2等)。之後,我將有一種方法來克隆我創建的任何汽車,因此創建一個具有所有相同屬性的新車,除了銷售和識別變量。對於出售,我可以將其重置爲false,但是如何每次都複製所有變量的值,同時生成新的標識號?謝謝 –

0

1 - 這是更好地使用這個 P = p來你的對象屬性。

2 setNbrDoors,返回一個無效的,你不能從中拿起一個變量,你應該創建一個getNbrDoors:int getNbrDoors() { return this.p; }