【问题标题】:Calling a constructor in another constructor (and getting the modified variables from cons.1)在另一个构造函数中调用构造函数(并从 cons.1 获取修改后的变量)
【发布时间】:2014-12-02 22:31:20
【问题描述】:

我搜索了网络,但没有找到我的答案。这是我的问题。 这里的目标是仅在我的第一个构造函数中使用我的验证(如果 codeForme9)...等,因为我的其他两个构造函数正在调用第一个。

但是,当我在我的第二个构造函数中输入例如 codeforme = 20 时,它没有被修改,因为它应该在运行第一个构造函数之后。我知道问题可能出在 this.codeForme = forme 上,因为它采用了我的参数的形式,而不是第一个的 codeForme。 应该有一个简单的方法,谢谢!

到目前为止,这是我的代码:

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

编辑:

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

然后我检查carte1,codeForme是10,它应该是0,因为它应该在第一个构造函数中被修改过。

【问题讨论】:

  • 当我使用第二个(这个调用第一个)时,有没有办法通过第一个构造函数的验证。或者我只是复制粘贴这些 if/else ?谢谢
  • 您首先应该与您的命名保持一致:codeForme 和 forme 在您的不同构造函数中用于相同的参数,这有点令人困惑且难以阅读。在设置实例变量时,您还应该始终使用“this”,而在第一个构造函数的末尾则不要这样做。也许有了这个,您将能够更好地阅读您的代码并了解正在发生的事情

标签: java class constructor this instance


【解决方案1】:

使用this 来限定实例变量,就像对marqueindentifiantcodeCarte 所做的那样。如果不在验证范围之外,还可以在 ifs 上使用 else 为变量分配正常值。例如。对于codeForme

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

capaciteMaxMemoirememoireInstalle 也是如此。

【讨论】:

  • 谢谢,但它并没有解决问题我在调用第二个或第三个构造函数时仍然找不到正确的修改变量。
  • 让第一个构造函数完成分配实例变量的所有工作。它不能修改其他构造函数传入的参数,因为它获得了这些变量的传递副本。在第一个构造函数中修改这些值只会修改本地副本,因此更改不会显示在其他构造函数的值中。在您使用this 调用第一个构造函数的其他构造函数中,删除尝试将值分配给实例变量的行;让第一个构造函数分配所有内容。
  • 所以没有办法跳过必须在其他构造函数中编写相同的验证 if/else?
  • 我不会这么说的。只需让第一个构造函数完成所有验证和实例变量赋值,这样其他构造函数的参数值不变就无关紧要了。
  • 是的,我设法让它工作,感谢你。我不得不删除这个。在我的 if/else 中。这。在第一个构造函数的每个实例中分配。而其他 2 个构造函数只有 this(x,y,z,etc.);瞧
猜你喜欢
  • 2011-03-24
  • 2010-12-15
  • 2018-10-06
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 2016-07-03
  • 2014-03-12
  • 2012-06-22
相关资源
最近更新 更多