2012-04-12 202 views
3

我正在做一个关于实现接口的家庭作业任务,并且有点迷路。我需要实现可比较的接口并使用compareTo()方法。这是我的超级班的代码,它有三个子类,都是不同形式的车辆。在这种情况下,我试图宣传他们拥有的门的数量。Java使用可比较的接口

下面是“车辆”的代码超类

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()方法在超类的工作,是有什么我需要添加到子类,使这个功能呢?

谢谢!

回答

4

你应该有车辆Comparable<Vehicle>,所以你的compareTo方法可以采取Vehicle的论点,而不是必须施放。但如果你问如何实施compareTo方法,那么如果这辆车应该比另一辆车少,那么返回一个负数;如果它应该更大,则返回一个正数,如果它们应该相等,则返回0。您可能会使用color.compareTo(otherVehicle.color)来比较颜色,因为它们是String s。

这应该是足够的提示!

+0

谢谢,这是有益的。虽然我仍然不确定如何正确使用这里的compareTo()方法。我不知道如何去比较numberoffDoors int与这些对象 – salxander 2012-04-12 19:19:39

+0

啊!没有看到你的答案的其余部分,谢谢! – salxander 2012-04-12 19:20:21

+0

你可以比较那些老式的方式。 'if(this.numberOfDoors 2012-04-12 19:21:21

1

compareTo()应该返回一个int,表示当前对象与给定对象(参数)的比较方式。换句话说,如果你的当前对象是“小于”传递的对象,那么它应该返回一个负数。

+0

非常感谢您的帮助! – salxander 2012-04-12 19:33:15

2

如果比较应该只根据门号,请尝试以下操作:

public int compareTo(Object o) { 
    if (o instanceof Vehicle) { 
     Vehicle v = (Vehicle) o; 
     return getNumberofDoors() - v.getNumberOfDoors(); 
    } else { 
     return 0; 
    } 
} 
+0

非常感谢! – salxander 2012-04-12 19:32:11

1

首先有你的车辆实施Comparable<Vehicle>路易说。然后,在你的compareTo方法中,你想返回一个值,比较你希望它们被比较的Vehicle的方面。

public int compareTo(Vehicle v) { 
     return this.getNumberOfDoors() - v.getNumberOfDoors(); 
    } 

如果门的数量相同,这将返回零。如果v有更多的门,则为负值,如果v有更少的门,则为正值。

(如果添加了像汽车的MAKE)您也可以比较别的

public int compareTo(Vehicle v) { 
      return this.make.compareTo(v.make); 
     } 
+0

感谢您的帮助! – salxander 2012-04-12 19:32:44