构造函数,Setter,Getter问题在对象创建中

时间:2016-12-06 22:53:52

标签: java constructor counter setter getter

我正在创建一个基于多个变量制作汽车的课程,例如年份(annee),品牌(marque),颜色(couleur)里程表(kilometrage),sold(vendue),传输(automatique),评论(评论) )和门数(nbrPortes)。

代码中的参数是法语,因为我在法国大学,所以请耐心等待。

我的代码需要完全按照现在(强加)的方式设置setter,构造函数和克隆方法。我自己制作了IDnumber和compteurvendue(已售车数)的吸气剂和增量。

以下是我的代码:

public class Automobile {

    private static final String[] COULEURS = { "Autre", "Noir", "Blanc", "Bleu Nuit", "Bleu Clair", "Vert Pomme", "Vert Bouteille", "Taupe", "Argent", "Sable", "Gris Charbon", "Gris Clair", "Orange", "Rouge", "Jaune", "Brun" };

    private static final String[] MARQUES = { "Autre", "Mazda", "Toyota", "Ford", "GM", "Hyunday", "BMW", "SAAB", "Honda", "Mitsubishi", "Mercedes", "KIA", "Wolkswagen"};    


    public static final int COULEUR_DEF = 8;
    public static final int COULEUR_MIN = 0;
    public static final int COULEUR_MAX = COULEURS.length - 1;

    public static final int MARQUE_DEF = 4;
    public static final int MARQUE_MIN = 0;
    public static final int MARQUE_MAX = MARQUES.length - 1;

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

    public static final int DEUX_PORTES = 2;
    public static final int QUATRE_PORTES = 4;
    public static final int PORTES_DEFAUT = QUATRE_PORTES;

    public static final boolean AUTO_DEF = true;
    public static final int ANNEE_MIN = 1997;
    public static final int ANNEE_MAX = 2016;
    public static final int ANNEE_DEFAUT = 2007;

    public static final String COMM_DEFAUT = "";

    public static final boolean VENDUE_DEF = false;

    //private variables

    private int marque;
    private int annee;
    private int couleur;
    private boolean a;
    private boolean automatique;
    private double k;
    private double kilometrage;
    private int p;
    private int nbrPortes;
    private String c;
    private String commentaire;
    private boolean v;
    private boolean vendue;
    private int compteurvendue = 0;

    private int IDnumber = 0;

// CONSTRUCTORS. I do not know I have to make 2, I am thinking for the purpose of the cloning without copying certain variables? I'm confused about this.

    public Automobile (int marque, int annee, int couleur, boolean automatique, double kilometrage) {


        if (marque>= MARQUE_MIN && marque <= MARQUE_MAX) {
            this.marque = marque;
        } else {
            this.marque = MARQUE_DEF;
        }

        if (annee>= ANNEE_MIN && annee <= ANNEE_MAX) {
            this.annee = annee;
        } else {
            this.annee = ANNEE_DEFAUT;
        }

        if (couleur>= COULEUR_MIN && couleur <= COULEUR_MAX) {
            this.couleur = couleur;
        } else {
            this.couleur = COULEUR_DEF;
        }

        if (a == true || a == false) {
            this.automatique = a;
        } else {
            this.automatique = AUTO_DEF;
        }

        if (k >= KILO_MIN && k <= KILO_MAX) {
            this.kilometrage = k;
        } else {
            this.kilometrage = KILO_DEFAUT;
        }

        this.IDnumber = IDnumber + 1;

    }

    public Automobile (int marque, int annee, int couleur, boolean automatique, double kilometrage,
                    int nbrPortes, String commentaire, boolean vendue){

        if (marque>= MARQUE_MIN && marque <= MARQUE_MAX) {
            this.marque = marque;
        } else {
            this.marque = MARQUE_DEF;
        }

        if (annee>= ANNEE_MIN && annee <= ANNEE_MAX) {
            this.annee = annee;
        } else {
            this.annee = ANNEE_DEFAUT;
        }

        if (couleur>= COULEUR_MIN && couleur <= COULEUR_MAX) {
            this.couleur = couleur;
        } else {
            this.couleur = COULEUR_DEF;
        }

        if (a == true || a == false) {
            this.automatique = a;
        } else {
            this.automatique = AUTO_DEF;
        }

        if (k >= KILO_MIN && k <= KILO_MAX) {
            this.kilometrage = k;
        } else {
            this.kilometrage = KILO_DEFAUT;
        }

        if (p == DEUX_PORTES || p == QUATRE_PORTES) {
            this.nbrPortes = p;
        } else {
            this.nbrPortes = PORTES_DEFAUT;
        }

        if (c != "") {
            this.commentaire = c;
        } else {
            this.commentaire = COMM_DEFAUT;
        }

        if (v == true || v == false) {
            this.vendue = v;
        } else {
            this.vendue = VENDUE_DEF;
        } 

        this.IDnumber = IDnumber + 1; //The problem is that the IDnumber is 1 for every new instance I create, wether I have the this. in front or not.

    }


//HERE I AM SUPPOSED TO PUT IN A CLASS GETTER...I have not managed to make it work in any way

