实现接口java_Java – 实现接口
我正在為我的編程類做一個家庭作業,涉及實現接口.這里的問題是我真的只是不了解接口或它們用于什么(教授對解釋它不是很好).
分配是制作一個“車輛”超類,而不是三個子類,比如“卡車”或“吉普”,每個都有自己的幾個特征. “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;
}
}
我絕對不會要求任何人為我做這項工作,但是如果有人能夠更多地了解接口如何工作以及這項功課真正要求的那么好.
非常感謝所有幫助
謝謝!
總結
以上是生活随笔為你收集整理的实现接口java_Java – 实现接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java hashmap 输出_JAVA
- 下一篇: java美元兑换,(Java实现) 美元