【发布时间】:2014-01-30 01:17:01
【问题描述】:
我有一个程序为汽车经销商解释和排序数据,但在尝试检索存储在数组中的汽车颜色时出错。
这里是主类和它的子类。
class Car
{
protected String model;
protected int price;
protected int year;
public Car(String m, int y, int p)
{
model = m;
price = p;
year = y;
}
}
class NewCar extends Car
{
protected String color;
public NewCar(String m, int y, int p, String c)
{
super(m, y, p);
color = c;
}
public String toString()
{
return "Model: " + model + "\n"
+ "Year: " + year + "\n"
+ "Price: $" + price + "\n"
+ "Color: " + color + "\n"
+ "Selling Price: " + price + "\n\n";
}
}
这是另一个发生错误的类,位于if(cars[z].color.equals(shade))。
程序在 Car 类中找不到可变颜色。
class CarDealerShip
{
public String printAllCarsOfColor(String shade)
{
String s = "";
for(int z = 0; z < i; z++)
{
if(cars[z].color.equals(shade))
{
s += "Car " + (z + 1) + "\n" + cars[z].toString();
}
}
return s;
}
如何让程序在存在可变颜色的类 NewCar 中查找?
【问题讨论】:
标签: java class inheritance