如果我理解正确,您的课程如下所示:
class Appointment {
private Date date;
private Time time;
}
class Date {
private String day;
private String month;
private int year;
}
class Time {
private int hour;
private int minutes;
}
并且您已经有一个时间和日期实例,每个实例都设置了它们的值,现在您想在约会类中设置这些日期和时间值吗?
如果是,您可以使用 Setter 或构造函数。
如果您使用 setter,您的 Appointment 类将如下所示:
class Appointment {
private Date date;
private Time time;
public void setDate(Date date){
this.date = date;
}
public void setTime(Time time) {
this.time = time;
}
}
然后你会像上面这样使用:
public static void main(String[] args){
Date yourDate = new Date("Monday",2,1993); //the date object which you already have
Time yourTime = new Time(5,6); // the time object that you already have
Appointment yourAppointment = new Appointment(); //creating an empty Appointment object
yourAppointment.setDate(yourDate); //setting your created date in appointment
yourAppointment.setTime(yourTime); //setting your created time in appointment
}
或者您可以在约会类中使用构造函数而不是设置器。
现在约会类看起来像:
class Appointment {
private Date date;
private Time time;
private Appointment(Date date, Time time){
this.date = date;
this.time = time;
}
}
然后您可以像这样设置日期和时间值:
public static void main(String[] args){
Date yourDate = new Date("Monday",2,1993); //the date object which you already have
Time yourTime = new Time(5,6); // the time object that you already have
Appointment yourAppointment = new Appointment(yourDate,yourTime);
}