无法使用可比较的接口(Java)正确比较生物体

无法使用可比较的接口(Java)正确比较生物体,java,comparable,Java,Comparable,所以我有一个Java的“虚拟世界”项目,它有一个四个动物(它们扩展了有机体类)。每一种动物(羚羊、狐狸、乌龟、狼)都有一个主动性、力量和年龄的领域(所有INT)。我想把它们放在一个数组中,然后在上面以最高的主动性进行排序,如果两种动物的主动性相等,则年龄较大的动物的主动性更高。为了比较这些,我的抽象类“有机体”实现了Comparable接口,我重写了compareTo方法,所以看起来如下: @Override public int compareTo(Organism anotherOr

所以我有一个Java的“虚拟世界”项目,它有一个四个动物(它们扩展了有机体类)。每一种动物(羚羊、狐狸、乌龟、狼)都有一个主动性、力量和年龄的领域(所有INT)。我想把它们放在一个数组中,然后在上面以最高的主动性进行排序,如果两种动物的主动性相等,则年龄较大的动物的主动性更高。为了比较这些,我的抽象类“有机体”实现了Comparable接口,我重写了compareTo方法,所以看起来如下:

    @Override
public int compareTo(Organism anotherOrganism) {
    int compareValue = 0;

    if (this.getInitiative() > anotherOrganism.getInitiative()){
        compareValue = 1;
    } else if (this.getInitiative() < anotherOrganism.getInitiative()){
        compareValue = -1;
    } else if (this.getInitiative() == anotherOrganism.getInitiative()){
        if (this.getAge() > anotherOrganism.getAge()){
            compareValue = 1;
        } else {
            compareValue = -1;
        }
    }
    return compareValue;
}
希望这会导致

 Fox, Wolf, Antelope, Turtle
我收到的不是这个

 Wolf, Antelope, Turtle, Fox
这里出了什么问题?显然,动物也实现了compareTo方法(它们指的是来自生物体的super.compareTo)


编辑:是的,下面的建议说代码相当好,我已经删除了动物的任何主动权(字段留空),我仍然得到了非常相同的输出。不知道为什么……

我已将代码扩展为一个。它会打印出预期的结果

[Organism$1Turtle, Organism$1Antelope, Organism$1Wolf, Organism$1Fox]
守则:

import java.util.*;

abstract class Organism implements Comparable<Organism> {
    public static void main(String[] args) throws Throwable {
        class Fox extends Organism {
            @Override public int getInitiative() {
                return 7;
            }
        }
        class Turtle extends Organism {
            @Override public int getInitiative() {
                return 1;
            }
        }
        class Antelope extends Organism {
            @Override public int getInitiative() {
                return 4;
            }
        }
        class Wolf extends Organism {
            @Override public int getInitiative() {
                return 5;
            }
        }
        List<Organism> organismList = Arrays.asList(
            new Fox(), new Turtle(), new Antelope(), new Wolf()
        );

        Collections.sort(organismList);

        System.err.println(organismList);
    }
    @Override public String toString() {
        return getClass().getName();
    }
    public abstract int getInitiative();
    public int getAge() {
        return 42;
    }
        @Override
public int compareTo(Organism anotherOrganism) {
    int compareValue = 0;

    if (this.getInitiative() > anotherOrganism.getInitiative()){
        compareValue = 1;
    } else if (this.getInitiative() < anotherOrganism.getInitiative()){
        compareValue = -1;
    } else if (this.getInitiative() == anotherOrganism.getInitiative()){
        if (this.getAge() > anotherOrganism.getAge()){
            compareValue = 1;
        } else {
            compareValue = -1;
        }
    }
    return compareValue;
}
}
import java.util.*;
抽象类{
公共静态void main(字符串[]args)抛出可丢弃的{
狐类生物{
@重写公共int getInitiative(){
返回7;
}
}
甲鱼纲生物{
@重写公共int getInitiative(){
返回1;
}
}
羚羊纲动物{
@重写公共int getInitiative(){
返回4;
}
}
狼类生物{
@重写公共int getInitiative(){
返回5;
}
}
List organismList=Arrays.asList(
新狐狸(),新乌龟(),新羚羊(),新狼())
);
收集。分类(组织列表);
系统错误打印(组织列表);
}
@重写公共字符串toString(){
返回getClass().getName();
}
公共摘要int getInitiative();
公共整数getAge(){
返回42;
}
@凌驾
公共国际比较(生物体与其他生物体){
int compareValue=0;
如果(this.getInitiative()>另一个有机体.getInitiative()){
比较值=1;
}else if(this.getInitiative()另一个有机体.getAge()){
比较值=1;
}否则{
compareValue=-1;
}
}
返回比较值;
}
}

