我在程序中实现compareto和clone方法时遇到一些麻烦,我得到一个错误:“Car不是抽象的,并且不会覆盖java.lang中的抽象方法compareTo(java.lang.Object) 。可比”。任何帮助表示赞赏:)
这是我到目前为止所做的:
import java.util.*;
public class Car implements Comparable, Cloneable
{
private String model;
private String color;
private int year;
private int vin_number;
private double price;
public Car(){}
public Car(String model, String color, int year, int vin_number, double price)
{
this.model = model;
this.color = color;
this.year = year;
this.vin_number = vin_number;
this.price = price;
}
public Object clone() { // shallow copy
try
{
return super.clone();
}
catch (CloneNotSupportedException e)
{
return null;
}
}
public String getModel(){return model;}
public String getColor(){return color;}
public int getYear(){return year;}
public int getVinNumber(){return vin_number;}
public double getPrice(){return price;}
public void setModel(String newModel){model = newModel;}
public void setColor(String newColor){color = newColor;}
public void setYear(int newYear){year = newYear;}
public void setVinNumber(int newVinNumber){vin_number = newVinNumber;}
public void setPrice(double newPrice){price = newPrice;}
public String toString()
{
return "Model: " + model + " Color: " + color + " Year: " + year + " Vin Number: " + vin_number + " Price: " + price;
}
public int compareTo(Car o)
{
if(this.getModel().compareTo(car.getModel())) //gives 1
return 1;
if(car.getModel().compareTo(this.getModel())) //gives -1
return -1;
else
{
return 0;
}
}
public static void main(String [] args)
{
}
}
import java.util.*;
public class Car implements Comparable, Cloneable
{
private String model;
private String color;
private int year;
private int vin_number;
private double price;
public Car(){}
public Car(String model, String color, int year, int vin_number, double price)
{
this.model = model;
this.color = color;
this.year = year;
this.vin_number = vin_number;
this.price = price;
}
public Object clone() { // shallow copy
try
{
return super.clone();
}
catch (CloneNotSupportedException e)
{
return null;
}
}
public String getModel(){return model;}
public String getColor(){return color;}
public int getYear(){return year;}
public int getVinNumber(){return vin_number;}
public double getPrice(){return price;}
public void setModel(String newModel){model = newModel;}
public void setColor(String newColor){color = newColor;}
public void setYear(int newYear){year = newYear;}
public void setVinNumber(int newVinNumber){vin_number = newVinNumber;}
public void setPrice(double newPrice){price = newPrice;}
public String toString()
{
return "Model: " + model + " Color: " + color + " Year: " + year + " Vin Number: " + vin_number + " Price: " + price;
}
public int compareTo(Car o)
{
if(this.getModel().compareTo(car.getModel())) //gives 1
return 1;
if(car.getModel().compareTo(this.getModel())) //gives -1
return -1;
else
{
return 0;
}
}
public static void main(String [] args)
{
}
}
答案 0 :(得分:1)
您必须在实施可比较时指定要比较的类型:
class Car implements Comparable<Car>, Cloneable
原始类型将使用Object
。您使用的是原始类型,因此,compareTo
必须将Object
声明为参数,而不是Car
。
要解决此错误,请将implements Comparable
更改为implements Comparable<Car>