gpt4 book ai didi

java - 确定任务是否较早,同时只知道日期是否较早以及时间是否较早

转载 作者:行者123 更新时间:2023-12-02 11:17:02 25 4
gpt4 key购买 nike

名为 Time 的类包含一个方法 isEarlier(Time): boolean,如果 Time 则返回 true > 早于作为参数传递的 Time 对象,否则返回 false

名为 Date 的第二个类包含类似的方法 isEarlier(Date): boolean,如果 Date 早于作为参数传递的 Date 对象,否则返回 false

名为Task的第三个类有两个数据字段Date dateTime time。该类还包含一个名为 isEarlier(Task): boolean 的方法,如果 Task 的调度早于 Task,则该方法返回 true 作为参数传递。此方法如何仅使用 Time 和 Date 对象 中的 isEalier() 方法来确定任务是否较早?

我认为在不知道日期是否相同的情况下这是不可能的,这是不可能的,因为 isEarlier() 方法仅返回 true。我说得对吗?

这更多的是逻辑问题,而不是特定于语言的问题。

public boolean isEarlier(Task argTask) {
if (date.isEarlier(argTask.date)) {
return true; // date is earlier
} else if ("Is this possible without equality comparison here?")
// date is the same, so we must also check the time
return time.isEarlier(argTask.time);
} else {
return false; // date is later
}
}

最佳答案

如果时间类中没有相等比较方法,您还必须使用 isEarlier 进行反向比较,以决定何时比较 time 字段。

public boolean isEarlier(Task argTask) {
if(this.date.isEarlier(argTas.date)) {
return true;
} else if(argTask.date.isEarlier(this.date)) {
return false;
} else if(this.time.isEarlier(argTask.time)) { //reached if date fields are equal
return true;
}

//At this stage, either `argTask.time.isEarlier(this.time)` returns true
//or date and time in both objects are exactly equal
return false;
}

关于java - 确定任务是否较早,同时只知道日期是否较早以及时间是否较早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50198096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com