【发布时间】:2012-04-28 01:56:56
【问题描述】:
我正在为涉及实现接口的编程课程做作业。这里的问题是我真的完全不理解接口或它们的用途(教授不太擅长解释它)。
任务是创建一个“Vehicle”超类,而不是三个子类,比如“Truck”或“Jeep”,它们每个都有自己的几个特征。 “Vehicle”类必须实现可比较的接口,我想我已经弄清楚了(我编写了compareTo() 方法来比较车辆上的门数),另外一个类也必须实现“Hybrid”类(我不知道这是什么意思)。然后我们必须实现toString()、equals(Object o) 和compareTo(Object o) 方法。
我认为我的 compareTo() 已关闭,但对于 equals() 我不知道。我们要做的最后一件事是编写一个 Main 来测试,这包括制作一个 Vehicle 对象的数组,将它们打印出来,对它们进行排序,然后重新打印它们。它还应该遍历数组并打印出混合动力车的溢价,并使用equals(Object o) 方法比较两辆汽车。
这是我的“车辆”超类的代码
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;
}
}
我绝对不会要求任何人为我做这项工作,但如果有人能更清楚地了解接口是如何工作的,以及这个作业真正要求的是什么,那就太好了。
非常感谢所有帮助
谢谢!
【问题讨论】:
-
接口基本上是一个类的契约。它说如果你想称自己为
Comparable,你需要实现构成Comparable的这些方法。 -
接口只是一种强制类的外观和行为标准化的方式。当一个接口定义一个方法时,实现该接口的每个类都必须覆盖该方法,这保证了该类将拥有自己的该方法版本。
-
我希望你的教授不要读这个问题。 :)
标签: java inheritance interface superclass comparable