我想在Item
類返回該項目的冠軍在那裏添加toString
方法。toString方法
我需要確保在DVD
類toString方法調用toString
方法Item
以便它可以返回一個包含標題和導演的字符串。
Item
是超和DVD
是子類。
public class Item
{
private String title;
private int playingTime;
private boolean gotIt;
private String comment;
public Item(String theTitle, int time)
{
title = theTitle;
playingTime = time;
gotIt = false;
comment = "<no comment>";
}
// Getters and setters omitted
public void print()
{
System.out.print(title + " (" + playingTime + " mins)");
if(gotIt) {
System.out.println("*");
} else {
System.out.println();
}
System.out.println(" " + comment);
}
}
public class DVD extends Item
{
private String director;
public DVD(String theTitle, String theDirector, int time)
{
super(theTitle, time);
director = theDirector;
}
// Getters and setters omitted
public void print()
{
System.out.println(" director: " + director);
}
}
我改寫的問題,以使其更具可讀性,請隨時回滾,如果這不是你的意思。 – 2011-04-02 13:38:38
如果這是家庭作業,請將其標記爲這樣 – 2011-04-02 15:19:03