您可以这样做:

    @Override
public int compareTo(Organism anotherOrganism) {
    int compareValue = 0;

    if (this.getInitiative() > anotherOrganism.getInitiative()){
        compareValue = 1;
    } else if (this.getInitiative() < anotherOrganism.getInitiative()){
        compareValue = -1;
    } else if (this.getInitiative() == anotherOrganism.getInitiative()){
        if (this.getAge() > anotherOrganism.getAge()){
            compareValue = 1;
        } else {
            compareValue = -1;
        }
    }
    return compareValue;
}
公共类演示{
公共静态void main(字符串[]args){
最终列表=新的ArrayList();
//每个有机体都是由主动性、力量和年龄创造的
添加(新羚羊(4,1,1));
添加(新狐狸(7,2,2));
添加(新海龟(1,3,3));
添加(新狼(5,4,4));
添加(新狼(5,4,5));
sort(Demo::sortbyinitive);
对于(生物体o:生物体){
系统输出打印ln(o);
}
}
私有静态int sortByInitiative(生物体o1、生物体o2){
int c=Integer.compare(o2.getInitiative(),o1.getInitiative());
如果(c!=0){
返回c;
}
返回整数.compare(o2.getAge(),o1.getAge());
}
}

假设initiative和age成员变量是公共的(您可以用getter替换),您可以在一行代码中实现您想要的排序

List<Organism> organisms = Arrays.asList(/* add your objects */);
organisms.sort(Comparator.comparing((Organism o) -> o.initiative, 
Comparator.reverseOrder()).thenComparing(o-> o.age, Comparator.reverseOrder()));
List orbiats=Arrays.asList(/*添加对象*/);
生物体。分类(比较器。比较((生物体o)->o,
然后比较(o->o.age,Comparator.reverseOrder());
请参见下面的完整示例

public class Quick {

    static class Organism{
        public String name;
        public int initiative;
        public int age;

        public Organism(String name, int initiative, int age) {
            this.name = name;
            this.initiative = initiative;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Organism{" +
                    "name='" + name + '\'' +
                    ", initiative=" + initiative +
                    ", age=" + age +
                    '}';
        }
    }

    public static void main(String[] args) {
        List<Organism> orgs = Arrays.asList(
                new Organism("Fox", 1, 2),
                new Organism("Ant", 1, 3),
                new Organism("Mule", 2, 2)
        );

        orgs.sort(Comparator.comparing((Organism o) -> o.initiative,
        Comparator.reverseOrder()).thenComparing(o-> o.age, Comparator.reverseOrder()));
        orgs.stream().forEach(System.out::println);

    }

}
公共类快速{
静态类生物{
公共字符串名称;
公共int倡议;
公共信息;
公共有机体(字符串名称、整数倡议、整数年龄){
this.name=名称;
主动性=主动性;
这个。年龄=年龄;
}
@凌驾
公共字符串toString(){
返回“有机体{”+
“name=”+name+“\”+
“,initiative=“+initiative”+
“,age=“+age+
'}';
}
}
公共静态void main(字符串[]args){
List orgs=Arrays.asList(
新生物(“狐狸”,1,2),
新生物(“蚂蚁”,1,3),
新生物(“骡子”,2,2)
);
组织分类(比较器比较((有机体)->o.主动性,
然后比较(o->o.age,Comparator.reverseOrder());
forEach(System.out::println);
}
}

我认为子类没有任何理由重写
compareTo
。问题似乎不在发布的代码中。我的适当的动物类(例如fox)只是从Organism类中调用此方法,因此它看起来像:@Override public int compareTo(Organisa-anotherOrganism){return super.compareTo(anotherOrganism);}如果您所做的只是返回super.compareTo,我会把这些方法从动物身上去掉。你的年龄比较有点问题。你没有检查同一年龄段的案例。是的,所以它看起来确实是一个结构问题,而不是代码问题。我不确定这里出了什么问题