- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
实现父类(super class)约会和子类一次性、每日和每月。
预约有描述(例如,“看牙医”)和日期。
编写一个方法 events On(intyear,intmonth,intday) 来检查约会是否发生在该日期。
例如,对于每月的约会,您必须检查该月的某一天是否匹配,然后用约会的混合填充 Appointment 对象的数组。
让用户输入一个日期并打印出该日期发生的所有约会。
public class Appointment {
private String description;
private Date date;
public Appointment(String description, int day, int month, int year)
throws ParseException {
this.description=description;
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("dd/MM/yyyy");
this.setDate(simpleDateFormat.parse(String.format("%d/%d/%d", day,
month, year)));
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return String.format("Your Appointment is : %s. On(date): ",
this.getDescription(), this.getDate()
.toString());
}
public boolean occursOn(int year, int month, int day) throws
ParseException {
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("dd/MM/yyyy");
Date date = simpleDateFormat.parse(String.format("%d/%d/%d", day,
month, year));
return this.getDate().equals(date);
}
}
最佳答案
将Appointment
基类与occurrsOn
方法一起抽象化。同时删除 date
变量,因为某些子类不需要它:
public abstract class Appointment {
private String description;
public Appointment(String description)
this.description=description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public abstract boolean occursOn(int year, int month, int day) throws
ParseException;
}
现在我们可以开始创建子类,它将实现 occurrsOn
方法。对于 Onetime
子类,它将与原始 Appointment
类基本相同,因此我现在不会编写它。相反,让我们看一下每月
,例如:
public class Monthly extends Appointment {
private String description;
private int onThisDay; //on this day of the month is the appointment
public Appointment(String description, int onThisDay) {
this.description=description;
this.onThisDay = onThisDay;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
//override tostring as well
@Override
public String toString() {
return "Your " + description + " appointment is on the " +
onThisDay + ". day of the month";
}
//implement our abstract method, month and year params have no importance here
public boolean occursOn(int year, int month, int day) throws
ParseException {
return day == onThisDay; //just check if day matches
}
}
关于如何填写约会列表,这里是:
List<Appointment> appointments = new ArrayList<>();
//2 appointments for example
appointments.addAll(new Onetime("Doctor",2019,07,21), new Monthly("Dentist",11));
appointments.forEach(app -> System.out.println(app.toString())); //print all
关于java - 如何在约会类中创建列表以及如何编写测试用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360826/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!