简体   繁体   English

在另一个构造函数中调用一个构造函数(并从cons.1获取修改后的变量)

[英]Calling a constructor in another constructor (and getting the modified variables from cons.1)

I searched the web but did not find my answers. 我在网上搜索,但没有找到答案。 Here is my question. 这是我的问题。 The goal here is to use my verifications (if codeForme<0 || codeforme>9)... etc only in my first constructor, because my two other constructors are calling the first one. 这里的目标是仅在第一个构造函数中使用我的验证(如果codeForme <0 || codeforme> 9)等,因为其他两个构造函数正在调用第一个构造函数。

But, when i enter for example codeforme = 20 in my second constructor, it is not modified as it should be after running the first constructor. 但是,当我在第二个构造函数中输入例如codeforme = 20时,它不会像运行第一个构造函数后那样进行修改。 I know the problem is probably from the this.codeForme = forme because it takes my parameter's forme instead of the codeForme from the first. 我知道问题可能出在this.codeForme = forme,因为它使用了我参数的forme而不是第一个参数的codeForme。 There should be an easy way, thanks! 应该有一个简单的方法,谢谢!

Here is my code so far: 到目前为止,这是我的代码:

public class carteMere {

    // variable(s) de classe s'il y a lieu
    private static int identifiant = 0;
    // variable(s) d'instance s'il y a lieu
    private String marque;
    private int codeForme = 0;
    private int capaciteMaxMemoire = 8;
    private int memoireInstalle = 0;
    private int codeCarte;
    // constructeur(s) s'il y a lieu
    public carteMere( String marque,
                      int codeForme,
                      int capaciteMaxMemoire,
                      int memoireInstalle) {
        this.marque = marque;
        this.identifiant = identifiant+1;
        this.codeCarte=this.identifiant;
        if(codeForme < 0 || codeForme > 9){
            codeForme = 0;
        }
        if(capaciteMaxMemoire<=0){
            capaciteMaxMemoire = 8;
        }
        if(memoireInstalle < 0){
            memoireInstalle = 0;
        }else if (memoireInstalle> capaciteMaxMemoire){
            memoireInstalle = capaciteMaxMemoire;
        }
        codeForme = codeForme;
        capaciteMaxMemoire = capaciteMaxMemoire;
        memoireInstalle = memoireInstalle;
    }
    public carteMere(int forme, int capaciteMax, int memoireInstalle){
        this("ASUS", forme, capaciteMax, memoireInstalle);
        this.codeForme = forme;
        this.capaciteMaxMemoire = capaciteMax;
        this.memoireInstalle = memoireInstalle;
        this.codeCarte=this.identifiant;
    }
    public carteMere(int codeForme, int memoireInstalle){
        this(codeForme, 8, memoireInstalle);
        this.codeForme=codeForme;
        this.memoireInstalle=memoireInstalle;
        this.identifiant = identifiant;
        this.codeCarte=this.identifiant;
    }

Edit: 编辑:

If I do: carte1 = new carteMere(10, 8); 如果我这样做:carte1 = new carteMere(10,8);

Then I inspect carte1, the codeForme is 10, and it should be 0 because it should have been modifed in the 1st constructor. 然后,我检查carte1,codeForme为10,并且应该为0,因为它应该已经在第一个构造函数中进行了修改。

Use this to qualify your instance variables as you have done for marque , indentifiant , and codeCarte . 使用this来限定实例变量,就像对marqueindentifiantcodeCarte Also use else on your if s to assign the variable the normal value if not outside the validation range. 还可以使用else的你if s到如果没有验证范围外的变量分配的正常价值。 Eg for codeForme : 例如codeForme

if(codeForme < 0 || codeForme > 9) {
    this.codeForme = 0;
}
else {
    this.codeForme = codeForme;
}

And similarly for capaciteMaxMemoire and memoireInstalle . 同样地,对于capaciteMaxMemoirememoireInstalle

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

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