JAVA - 使用 getter 和 setter 为空值执行条件 if 语句
JAVA - Execute a conditional if statement for a null value using getters and setters
我在使用 getter 和 setter 返回值 null
并将其替换为另一个 class 有意义的内容时遇到问题,我想知道我做错了什么。
getter 和 setter 的配置:
public class CarClass {
private String make;
private String model;
private int yearOfMake;
public void setMake(String make){
if (make.isEmpty()){
this.make = "";
System.out.println("Error please input value");
} else {
System.out.println("Hello");
this.make = make;
}
}
public String getMake(){
return make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
if (model.isEmpty()){
this.model = "";
System.out.println("Error, you have not entered a valid model");
} else {
this.model = model;
}
}
public int getYearOfMake() {
return yearOfMake;
}
public void setYearOfMake(int yearOfMake) {
if (yearOfMake > 1900)
this.yearOfMake = yearOfMake;
else {
this.yearOfMake = yearOfMake;
System.out.println("Year of make is invalid");
}
}
}
上面的执行脚本:
public class GetterSetter {
public static void main(String[] args) {
CarClassTwo bmw = new CarClassTwo();
bmw.setMake();
System.out.println(bmw.getMake());
CarClass benz = new CarClass();
benz.setModel("C300");
System.out.println(benz.getModel());
benz.setYearOfMake(1999);
System.out.println(benz.getYearOfMake());
System.out.println("!!!!!!!!");
benz.setModel("C300");
System.out.println(benz.getModel());
benz.setYearOfMake(1800);
System.out.println(benz.getYearOfMake());
System.out.println(bmw.getMake());
System.out.println(bmw.getModel());
}
}
如果能帮助我理解我做错了什么,将不胜感激。
当执行以下代码时:
CarClassTwo bmw = new CarClassTwo();
bmw.setMake();
当时
的价值观
private String make;
private String model;
默认初始化为null。
所以当bmw.setMake();
执行时,执行if (make.isEmpty()){
类似于 null.isEmpty() --> 将抛出 NullPointerException。
我在使用 getter 和 setter 返回值 null
并将其替换为另一个 class 有意义的内容时遇到问题,我想知道我做错了什么。
getter 和 setter 的配置:
public class CarClass {
private String make;
private String model;
private int yearOfMake;
public void setMake(String make){
if (make.isEmpty()){
this.make = "";
System.out.println("Error please input value");
} else {
System.out.println("Hello");
this.make = make;
}
}
public String getMake(){
return make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
if (model.isEmpty()){
this.model = "";
System.out.println("Error, you have not entered a valid model");
} else {
this.model = model;
}
}
public int getYearOfMake() {
return yearOfMake;
}
public void setYearOfMake(int yearOfMake) {
if (yearOfMake > 1900)
this.yearOfMake = yearOfMake;
else {
this.yearOfMake = yearOfMake;
System.out.println("Year of make is invalid");
}
}
}
上面的执行脚本:
public class GetterSetter {
public static void main(String[] args) {
CarClassTwo bmw = new CarClassTwo();
bmw.setMake();
System.out.println(bmw.getMake());
CarClass benz = new CarClass();
benz.setModel("C300");
System.out.println(benz.getModel());
benz.setYearOfMake(1999);
System.out.println(benz.getYearOfMake());
System.out.println("!!!!!!!!");
benz.setModel("C300");
System.out.println(benz.getModel());
benz.setYearOfMake(1800);
System.out.println(benz.getYearOfMake());
System.out.println(bmw.getMake());
System.out.println(bmw.getModel());
}
}
如果能帮助我理解我做错了什么,将不胜感激。
当执行以下代码时:
CarClassTwo bmw = new CarClassTwo();
bmw.setMake();
当时
的价值观private String make;
private String model;
默认初始化为null。
所以当bmw.setMake();
执行时,执行if (make.isEmpty()){
类似于 null.isEmpty() --> 将抛出 NullPointerException。