- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我正在尝试编译一个子类 Car.java,它扩展了抽象类 Vehicle.java 并实现了 Comparable 接口(interface)。
当我尝试编译 Car.java 类时,出现以下错误:
Car不是抽象的,不会重写Vehicle中的抽象方法get PassengerCapacity(),找不到标志和缺少方法体,或声明 Abstract
我要寻找什么来纠正它?
Car.java 类扩展了 Abstact 类车辆,并且还实现了 Comparable 接口(interface)
即: 公共(public)类 Car 扩展了 Vehicle 实现 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
最佳答案
您必须在 Car
类中实现 getPassengerCapacity()
方法,或者将您的 Car
类标记为 abstract
。您不能扩展抽象类并保留方法而不实现主体。如果您不想实现该方法,则此类也需要标记为abstract
。
关于java - 尝试用 Java 编译子类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20426086/
冷静下来,不吹不黑。 01 最近半年,互联网一款现象级的应用诞生:「ChatGPT」; 其火爆的程度,不输前面的羊了个羊; 最初了解到ChatGP
在了解在 .Net 中开发是什么样子之前,我有几个新手问题: 非管理员用户能否安装 .Net 框架,无论是原始包还是运行程序所需的任何后续更新?我们的一些客户锁定了 XP/Vista 主机,当我们上传
https://plus.google.com/u/1/110412141990454266397/posts/Nvr6Se6eAPh 有谁知道如何使用最新版本的 GWT(来自 svn)和 Chrom
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是偏离
我需要在加载 NSDocument 之前创建多个窗口,或者创建一个阻止 NSDocument 窗口和顶部菜单的窗口。 我尝试了几种解决方案 - 但它们都不起作用。 模态窗口,一个接一个。 Async
尝试配置 spring 3 MVC,这是我目前所做的: 我将所有 spring 3.0 库添加到我的 netbeans 项目中。 我的 web.xml 是: WebApp
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我无法让 Resharper 的清理代码停止更改如下属性: public long Value { get { return _thingy; } se
我并不是真的在寻找基本的 SQL 指南,而是寻找特定于 PostgreSQL 的东西。 而且我确实运行自己的服务器,因此获取最新版本(我相信是 8.2?)没问题。 最佳答案 重要的事情: 如果可以(既
这是 Adobe Encore 程序的屏幕截图。 在这里,您可以看到一组深色组件。 可以购买,下载等吗?它们是从 Adobe Cor. 公开的吗? 谢谢 最佳答案 实现这种外观和感觉的最简单方
我有一个代码(可以正常工作)用于使用 C++ Builder 来引导 word。它对于访问文档中的不同书签很有用。 Variant vNom, vWDocuments, vWDocument, vMS
我是一名优秀的程序员,十分优秀!