我正在为我的编程类做一个家庭作业,涉及实现接口.这里的问题是我真的只是不了解接口或它们用于什么(教授对解释它不是很好).
分配是制作一个“车辆”超类,而不是三个子类,比如“卡车”或“吉普”,每个都有自己的几个特征. “Vehicle”类必须实现类似的界面,我想我已经想到了(我用compareTo()方法编写比较车辆上的门数),另一个类也必须实现“混合”类(I不知道这意味着什么).然后,我们必须实现toString(),equals(Object o)和compareTo(Object o)方法.
我想我有compareTo(),但是使用equals()我不知道.我们要做的最后一件事是编写一个Main进行测试,这涉及制作一个Vehicle对象数组,打印出来,对它们进行排序,然后重新打印它们.它还应该遍历数组并打印混合动力车的价格溢价,并使用等于(对象o)方法来比较2辆车.
这是我的“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;
}
}
我绝对不会要求任何人为我做这项工作,但是如果有人能够更多地了解接口如何工作以及这项功课真正要求的那么好.
非常感谢所有帮助
谢谢!