提示:许多TravelCup没有手柄。为此,在TravelCup类中添加一个布尔实例变量。更改构造函数和方法以适应此更改。将任何适当的方法添加到该更改所需的类中。证明更改在测试运行中有效。
我能完成大部分项目,但我被困在这里了。我不确定是应该创建一个新类还是创建一个对象,但我尝试创建了一个新的构造函数,这只会给我一个错误。我已经走到了死胡同,现在我不知道该怎么办。有人能帮忙吗?
代码:
public class Cup
{
// instance variables
private int volume; // in oz.
private String color;
private String material;
/**
* Default Constructor for objects of class Cup
*/
public Cup()
{
// initialise instance variables
volume = 8;
color = "white";
material = "ceramic";
}
public Cup(int v, String c, String m)
{
// initialise instance variables
volume = v;
color = c;
material = m;
}
public Cup(Cup other)
{
// initialise instance variables
volume = other.volume;
color = other.color;
material = other.material;
}
public void set(int v, String c, String m)
{
volume = v;
color = c;
material = m;
}
public String toString()
{
return "This is a " + color + " cup made of " + material
+ "nIt holds " + volume + " oz. ";
}
/**
*
* @return the volume
*/
public int getVolume()
{
return volume;
}
public String getColor()
{
return color;
}
public String getMaterial()
{
return material;
}
public boolean equals(Cup other)
{
if(other == null)
return false;
else if( getClass() != other.getClass())
return false;
else
{
Cup otherCup = (Cup)other;
return volume == otherCup.volume && color.equals(otherCup.color) && material.equals(otherCup.material);
}
}
}
public class LogoCup extends Cup
{
private String logo;
private String slogan;
public LogoCup()
{
super( );
logo = "";
slogan = "";
}
public LogoCup(int v, String c, String m, String lg, String s)
{
super(v, c, m );
logo = lg;
slogan = s;
}
public LogoCup(LogoCup other)
{
super(other );
logo = other.logo;
slogan = other.slogan;
}
public String toString()
{
return super.toString()
+ " Logo: " + logo + " Slogan: " + slogan;
}
public boolean equals(LogoCup other)
{
if(other == null)
return false;
else if( getClass() != other.getClass())
return false;
else
{
LogoCup otherLogoCup = (LogoCup)other;
return logo.equals(otherLogoCup.logo) && slogan.equals(otherLogoCup.slogan) && super.equals( otherLogoCup);
}
}
public String getLogo()
{
return logo;
}
public String getSlogan()
{
return slogan;
}
}
public class TravelCup extends LogoCup
{
public TravelCup()
{
super();
}
public TravelCup(int v, String c, String m, String lg, String s)
{
super(v, c, m, lg, s );
}
public TravelCup(TravelCup other)
{
super(other );
}
public String toString()
{
return "Travel Cup! " + super.toString() + "nEvery TravelCup has a lid!";
}
public boolean equals(Object other)
{
if(other == null)
return false;
else if( getClass() != other.getClass())
return false;
else
{
TravelCup otherTravelCup = (TravelCup)other;
return super.equals( otherTravelCup);
}
}
public boolean equals(Object handle);
{
}
}
TravelCup
可以通过以下更新实现:
- 通过构造函数添加了字段
boolean hasHandle
、它的getter和设置 - 更新构造函数以使用
this(...)
使用现有构造函数 - 更新
toString
和equals
以包括新字段 - (可选(添加了
set
方法,通过调用需要添加到LogoCup
的super.set
来设置所有参数
class TravelCup extends LogoCup {
private boolean hasHandle = false;
public TravelCup() {
super();
}
public TravelCup(int v, String c, String m, String lg, String s) {
this(v, c, m, lg, s, false);
}
public TravelCup(int v, String c, String m, String lg, String s, boolean h) {
super(v, c, m, lg, s );
this.hasHandle = h;
}
public TravelCup(TravelCup other) {
super(other);
this.hasHandle = other.hasHandle;
}
public String toString() {
return "Travel Cup! " + super.toString() + ", hasHandle? " + hasHandle + "nEvery TravelCup has a lid!";
}
public boolean equals(Object other)
{
if(other == null)
return false;
if (this == other)
return true;
if(getClass() != other.getClass())
return false;
TravelCup otherTravelCup = (TravelCup) other;
return super.equals(otherTravelCup) && this.hasHandle == otherTravelCup.hasHandle;
}
public boolean hasHandle() {
return hasHandle;
}
public void set(int v, String c, String m, String l, String s, boolean h)
{
super.set(v, c, m, l, s);
this.hasHandle = h;
}
}
// class LogoCup
class LogoCup extends Cup {
// ...
public void set(int v, String c, String m, String l, String s) {
super.set(v, c, m);
logo = l;
slogan = s;
}
}