如何实现Java可比接口?
2022-08-31 09:37:58
我不确定如何在我的抽象类中实现可比较的接口。我有以下示例代码,我用它来尝试解决它:
public class Animal{
public String name;
public int yearDiscovered;
public String population;
public Animal(String name, int yearDiscovered, String population){
this.name = name;
this.yearDiscovered = yearDiscovered;
this.population = population; }
public String toString(){
String s = "Animal name: "+ name+"\nYear Discovered: "+yearDiscovered+"\nPopulation: "+population;
return s;
}
}
我有一个测试类,它将创建动物类型的对象,但是我希望在此类中有一个可比较的接口,以便较早的发现排名高于低。我不知道该怎么做。