简体   繁体   English

if / else语句在java构造函数中

[英]If/else statements inside a java constructor

This program wasn't working when I only had the if/else conditions in the Setters. 当我在Setters中只有if / else条件时,该程序无效。 I got a tip that I have to use them inside the Constructors too. 我得到了一个提示,我必须在构造函数中使用它们。 Can someone explain to me.. why? 有人可以向我解释..为什么?

Another Question: Do you place the if/else statements inside the Constructor or Setters? 另一个问题:您是否将if / else语句放在Constructor或Setters中?

//Constructor //构造函数

   public Invoice(String partNumber, String partDescription, int quantity,
        double pricePerItem) {
    super();
    this.partNumber = partNumber;
    this.partDescription = partDescription;

    if (quantity <= 0)
        quantity = 0;
    else
        this.quantity = quantity;

    if (pricePerItem <= 0)
        pricePerItem = 0.0;
    else
        this.pricePerItem = pricePerItem;
}

//Setters //塞特斯

  public void setQuantity(int quantity) {
    if (quantity <= 0)
        this.quantity = 0;
    else
        this.quantity = quantity;
}

public double getPricePerItem() {
    return pricePerItem;
}

public void setPricePerItem(double pricePerItem) {

    if (pricePerItem != 0.0)
        this.pricePerItem = 0.0;

    else
        this.pricePerItem = pricePerItem;
}

You need to put them in the constructors or the data could be invalid there as well. 您需要将它们放在构造函数中,否则数据也可能无效。 Of course you can avoid redundant code by calling the setters from within the Constructor! 当然,您可以通过在构造函数中调用setter来避免冗余代码!

The reason they don't work within the constructor is because you need to do this.quantity = 0; 他们在构造函数中不起作用的原因是因为你需要这样做this.quantity = 0; as opposed to quantity = 0; quantity = 0;

Example to call setters from within constructor: 从构造函数中调用setter的示例:

public Invoice(String partNumber, String partDescription, int quantity,
               double pricePerItem) {
    super();
    this.partNumber = partNumber;
    this.partDescription = partDescription;

    // call Setter methods from within constructor
    this.setQuantity(quantity);
    this.setPricePerItem(pricePerItem);
}

Your best bet is to put the if/else statements in the setters and use the setters from within the constructor. 最好的办法是将if / else语句放在setter中,并使用构造函数中的setter。 That way you have your logic in exactly one place and it's much easier to maintain. 这样你就可以将你的逻辑放在一个地方,而且维护起来要容易得多。

putting if/else statements inside of both the constructor and setters are valid often used. 把if / else语句放在构造函数和setter中都经常使用。 It ensures that the object is never in an invalid state. 它确保对象永远不会处于无效状态。

As John3136 pointed out in the comments, its a good idea to call the setters from your constructor to reduce the amount of duplicate code. 正如John3136在评论中指出的那样,从构造函数中调用setter是一个好主意,以减少重复代码的数量。

Your constructor's if/else blocks still have a bug. 您的构造函数的if / else块仍然存在错误。

  if (quantity <= 0)
    quantity = 0;    //  <-- this is setting the parameter passed to 0
else
    this.quantity = quantity;  // <-- this.quantity is only ever set if quantity is > 0.

You are going to want to change the body of the if to be this.quantity or remove the else and just always perform the this.quantity = quantity assignment after the if 你会想改变的身体ifthis.quantity或删除else ,只是一直执行this.quantity = quantity后转让if

design suggestion: consider throwing an IllegalArgumentException when you receive a quantity < 0 or price < 0 instead of just defaulting to 0. It would depend on your specific requirements, but creating an invoice for -1 objects seems like it should be an error. 设计建议:当收到数量<0或价格<0而不是默认为0时,考虑抛出IllegalArgumentException。这取决于您的具体要求,但为-1对象创建发票似乎应该是一个错误。

. I got a tip that I have to use them inside the Constructors too. 我得到了一个提示,我必须在构造函数中使用它们。 Can someone explain to me.. why? 有人可以向我解释..为什么?

Most likely to avoid code duplication. 最有可能避免代码重复。

Another Question: Do you place the if/else statements inside the Constructor or Setters? 另一个问题:您是否将if / else语句放在Constructor或Setters中?

Even if you put it in constructor you will need them in setters as well. 即使你把它放在构造函数中,你也需要它们在setters中。

BTW the if/else statements are validations BTW if/else statements验证

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

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