如果空 OOP 如何设置位置
How to set location if null OOP
如果最初未输入该位置,我如何才能打印 "whole house" 字样。我认为它不起作用,因为创建任务时它使用的是没有位置说明的构造函数。有没有办法让location从null变成我指定的字符串?
这是我的任务 class:
public class Task{
private final String choreName;
private final int maxCompletionInMinutes;
private final String difficulty;
private String location;
public Task(String choreName, int maxCompletionInMinutes, String difficulty){
this.choreName = choreName;
this.maxCompletionInMinutes = maxCompletionInMinutes;
this.difficulty = difficulty;
}
public Task(String choreName, int maxCompletionInMinutes, String difficulty, String location){
this(choreName, maxCompletionInMinutes, difficulty);
this.location = location;
}
public String getChoreName(){
return choreName;
}
public int getMaxCompletionInMinutes(){
return maxCompletionInMinutes;
}
public String getDifficulty(){
return difficulty;
}
public String getLocation(){
return location;
}
public void setLocation(String location){
if(location == null)
location = "Whole house";
this.location = location;
}
public void prettyPrint(){
System.out.printf("Complete the chore: %s (%s) in location: %s. This chore should take a maximum of %d minutes.%n", choreName, difficulty, location, maxCompletionInMinutes);
}
}
这是我的 driver class:
public class Assignment5{
public static void main (String[] args){
Task task1 = new Task("Dishes", 40, "Hard", "Kitchen");
Task task2 = new Task("Dust", 45, "Hard");
Task task3 = new Task("Vacuum", 30, "Medium");
Task task4 = new Task("Make beds", 15, "Easy");
Task task5 = new Task("Water plants", 15, "Easy", "Living Room");
task1.prettyPrint();
task2.prettyPrint();
task3.prettyPrint();
task4.prettyPrint();
task5.prettyPrint();
}
}
我可能只是调换构造函数的顺序。使三参数构造函数调用四参数构造函数并提供默认值:
public Task(String choreName, int maxCompletionInMinutes, String difficulty) {
this(choreName, maxCompletionInMinutes, difficulty, "Whole house");
}
public Task(String choreName, int maxCompletionInMinutes, String difficulty, String location) {
this.choreName = choreName;
this.maxCompletionInMinutes = maxCompletionInMinutes;
this.difficulty = difficulty;
this.location = location;
}
然后只需删除 setter 中的空检查逻辑即可。
这也很好,因为现在您也可以将 location
设置为最终字段,从而使 Task
class 不可变且线程安全
如果最初未输入该位置,我如何才能打印 "whole house" 字样。我认为它不起作用,因为创建任务时它使用的是没有位置说明的构造函数。有没有办法让location从null变成我指定的字符串?
这是我的任务 class:
public class Task{
private final String choreName;
private final int maxCompletionInMinutes;
private final String difficulty;
private String location;
public Task(String choreName, int maxCompletionInMinutes, String difficulty){
this.choreName = choreName;
this.maxCompletionInMinutes = maxCompletionInMinutes;
this.difficulty = difficulty;
}
public Task(String choreName, int maxCompletionInMinutes, String difficulty, String location){
this(choreName, maxCompletionInMinutes, difficulty);
this.location = location;
}
public String getChoreName(){
return choreName;
}
public int getMaxCompletionInMinutes(){
return maxCompletionInMinutes;
}
public String getDifficulty(){
return difficulty;
}
public String getLocation(){
return location;
}
public void setLocation(String location){
if(location == null)
location = "Whole house";
this.location = location;
}
public void prettyPrint(){
System.out.printf("Complete the chore: %s (%s) in location: %s. This chore should take a maximum of %d minutes.%n", choreName, difficulty, location, maxCompletionInMinutes);
}
}
这是我的 driver class:
public class Assignment5{
public static void main (String[] args){
Task task1 = new Task("Dishes", 40, "Hard", "Kitchen");
Task task2 = new Task("Dust", 45, "Hard");
Task task3 = new Task("Vacuum", 30, "Medium");
Task task4 = new Task("Make beds", 15, "Easy");
Task task5 = new Task("Water plants", 15, "Easy", "Living Room");
task1.prettyPrint();
task2.prettyPrint();
task3.prettyPrint();
task4.prettyPrint();
task5.prettyPrint();
}
}
我可能只是调换构造函数的顺序。使三参数构造函数调用四参数构造函数并提供默认值:
public Task(String choreName, int maxCompletionInMinutes, String difficulty) {
this(choreName, maxCompletionInMinutes, difficulty, "Whole house");
}
public Task(String choreName, int maxCompletionInMinutes, String difficulty, String location) {
this.choreName = choreName;
this.maxCompletionInMinutes = maxCompletionInMinutes;
this.difficulty = difficulty;
this.location = location;
}
然后只需删除 setter 中的空检查逻辑即可。
这也很好,因为现在您也可以将 location
设置为最终字段,从而使 Task
class 不可变且线程安全