继承Java实现接口
我正在为我的编程课做一个家庭作业,涉及实现接口。这里的问题是,我真的完全不理解接口或它们的用途(教授不太擅长解释)
任务是创建一个“Vehicle”超类,以及三个以上的子类,比如“Truck”或“Jeep”,每个子类都有自己的一些特性。“Vehicle”类必须实现可比较的接口,我想我已经找到了这个接口(我编写了compareTo()
方法来比较车辆上的车门数量),另外一个类也必须实现“Hybrid”类(我不知道这意味着什么)。然后我们必须实现toString()
、equals(Object o)
和compareTo(Object o)
方法
我想我已经把compareTo()
放下了,但是对于equals()
我不知道。我们要做的最后一件事是编写一个要测试的Main,这涉及到制作一个车辆对象数组,将它们打印出来,对它们进行排序,然后重新打印它们。它还应该遍历数组,打印出混合动力车的价格溢价,并使用equals(Object o)
方法比较两辆车
这是我的“Vehicle”超类的代码
package vehicle;
abstract public class Vehicle implements Comparable {
private String color;
private int numberOfDoors;
// Constructor
/**
* Creates a vehicle with a color and number of doors
* @param aColor The color of the vehicle
* @param aNumberOfDoors The number of doors
*/
public Vehicle(String aColor, int aNumberOfDoors) {
this.color = aColor;
this.numberOfDoors = aNumberOfDoors;
}
// Getters
/**
* Gets the color of the vehicle
* @return The color of the vehicle
*/
public String getColor() {return(this.color);}
/**
* Gets the number of doors the vehicle has
* @return The number of doors the vehicle has
*/
public int getNumberOfDoors() {return(this.numberOfDoors);}
// Setters
/**
* Sets the color of the vehicle
* @param colorSet The color of the vehicle
*/
public void setColor(String colorSet) {this.color = colorSet;}
/**
* Sets the number of doors for the vehicle
* @param numberOfDoorsSet The number of doors to be set to the vehicle
*/
public void setNumberOfDoors(int numberOfDoorsSet) {this.numberOfDoors = numberOfDoorsSet;}
@Override
public int compareTo(Object o) {
if (o instanceof Vehicle) {
Vehicle v = (Vehicle)o;
return this.numberOfDoors - v.getNumberOfDoors();
}
else {
return 0;
}
}
/**
* Returns a short string describing the vehicle
* @return a description of the vehicle
*/
@Override
public String toString() {
String answer = "The car's color is "+this.color
+". The number of doors is"+this.numberOfDoors;
return answer;
}
}
我还将发布我的一个子类
package vehicle;
abstract public class Convertible extends Vehicle {
private int topSpeed;
// Constructor
/**
* Creates a convertible with a color, number of doors, and top speed
* @param aColor The color of the convertible
* @param aNumberOfDoors The number of doors of the convertible
* @param aTopSpeed The top speed of the convertible
*/
public Convertible (String aColor, int aNumberOfDoors, int aTopSpeed) {
super(aColor, aNumberOfDoors);
this.topSpeed = aTopSpeed;
}
// Getters
/**
* Gets the top speed of the convertible
* @return The top speed of the convertible
*/
public int getTopSpeed() {return(this.topSpeed);}
// Setters
/**
* Sets the top speed of the convertible
* @param topSpeedSet The top speed to set to the convertible
*/
public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}
/**
* Returns a short description of the convertible
* @return a short description of the convertible
*/
@Override
public String toString() {
String answer = "The car's color is "+super.getColor()
+", the number of doors is "+super.getNumberOfDoors()
+", and the top speed is "+this.topSpeed+" mph.";
return answer;
}
}
我绝对不是要求任何人为我做这项工作,但如果有人能对接口的工作原理和这个家庭作业的真正要求有更多的了解,那就太好了
非常感谢您的帮助
谢谢
共 (0) 个答案