【问题标题】:errors when trying to compile a child class in Java尝试在 Java 中编译子类时出错
【发布时间】:2013-12-06 22:57:20
【问题描述】:

您好,我正在尝试编译一个子类 Car.java,它扩展了抽象类 Vehicle.java 并实现了 Comparable 接口。

当我尝试编译 Car.java 类时出现以下错误:

Car 不是抽象的,并且不会覆盖 Vehicle 中的抽象方法 getPassengerCapacity(), 找不到标志 和 缺少方法主体,或声明摘要

我该寻找什么来纠正它?

Car.java 类扩展了 Abstact 类车辆,还实现了 Comparable 接口

即: 公共类 Car 扩展 Vehicle 实现 Comparable

我必须编写 2 个类 Car.java 和 Bus.java。它们都扩展了车辆并实现了 Comparable。

一切都需要我运行程序 selectionSort.java 和 SortVehicle.java。

这些错误使我无法完成我的 Car.java 类,因为我无法通过它们来编写 Bus.java 类,然后运行 ​​SortSelection.java 和 SortVehicle.java 程序。

Vehicle.java、SelectionSort.java 和 SortVehicle.java 已提供给我,没有任何错误或问题。

如果能帮助我纠正这些错误,我将不胜感激。

我的 Car.java 代码是:

public class Car extends Vehicle implements Comparable
{
public static final int MAX_ENGINE_CAPACITY = 2000;
public static final int MIN_ENGINE_CAPACITY = 1800;
public static final int MAX_NUMBER_OF_SEATS = 7;
public static final int MIN_NUMBER_OF_SEATS = 7;

private String vehicleColour;
private int numberOfSeats;
private int engineCapacity;

public Car (Car otherCar)
{
    super();
    this.vehicleColour = otherCar.vehicleColour;
    this.numberOfSeats = otherCar.numberOfSeats;        
    this.engineCapacity = otherCar.engineCapacity;
}   

/public Car()
{
    super();
    this.vehicleColour = new vehicleColour();
    this.numberOfSeats = MIN_NUMBER_OF_SEATS;
    this.engineCapacity = MIN_ENGINE_CAPACITY;
}

public Car (String aModel, int aYearOfManufacture, String aVehicleColour, int aNumberOfSeats, int aEngineCapacity)
{
    super(aModel, aYearOfManufacture);
    this.vehicleColour = otherCar.vehicleColour;
    this.numberOfSeats = otherCar.numberOfSeats;        
    this.engineCapacity = otherCar.engineCapacity;
}


public String getVehicleColour()
    {
    return this.vehicleColour;
    }

public int getNumberOfSeats()
    {
    return this.numberOfSeats;
    }

public int getEngineCapacity()
    {
    return this.engineCapacity;
    }
public void setVehicleColour (String newVehicleColour);
    {
        if (newVehicleColour == null)
        {
            System.out.println("Fatal Error setting vehicle colour.");
            System.exit(0);
        }
        else
            this.vehiclColour = newVehicleColour;
    }

public void  setNumberOfSeats (int newNumberOfSeats)
    {
    if (newNumberOfSeats == null)
        {
         System.out.println("Fatal Error setting number Of Seats.");
         System.exit(0);
        }
   else
        this.numberOfSeats = newNumberOfSeats;
    }

 public void  setEngineCapacity (int newEngineCapacity);
   {
    if (newEngineCapacity == null)
        {
         System.out.println("Fatal Error setting number Of Seats.");
         System.exit(0);
        }
   else
        this.engineCapacity = newEngineCapacity;
    }

}

Vehicle.java 代码是:

