如何在重载的构造函数中调用 mutator 方法而不是在 java 中分配它们?
How to call the mutator methods in an overloaded constructor instead of assigning them in java?
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 方法而不是使用赋值语句?另外,如果我调用增变器方法,我是否应该删除构造函数中的 if 语句?
(我正在使用 blueJ)
我是初学者,所以请提及我的代码中是否还有其他问题。
提前致谢。
您所要做的就是像任何其他方法一样引用增变器方法:
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
变量将默认为 0
,double
将默认为 0.0
,boolean
将默认为 false
,String
将默认为 null
。所以你的 if 语句正在做 "If the first name is null
, don't set it . . . so it'll just be null
anyway by default."
如果你想做一些事情,比如强制高度为正数,你总是可以把那个逻辑放在 setter.
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 方法而不是使用赋值语句?另外,如果我调用增变器方法,我是否应该删除构造函数中的 if 语句? (我正在使用 blueJ) 我是初学者,所以请提及我的代码中是否还有其他问题。 提前致谢。
您所要做的就是像任何其他方法一样引用增变器方法:
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
变量将默认为 0
,double
将默认为 0.0
,boolean
将默认为 false
,String
将默认为 null
。所以你的 if 语句正在做 "If the first name is null
, don't set it . . . so it'll just be null
anyway by default."
如果你想做一些事情,比如强制高度为正数,你总是可以把那个逻辑放在 setter.