私はインターフェイスの実装に関する宿題を勉強していますが、少し紛失しています。比較可能なインターフェイスを実装し、compareTo()メソッドを使用する必要があります。ここに私のスーパークラスのコードはありますが、それは3つのサブクラスを持っています。この場合、私は彼らが持っているドアの数をキャンパーにしようとしています。以下はJavaはComparable Interfaceを使用しています
「車両」スーパークラスのコード
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;}
public int compareTo(Object o) {
if (o instanceof Vehicle) {
Vehicle v = (Vehicle)o;
}
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;
}
}
は現在、それは進行中の作業である、と私はどこcompareToメソッドにここから行くようにしてくださいしないんだけどさ。どんな助けでも大歓迎です。
ありがとうございます!
編集 私はスーパーで働くのcompareTo()メソッドを取得したら、私はこの関数を作るために、サブクラスに追加する必要があるものはありますか?
ありがとうございます!
を何か他のものを比較することができ、これは便利です、ありがとうございます。私はまだここからcompareTo()メソッドを適切に使用する方法が不明です。私はこれらのオブジェクトとnumberOfDoors整数を比較する方法についてはわかりません – salxander
ああ!あなたの答えの残りを見ていない、ありがとう! – salxander
あなたはそれらを古風な方法で比較することができます。 'if(this.numberOfDoors