public abstract class Vehicle
{
public static final int MAX_REASONABLE_YEAR_OF_MANUFACTURE = 2100;
public static final int MIN_REASONABLE_YEAR_OF_MANUFACTURE = 1000;

private String model;
private int yearOfManufacture;

public Vehicle ()
{
    this.model = "(model unspecified)";
    this.yearOfManufacture = MIN_REASONABLE_YEAR_OF_MANUFACTURE;
}

public Vehicle (String aModel, int aYearOfManufacture)
{
    this.model = aModel;
    if (yearOfManufactureReasonable(aYearOfManufacture))
        this.yearOfManufacture = aYearOfManufacture;
    else
    {
        System.out.println("Fatal Error: unreasonable year of manufacture used defining vehicle.");
        System.exit(0);
    }
}

public Vehicle (Vehicle otherVehicle)
{
    this.model = otherVehicle.model;
    this.yearOfManufacture = otherVehicle.yearOfManufacture;
}

private static boolean yearOfManufactureReasonable(int aYearOfManufacture)
{
    return (aYearOfManufacture >= MIN_REASONABLE_YEAR_OF_MANUFACTURE 
             && aYearOfManufacture <= MAX_REASONABLE_YEAR_OF_MANUFACTURE);
}

public String getModel ()
{
    return this.model;
}

public int getYearOfManufacture ()
{
    return this.yearOfManufacture;
}

public void setModel (String aModel)
{
    this.model = aModel;
}

public void setYearOfManufacture (int aYearOfManufacture)
{
    if (yearOfManufactureReasonable(aYearOfManufacture))
        this.yearOfManufacture = aYearOfManufacture;
}

public String toString ()
{
    return (this.model + ", " + this.yearOfManufacture);
}

public boolean equals (Object otherObject)
{
    if (otherObject == null)
        return false;
    if (getClass() != otherObject.getClass())
        return false;
    Vehicle otherVehicle = (Vehicle)otherObject;
    return (this.model.equals(otherVehicle.model) 
             && this.yearOfManufacture == otherVehicle.yearOfManufacture);
}

public abstract int getPassengerCapacity ();

public boolean greaterPassengerCapacityThan (Vehicle otherVehicle)
{
    return (this.getPassengerCapacity() > otherVehicle.getPassengerCapacity());
}

}

我得到的完整错误列表如下:

    Car.java:5: error: Car is not abstract and does not override abstract method get
    PassengerCapacity() in Vehicle
    public class Car extends Vehicle implements Comparable
           ^
    Car.java:27: error: cannot find symbol
            this.vehicleColour = new vehicleColour();
                                                         ^
    symbol:   class vehicleColour
    location: class Car
    Car.java:35: error: cannot find symbol
    this.vehicleColour = otherCar.vehicleColour;
                                        ^
    symbol:   variable otherCar
    location: class Car
    Car.java:36: error: cannot find symbol
            this.numberOfSeats = otherCar.numberOfSeats;
                                                    ^
    symbol:   variable otherCar
    location: class Car
    Car.java:37: error: cannot find symbol
            this.engineCapacity = otherCar.engineCapacity;
                                                    ^
    symbol:   variable otherCar
    location: class Car
    Car.java:56: error: missing method body, or declare abstract
    public void setVehicleColour (String newVehicleColour);
                   ^
    Car.java:58: error: cannot find symbol
                    if (newVehicleColour == null)
                         ^
    symbol:   variable newVehicleColour
    location: class Car
    Car.java:64: error: cannot find symbol
                            this.vehiclColour = newVehicleColour;
                                  ^
    symbol: variable vehiclColour
    Car.java:64: error: cannot find symbol
                            this.vehiclColour = newVehicleColour;
                                                              ^
   symbol:   variable newVehicleColour
   location: class Car
   Car.java:69: error: incomparable types: int and <null>
    if (newNumberOfSeats == null)
                                              ^
   Car.java:78: error: missing method body, or declare abstract
   public void  setEngineCapacity (int newEngineCapacity);
                       ^
   Car.java:80: error: cannot find symbol
    if (newEngineCapacity == null)
         ^
   symbol:   variable newEngineCapacity
   location: class Car
   Car.java:86: error: cannot find symbol
        this.engineCapacity = newEngineCapacity;
                                                ^
   symbol:   variable newEngineCapacity
   location: class Car
   13 errors

【问题讨论】:

  • 展示你的抽象和具体的类代码
  • @user3074723 您需要将代码放入您的问题中。还要确保正确解释。
  • KevinDTimms 我已使用编译器的错误列表将代码添加到我的问题中。
  • @KevinDTimms 我已经使用编译器的错误列表将我的代码添加到我的问题中

标签: java compiler-errors


【解决方案1】:

您必须在您的Car 类中实现getPassengerCapacity() 方法或将您的Car 类标记为abstract。您不能在不实现主体的情况下扩展抽象类并留下方法。如果你不想实现那个方法,那么这个类也需要标记为abstract

【讨论】:

    【解决方案2】:

    您的错误消息指出,您的类 Car 必须实现 Vehicle 中定义的抽象方法 PassengerCapacity()。如果你认为你已经完成了,那可能是你在方法的签名中输入了错误的关键字?

    【讨论】:

      【解决方案3】:

      现有的答案很好 - 但只是补充一点,如果您在实现抽象方法的地方使用 @override 注释,那么如果您在方法签名中遇到错误,您将立即收到错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多