    public static Automobile getAutomobile() {
        return null; //just put a null return in the meantime so I can compile
    }

//GETTERS

    public int getAnnee() {
        return annee;
    } 

    public int getMarque() {
        return marque;
    } 

    public int getCouleur() {
        return couleur;
    } 

    public int getNbrPortes() {
        return p;
    } 

    public double getKilometrage() {
        return k;
    } 
    public boolean getVendue() {
        return v;
    } 
    public String getCommentaire() {
        return c;
    } 
    public boolean getAutomatique() {
        return a;
    } 

//Setters. According to the imposed settings, Not supposed to have any error message or correction in case of wrong input, it just goes right through?

    public void setAnnee ( int annee ) {
        //A COMPLETER
        if (annee >= ANNEE_MIN && annee <= ANNEE_MAX) {
            this.annee = annee;
        }
    }

    public void setMarque (int marque){
       //A COMPLETER
       if (marque >= MARQUE_MIN && marque <= MARQUE_MAX) {
           this.marque = marque;
       }
    }

    public void setCouleur (int couleur) {
      //A COMPLETER
      if (couleur >= COULEUR_MIN && couleur <= COULEUR_MAX){
          this.couleur = couleur;
      }
    }

    public void setNbrPortes (int p) {
        //A COMPLETER
        if (p == DEUX_PORTES || p == QUATRE_PORTES){
            this.p = p;
        }
    }

    public void setKilometrage (double k) {
        //A COMPLETER  
        if (k >= KILO_MIN && k <= KILO_MAX) {
            this.k = k;
        }
    }

    // This is supposed to count if I select a car as sold, and counts how many are sold in total (So I can later show it to the user)

    public void setVendue (boolean v) {

        if (v == true || v == false) {
            this.v = v;
        }
        if (v = true) {
            compteurvendue = compteurvendue + 1;
        }
    }

//Sets comment as long as it is not null
    public void setCommentaire (String c){

        if (c != null) {
            this.c = c;
        }
    }

    public void setAutomatique (boolean a){
        if (a == true || a == false) {
            this.a = a;
        }
    }

   //Supposed to be able to create a clone of the car with all the same variables except the comment and sold that are reverted to default, and the IDnumber which will be a new one (+1)

    public Automobile cloner () {
        Automobile Clone = new Automobile(marque,annee,couleur,a,k,p,COMM_DEFAUT,VENDUE_DEF);
        return Clone;
    }

} // Automobile

我的代码中有多个部分目前无效:

  • 当我在BlueJ环境中测试时,Automobile的第一个构造函数接受我的变量输入,但是布尔值(vendue和automatique)总是出现为FALSE并且注释总是以COMM_DEFAUT(空字符串&#34)出现;&#34;。)

  • 第二个构造函数将我实例中的所有内容都作为默认值,即使我输入了有效值。布尔和评论仍有同样的问题。

  • 我不明白如何创建一个&#34;类getter&#34; ......以及在这种情况下它的目的是什么

  • IDnumber始终显示为1.在创建每个新对象之间,它不会保存在内存中。

  • 我觉得我的克隆方法是正确的,但我不能打电话或测试它甚至创建一个DEFAULT汽车。

***请记住,除了私有变量和getter方法之外,所有setter,构造函数,最终变量和cloner头的头都是强加的,不能修改(这就是这样做的原因)更难)。

我可以做些什么来解决我的代码中遇到的这些不同问题?非常感谢,因为这个社区已经非常有帮助!

1 个答案:

答案 0 :(得分:0)

第一个构造函数自动化和注释:  那里有拼写错误或复制/粘贴问题:)

if (a == true || a == false) {
        **this.automatique = a;**
    } else {
        this.automatique = AUTO_DEF;
    }

尖线做什么?实际上它等于

this.automatique = this.a; 

不是构造函数的自动化参数。

那么有两个变量和自动化的原因是什么? (对其他人也一样)。  那么

if (a == true || a == false)

完全没有意义。 boolean a只能是true或false,那么结果是什么?

所以,删除不需要的重复变量。  然后采用这种模式:

// holder
private int marque;

//constructor: sure there will be all other parameters as well
public Automobile (int marque)    
{
   //if it is needed to check between MIN and MAx do it as in your code. (not for boolean :) )
   this.marque = marque;
}

// getter
public int getMarque()
{
  return this.marque;
}
// setter 
public void setMarque(int marque)
{
   //if it is needed to check between MIN and MAx do it as in your code.
  this.marque = marque;
}

// cloner
public Automobile clone()
{
    // sure there will be all other parameters and logic as well
    Automobile clonedAuto = new Automobile(this.marque);
    return clonedAuto;
}

是的。