如何在重载的构造函数中调用mutator方法,而不是在Java中进行分配?

时间:2019-02-23 06:21:09

标签: java bluej mutators

 public class Player
{
private String firstName;
private String lastName;
private int heightInInches;
private double weightInPounds;
private boolean goalScorer;
private boolean drinksBeer;

public Player(){
}

public Player(String firstName, String lastName, int heightInInches, double weightInPounds, boolean goalScorer, boolean drinksBeer){
    if(lastName != null && lastName.trim().length() > 0){
        if(lastName != null && lastName.trim().length() > 0){
            if(heightInInches >= 0){ 
                if(weightInPounds >= 0){
                    this.firstName = firstName;
                    this.lastName = lastName;
                    this.heightInInches = heightInInches;
                    this.weightInPounds = weightInPounds;
                    this.goalScorer = goalScorer;
                    this.drinksBeer = drinksBeer;

                }
            }
        }
    }
}

public String getFirstName(){
    return firstName;
}

public String getLastName(){
    return lastName;
}

public int getHeightInInches(){
    return heightInInches;
}

public double getWeightInPounds(){
    return weightInPounds;
}

public boolean getGoalScorer(){
    return goalScorer;
}

public boolean getDrinksBeer(){
    return drinksBeer;
}

public void setFirstName(String firstName){
    if(firstName != null && firstName.trim().length() > 0){
        this.firstName = firstName;
    }else{
        System.out.println("Error. Invalid First Name.");
    }

}

public void setLastName(String lastName){
    if(lastName != null && lastName.trim().length() > 0){
        this.lastName = lastName;
    }else{
        System.out.println("Error. Invalid Last Name.");
    }

}

public void setHeightInInches(int heightInInches){
    if(heightInInches >= 0){
        this.heightInInches = heightInInches;
    }else{
        System.out.println("Error. Invalid Height.");
    }

}

public void setWeightInPounds(double weightInPounds){
    if(weightInPounds >= 0){
        this.weightInPounds = weightInPounds;
    }else{
        System.out.println("Error. Invalid Weight.");
    }

}

public void setGoalScorer(boolean goalScorer){
    this.goalScorer = goalScorer;
}

public void setDrinksBeer(boolean drinksBeer){
    this.drinksBeer = drinksBeer;
}


}

在重载的构造函数中,如何为每个字段调用mutator方法而不是使用赋值语句?并且如果我调用mutator方法,是否应该在构造函数中删除if语句?  (我正在使用blueJ) 我是一个初学者,所以请提及我的代码中是否还有其他问题。 提前致谢。

1 个答案:

答案 0 :(得分:0)

您所要做的就是像其他方法一样引用mutator方法:

public Player(String firstName, String lastName, int heightInInches, double weightInPounds, boolean goalScorer, boolean drinksBeer) {
    setFirstName(firstName);
    setLastName(lastName);
    setHeightInInches(heightInInches);
    setWeightInPounds(weightInPounds);
    setGoalScorer(goalScorer);
    setDrinksBeer(drinksBeer);
}

此外,也不需要那些if语句,因为它们实际上并没有做任何有用的事情。如果您未明确设置任何值,则int变量将默认为0double将默认为0.0boolean将默认为{{1 }}和false将默认为String。因此,如果您的if语句执行的操作是“如果名字是null,请不要设置它……因此默认情况下,它只会是null”。

如果要执行诸如将高度强制为正数之类的操作,则可以始终将此逻辑放入设置器中。