将 setDate 用于用户定义 class.. Java
Using setDate for User defined class.. Java
我创建了一个用户定义的 class 日期,具有 3 个属性,日、月、年,然后我将日期 class 调用到我的约会 class 中,它变成了私人日期date.. 现在我需要使用 setters 和 getters.. 我知道如何为单个属性创建 setters 和 getters 但对于整个 class 就像 setDate,我调用了我的约会 class,我不知道知道怎么做。这就是我尝试做的,但值没有改变
ArrayList<Appointment> ai= new ArrayList();
Appointment ap= new Appointment(date,time);
ap.setDoctor_id("1");
ap.setMedication("w");
ap.setPatient_id("3");
ap.setProblem("pro");
ap.setRoom("ss");
ai.add(ap);
Appointment o=null;
try
{
//Creating a deep clone of ap and assigning it to o
o = (Appointment) ap.clone1();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
date.setDay(4);
date.setMonth(9);
date.setYear(2000);
o.setDate(date);
o.setRoom("pp");
o.setProblem("Happiness");
o.setMedication("Anti-Happiness pills");
ai.add(o);
Appointment t= (Appointment) o.clone1();
date.setDay(1);
date.setMonth(12);
date.setYear(18);
t.setDate(date);
t.setRoom("p");
t.setProblem("depressin");
t.setMedication("Anti-depression pills");
ai.add(t);
你知道如何使用 setter 表示整数,你会这样做:
private int setDay(int day) {
this.day = day
}
您还知道如何创建新对象:
Appointment ap= new Appointment(date,time);
对于可能看起来像的构造函数:
public Appointment(Date date, Time time) {
this.date = date;
this.time = time;
}
因此,如果您的日期构造函数类似于:
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
您可以使用与预约新约会类似的方式来预约新日期:
Date newDate = new Date(day, month, year)
你可以用同样的方法制作一个setter!它看起来像:
private setDate(int day, int month, int year) {
this.date = new Date(day, month, year);
}
或:
private setDate(Date date) {
this.date = date;
}
对于上面的那个,你必须先确定日期,这样你就可以这样称呼它:
Date newDate = new Date(day,month,year);
setDate(newDate);
我创建了一个用户定义的 class 日期,具有 3 个属性,日、月、年,然后我将日期 class 调用到我的约会 class 中,它变成了私人日期date.. 现在我需要使用 setters 和 getters.. 我知道如何为单个属性创建 setters 和 getters 但对于整个 class 就像 setDate,我调用了我的约会 class,我不知道知道怎么做。这就是我尝试做的,但值没有改变
ArrayList<Appointment> ai= new ArrayList();
Appointment ap= new Appointment(date,time);
ap.setDoctor_id("1");
ap.setMedication("w");
ap.setPatient_id("3");
ap.setProblem("pro");
ap.setRoom("ss");
ai.add(ap);
Appointment o=null;
try
{
//Creating a deep clone of ap and assigning it to o
o = (Appointment) ap.clone1();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
date.setDay(4);
date.setMonth(9);
date.setYear(2000);
o.setDate(date);
o.setRoom("pp");
o.setProblem("Happiness");
o.setMedication("Anti-Happiness pills");
ai.add(o);
Appointment t= (Appointment) o.clone1();
date.setDay(1);
date.setMonth(12);
date.setYear(18);
t.setDate(date);
t.setRoom("p");
t.setProblem("depressin");
t.setMedication("Anti-depression pills");
ai.add(t);
你知道如何使用 setter 表示整数,你会这样做:
private int setDay(int day) {
this.day = day
}
您还知道如何创建新对象:
Appointment ap= new Appointment(date,time);
对于可能看起来像的构造函数:
public Appointment(Date date, Time time) {
this.date = date;
this.time = time;
}
因此,如果您的日期构造函数类似于:
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
您可以使用与预约新约会类似的方式来预约新日期:
Date newDate = new Date(day, month, year)
你可以用同样的方法制作一个setter!它看起来像:
private setDate(int day, int month, int year) {
this.date = new Date(day, month, year);
}
或:
private setDate(Date date) {
this.date = date;
}
对于上面的那个,你必须先确定日期,这样你就可以这样称呼它:
Date newDate = new Date(day,month,year);
setDate(newDate);