If/else statements inside a java constructor

北战南征 提交于 2020-01-24 11:18:14

问题


This program wasn't working when I only had the if/else conditions in the Setters. 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?

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

回答1:


Your best bet is to put the if/else statements in the setters and use the setters from within the constructor. That way you have your logic in exactly one place and it's much easier to maintain.




回答2:


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!

The reason they don't work within the constructor is because you need to do this.quantity = 0; as opposed to quantity = 0;

Example to call setters from within constructor:

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



回答3:


putting if/else statements inside of both the constructor and setters are valid often used. 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.

Your constructor's if/else blocks still have a bug.

  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

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.




回答4:


. 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?

Even if you put it in constructor you will need them in setters as well.

BTW the if/else statements are validations



来源:https://stackoverflow.com/questions/13202672/if-else-statements-inside-a-java-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!