Java中的吸气剂,制定者和构造者 - 制造汽车并储存它

时间:2016-12-06 00:36:17

标签: java constructor setter getter bluej

我正在尝试使用BlueJ在Java中创建一个类。我的班级名为Automobile。我的目标是能够使用我的构造函数方法来创建具有变量的汽车:年份,颜色,品牌,门数,公里数,如果它是自动的(布尔值),是否已售出(布尔值),描述和识别号码。所有变量都有一个默认值,最小和最大可接受值。

我必须为我的方法使用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,返回其值并将其用于Automobile构造函数中的nbrDoors?

  • 我应该在构造函数方法中写什么,比如它能从getter中获取它的值?

这都是因为第二部分是我必须创建一个小代码来要求用户输入变量的所有值,然后创建一个表来存储用户创建的汽车。

P.S。:这项工作原本是法语,所以我最好地翻译了变量和方法名称,以便您更好地理解。此外,变量名称,方法等都是强制性的,我强行要求这样做。

编辑:因此,也强加了使用静电进行品牌和颜色转换。这两个方法仅用于从int值返回字符串。它们不在构造函数中使用。最后,将使用单独的验证循环在工作的第二部分处理异常。 Automobile类实际上仅用于处理“汽车”对象的创建。

2 个答案:

答案 0 :(得分: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...
}

答案 1 :(得分:0)

1 - 最好使用 .p = p来定位您的对象。

2-setNbrDoors,返回一个void,你不能从中获取一个变量,你应该创建一个getNbrDoors:int getNbrDoors() { return this.p; }