Java 中的 Getters、Setter 和构造函数 - 制造汽车并储存它

Getters, Setters and Constructors in Java - Making a car and stocking it

我正在尝试使用 BlueJ 在 Java 中创建一个 class。我的 class 名为 Automobile。我的目标是能够使用我的 Constructor 方法创建具有以下变量的汽车:年份、颜色、品牌、门数、公里数、是否自动(布尔值)、是否已售出(布尔值)、描述以及一个识别号码。所有变量都有一个设置的默认值、最小和最大可接受值。

我的方法必须使用 getVariablename 和 setVariablename。我的颜色和品牌变量是 int,我在 class 的 table 中创建了方法来检索它们对应的字符串。

我的问题是我不明白在一种方法中设置我的变量并在另一种方法中获取它的原理(同时确保它是一个可接受的值)。另外,一旦我有了 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

所以我的困难在于:

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

P.S.: 原作是法文,所以我尽量翻译了变量名和方法名,以便您更好地理解。另外,变量名,方法等都是强加的,我被迫完全按照这种方式制作class。

编辑:因此,还强制使用静态进行品牌和颜色转换。这两种方法仅用于 return 从 int 值转换字符串。它们不在构造函数中使用。最后,将在工作的第二部分使用单独的验证循环处理异常。 Automobile class 实际上仅用于处理 "car" 对象的创建。

您的代码有几个问题:

(1) 您没有任何适合 Automobile

的实例变量(如 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-最好使用 this.p=p 来对您的对象进行属性设置。

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