Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果OOP为空,如何设置位置_Java_Oop_Getter Setter - Fatal编程技术网

Java 如果OOP为空,如何设置位置

Java 如果OOP为空,如何设置位置,java,oop,getter-setter,Java,Oop,Getter Setter,如果最初没有输入位置,我如何让它打印“整栋房子”字样。我认为它不起作用,因为在创建任务时,它使用的构造函数没有位置指示。有没有办法将位置从null更改为我指定的字符串 这是我的任务类: public class Task{ private final String choreName; private final int maxCompletionInMinutes; private final String difficulty; private Strin

如果最初没有输入位置,我如何让它打印“整栋房子”字样。我认为它不起作用,因为在创建任务时,它使用的构造函数没有位置指示。有没有办法将位置从null更改为我指定的字符串

这是我的任务类:

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

}
这是我的驾驶课:

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中的null检查逻辑


这也很好,因为现在您还可以将
位置
作为最后一个字段,使
任务
类不可变且线程安全

我建议使用空字符串而不是
null
来指示整个内部任务。这样做了。非常感谢!:)