Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找不到符号。正在查找超类而不是子类_Java_Class_Inheritance - Fatal编程技术网

Java 找不到符号。正在查找超类而不是子类

Java 找不到符号。正在查找超类而不是子类,java,class,inheritance,Java,Class,Inheritance,我有一个为汽车经销商解释和排序数据的程序,在试图检索存储在数组中的汽车颜色时出错 这是主类及其子类 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 { protect

我有一个为汽车经销商解释和排序数据的程序,在试图检索存储在数组中的汽车颜色时出错

这是主类及其子类

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;
    }
class卡片管理员
{
公共字符串printAllCarsOfColor(字符串阴影)
{
字符串s=“”;
对于(intz=0;z

如果存在可变颜色,如何让程序在NewCar类中进行查找?

您的数组
cars
似乎属于
Car[]
。在引用数组元素后,如果引用变量为
Car
,则无法判断它是指
Car
NewCar
,还是指
Car
的另一个子类

看起来您希望
cars[z]
具有属性
color
,因此
cars
可能应该是
NewCar[]
类型,而不是
Car[]


另一个选项是将属性
color
移动到超类
Car
,这样任何
Car
都可以有颜色。

使用受保护访问时,该字段将在同一包内的类或基类的子类中可用。我假设访问
color
字段不在同一个包中,或者不扩展
Car
color
NewCar
中受保护。您只能在子类中访问受保护的变量。您需要将
color
移动到
Car
并添加
公共字符串getColor()
方法,使其可用于不属于
Car
继承层次结构的类

public String getColor() {
   return color;
}
然后你的情况会是

if(cars[z].getColor().equals(shade))
更新

如果您希望
color
出现在
NewCar
中,您应该在
NewCar
中添加
公共字符串getColor();
方法,并且您的
cars[]
应该是
NewCar[]
,类似于

NewCar cars[] = new NewCar[arraySize]();

这样,您将失去继承功能,您不能再使用
Car cars[]=new NewCar[arraySize]

如果要求
color
必须在class
NewCar
中,您可以使用
instanceof
操作符,然后将其强制转换为:

class CarDealerShip
{
    public String printAllCarsOfColor(String shade)
    {
        String s = "";

        for(int z = 0; z < i; z++)
        {
            if (cars[z] instanceof NewCar)
            {
                NewCar nc = (NewCar)cars[z];
                if (nc.color.equals(shade))
                {
                    s += "Car " + (z + 1) + "\n" + nc.toString();
                }
            }
        }
        return s;
    }
}
class卡片管理员
{
公共字符串printAllCarsOfColor(字符串阴影)
{
字符串s=“”;
对于(intz=0;z

实际上,您跳过了每一辆
Car
而不是
NewCar
,只使用那些属于类
NewCar

的实例来执行“Car cars[]=new NewCar[80];”无法消除错误,“color”属性必须在子类“NewCar”中,变量color必须在类NewCar中,这是一项要求。将数组全部更改为NewCar会破坏类CarYes的子类UsedCar,这将破坏
多态性
。除非将
color
移动到super类,否则不能使用with
Car[]
。您的
cars[]
仅存放
NewCar
s或任何其他对象,如
UsedCar