2017-03-18 5 views
0

私は、3つの属性、曜日、月、年を持つユーザ定義クラスDateを作成してから、私のAppointmentクラスにDateクラスを呼び出し、プライベートDate date .. setterとgettersを使用する必要があります。私はsetterとgetterを単一の属性用に作成する方法を知っていますが、setDateのようなクラス全体では、Appointmentクラスに呼び出されています。んが、値は、まあ、あなたはそれが好きだろう、整数のためのセッターを使用する方法を知っているユーザ定義クラスにsetDateを使用します。Java

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); 
+0

を* "私は単一の属性のセッターとゲッターを作成する方法を知っていますが、setDateのようなクラス全体に対して、私はAppointmentクラスにコールしました。私はそれを行う方法を知らない" *この文は私には意味がありません。変数のタイプの差異あなたは設定しようとします。 – Tom

+0

@Tomごめんなさい私は、StringsとIntegeterのためだけにsetterとgetterを使用する方法を知っていましたが、ユーザー定義のものではありません。3つの属性を持つDateクラス –

+0

そしてこれは私には意味をなさない部分です。ここに違いはありません。それは私が椅子に座る方法を知っているが、ソファに座る方法を知らないということのようです。 – Tom

答えて

0

を変更されていません。

private int setDay(int day) { 
this.day = day 
} 

あなたはまた、新しいオブジェクトを作成する方法を知っている:

public Date(int day, int month, int year) { 
    this.day = day; 
    this.month = month; 
    this.year = year; 
} 
:あなたの日付のコンストラクタのようなものに見える場合ので

public Appointment(Date date, Time time) { 
    this.date = date; 
    this.time = time; 
} 

:おそらくのようになります。コンストラクタのため

Appointment ap= new Appointment(date,time); 

新しい予定を作るのと同じように新しい日付を作ることができます:

Date newDate = new Date(day, month, year) 

同じ方法でセッターを作ることができます。これは、どちらかに見えるようになります:

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); 
関連する問題