尝试在Java中编译子类时出错

时间:2013-12-06 14:17:34

标签: java compiler-errors

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

当我尝试编译Car.java类时,我收到以下错误:

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

我该怎么做才能纠正它?

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

即:  公共类汽车扩展汽车实施可比较

我必须编写2个类Car.java和Bus.java。它们既可以扩展车辆又可以实现可比性。

我需要运行程序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

3 个答案:

答案 0 :(得分:2)

您必须在getPassengerCapacity()课程中实施Car方法,或将Car课程标记为abstract。您无法扩展抽象类并在不实现主体的情况下保留方法。如果您不想实现该方法,则此类也需要标记为abstract

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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