【发布时间】:2011-04-02 13:29:58
【问题描述】:
我想在Item 类中添加一个toString 方法,该方法返回其中项目的标题。
我需要确保DVD 类中的toString 方法调用Item 中的toString 方法,以便它可以返回一个包含标题和导演的字符串。
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);
}
}
【问题讨论】:
-
我已经改写了这个问题以使其更具可读性,如果这不是您的意思,请随时回滚。
-
如果这是家庭作业,请标记它
标签: java