【发布时间】:2021-12-09 22:32:30
【问题描述】:
我想根据 seats 对 Vehicles 数组进行排序。我尝试在这里寻找解决方案,但是每当我将Vehicle 对象传递给compareTo 方法时,我的所有其他文件中都会出现错误,例如The type Bike must implement the inherited abstract method Comparable.compareTo(Object)。有人可以帮我处理这段代码吗?
public abstract class Vehicle implements Comparable {
protected int seats;
protected int wheels;
protected int price;
protected int weight;
public int compareTo(Vehicle o) {
return -1;
}
public static void main(String [] args) {
Car Lamborghini = new Car(5,4,30000, 1500);
Bike BMX = new Bike(1,2,300, 15);
Vehicle Vehicles[] = new Vehicle[2];
Vehicles[0] = Lamborgini;
Vehicles[1] = Bmx;
Arrays.sort(Vehicles);
for(int i=0;i<Vehicles.length;i++) {
System.out.println(Vehicles[i]);
}
}
【问题讨论】:
-
The type Bike must implement the inherited abstract method Comparable.compareTo(Object)compareTofromBike的实现在哪里?
标签: java