简体   繁体   English

对象创建中的构造方法,设置方法,获取方法问题

[英]Constructor, Setter, Getter issue in an object creation

I am creating a class to make cars, based on multiple variables such as year (annee), brand (marque), color (couleur) odometer (kilometrage), sold (vendue), transmission (automatique), comment (commentaire) and number of doors (nbrPortes). 我正在基于多个变量来创建汽车制造类,例如年份(annee),品牌(marque),颜色(couleur)里程表(kilometrage),已售出(vendue),变速器(automatique),评论(commentaire)和编号门(nbrPortes)。

The arguments inside the code are in French because I am in a French university, so please bear with me. 代码中的参数是法语的,因为我在法国的一所大学,所以请多多包涵。

My code needs to have the setters, the constructors and the cloner methods exactly as they are right now (imposed). 我的代码需要完全符合设置器,构造器和克隆器方法的要求(强制)。 I made the getters and the incrementation for IDnumber and compteurvendue (number of sold cars) myself. 我自己做吸气剂,并自己增加了IDnumber和compteurvendue(售出汽车的数量)。

Here is what I have as code: 这是我的代码:

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

There are multiple parts of my code that are currently not working: 我的代码有多个部分当前无法正常工作:

  • The first constructor for Automobile accepts my variable inputs when I test it in BlueJ environment, but the booleans (vendue and automatique) always come out as FALSE and the comment always comes out as COMM_DEFAUT (empty string ""). 当我在BlueJ环境中测试汽车时,第一个Automobile构造函数会接受我的变量输入,但是布尔值(vendue和automatique)始终以FALSE形式出现,注释始终以COMM_DEFAUT形式(空字符串“”)出现。

  • The second constructor brings everything out in my instance as the default value, even if I input valid values. 第二个构造函数将实例中的所有内容都作为默认值显示出来,即使我输入了有效值也是如此。 the booleans and comment still have the same issue. 布尔值和注释仍然存在相同的问题。

  • I don't understand how to create a "class getter"...and what its purpose would be in this case 我不明白如何创建“类获取器” ...在这种情况下其目的是什么

  • The IDnumber always comes out as 1. It doesn't get saved in the memory between my creating each new object. IDnumber始终显示为1。在创建每个新对象之间,它不会保存在内存中。

  • I feel like my cloner method is correct, but I cannot call on it or test it to create even a DEFAULT car. 我感觉自己的克隆器方法正确,但是我无法调用它或对其进行测试以甚至无法创建DEFAULT汽车。

***Please keep in mind that aside from the private variables and the getter methods, the headers of all the setters, constructors, the final variables and the cloner header are imposed and cannot be modified (which is what makes this so much harder). ***请记住,除了私有变量和getter方法之外,所有设置方法,构造方法,最终变量和克隆器标头的标头都是强制性的,无法修改(这使这变得更加困难) 。

What can I do to solve these different problems I'm having with my code? 如何解决我的代码遇到的这些不同问题? Thank you very much as this community has already been extremely helpful ! 非常感谢您,因为这个社区已经非常有用!

first constructor automatique and comments: there as typo or copy/paste issue :) 第一个构造函数自动生成和注释:存在拼写错误或复制/粘贴问题:)

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

pointed line does what? 尖线是什么? actually it is equal to 实际上等于

this.automatique = this.a; 

not automatique parameter from constructor. 不是构造函数的自动参数。

then what is the reason to have two variables a and automatique at all? 那么,为什么要完全拥有两个变量a和automatique呢? (same for others as well). (其他人也一样)。 then 然后

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

has no sense at all. 完全没有意义 boolean a can be only true or false so what is a result of that if? 布尔值a只能是true或false,那么if的结果是什么?

so, remove unneeded duplicate variables. 因此,删除不需要的重复变量。 then go this pattern: 然后走这种模式:

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

that's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM