以下のコードで私はすでにその部屋= rと宣言しています。サブジェクト= s;時間= tである。私の講師は、部屋のテーマと時間にsetメソッドを追加するよう具体的に尋ねましたが、私がコメントアウトしてもそれが機能するように冗長なコードです。定義済みコンストラクタが使用されていない場合にのみ、setメソッドをインクルードする必要がありますか?彼らに方法を設定させる利点は何ですか?属性が既に定義済みコンストラクターで定義されている場合、setメソッドを使用する上で重要なのは何ですか?
class LectureTest{
public static void main (String [] args){
Lecture l1 = new Lecture(140, "Comp", 5);
l1.display();
Lecture l2 = new Lecture(280, "Sports", 3);
l2.display();
Lecture l3 = new Lecture(101, "Business", 5);
l3.display();
Lecture l4 = new Lecture(360, "Shooting", 4);
l4.display();
Lecture l5 = new Lecture();
l5.display();
}
}//end of LectureTest
class Lecture{
private int room;
private String subject;
private int time;
Lecture(int r, String s, int t){
room = r;
subject = s;
time = t;
}
Lecture(){}
public void setroomNumber(int r){
room = r;
}
public void setSubject(String s){
subject = s;
}
public void setTime(int t){
time = t;
}
public int getroomNumber(){
return room;
}
public String getSubject(){
return subject;
}
public int getTime(){
return time;
}
public void display(){
System.out.printf("\n" + "Room Number: " + getroomNumber() + "\n" + "Subject: " + getSubject() + "\n" + "Time " + getTime() + "\n");
}
}
をテストする場合はSO私は構文エラーがので、注意... IDEを使用していなかったかもしれない、あなたが道を必要とするこのフォーラムにこのすべてをDIDそれを突然変異させる。 – azurefrog