2个compareTo方法在同一个类定义中被重写,我如何强制使用第二个?

发布于 2024-10-08 11:56:46 字数 558 浏览 6 评论 0原文

我想对列表进行排序 List; donnees 根据其领域之一的标准。我的问题是,compareTo 已经被这个类覆盖。所以我有类似的东西:

Blabla {

    public int compareTo(Object other) {
        ...
     }

    public int compareTo(Blabla other) {
        ... 
     }

}

在我调用的业务层类中:

Business {
   method (){
       Collections.sort(List<Blabla > donnees);
   }

}

但是这个调用带有对象参数的N°1compareTo方法。如何使用 N°2 方法对列表进行排序?

这是一个古老而庞大的应用程序,我不知道更改 N°1compareTo 方法中的任何内容会产生副作用,这就是为什么我想添加另一个compareTo(或其他任何)方法而不是更改第一个方法。

I want to sort a list List<Blabla> donnees by a criterion on one of its field. My problem is that compareTo is already overriden for this Class. So I've got something like :

Blabla {

    public int compareTo(Object other) {
        ...
     }

    public int compareTo(Blabla other) {
        ... 
     }

}

In a business layer class I call :

Business {
   method (){
       Collections.sort(List<Blabla > donnees);
   }

}

But this call N°1 compareTo method with object parameter. How could I sort my list with the N°2 method?

This is on an old and huge application, I don't know the side effects on changing anything in the N°1compareTo method that's why I want to add another compareTo (or anything else )method instead of changing the first one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野稚 2024-10-15 11:56:46

使用比较器而不是可比较

它将让您使用尽可能多的不同方式进行比较

use Comparator instead of Comparable.

It will let you use as many as different ways u want to compare

断爱 2024-10-15 11:56:46

我相信关于为什么您最终调用compareTo(Object)而不是特定方法的问题的答案是因为您的类定义说实现了Comparable(没有任何类型)范围)。如果没有任何泛型类型,任何类型(例如 T)都将默认为 Object

对此有几个解决方案。第一个已经被其他人推荐,它涉及创建您自己的自定义 Comparator 对象并在排序方法中使用它们。因为这些比较器是单独的对象,所以它们不会干扰现有的类行为。以这些比较器为例,在某些具有 getName()getAge() 方法的 Person 类上。您可以根据需要创建任意数量的这些。

public class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o1.getAge() - o2.getAge();
    }
}

public class NameComparator implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

完成该工作后,您可以决定是否要更改原始类,以便它正确实现 Comparable。此时,您完全删除 compareTo(Object) 方法并保留另一个方法。当您编译或在 IDE 中保存时,您将发现该对象在没有类型的情况下被引用为 Comparable 的所有位置,并且它们将显示为编译器警告或错误。然后由您决定如何修复每种情况下的代码。这在很大程度上取决于 compareTo(Object) 方法与 compareTo(Blabla) 方法的不同之处。如果它显式执行 instanceof 检查和/或直接转换为 Blabla 类型,那么转换应该是 100% 安全的,因为这意味着所有现有代码必须正确使用它,如果您在运行时永远不会遇到任何 ClassCastException

I believe the answer to the question about why you end up calling the compareTo(Object) method instead of the specific one is because your class definition says implements Comparable (without any type parameter). Without any generic type, any types like T will default to Object.

There are a couple of solutions to this. The first one has already been recommended by others, and it involves making your own custom Comparator<Blabla> objects and using those in your sort methods. Because these comparators are separate objects, they won't interfere with the existing class behaviour. Take these comparators for example, on some class Person which has getName() and getAge() methods. You can create as many of these as you need.

public class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o1.getAge() - o2.getAge();
    }
}

public class NameComparator implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

After you get that working, you can decide whether or not you wish to change your original class so that it properly implements Comparable<Blabla>. At that point, you remove the compareTo(Object) method entirely and keep the other one. When you compile, or when you save in your IDE, you'll then discover all the places where that object is referenced as Comparable without a type, and they'll show as compiler warnings or errors. It's then down to you to decide how to fix the code in each case. That very much depends on what the compareTo(Object) method does differently from the compareTo(Blabla) method. If it explicitly does an instanceof check and/or cast directly to type Blabla, then the conversion should be 100% safe because that means all existing code must be using it correctly if you never experience any ClassCastExceptions at runtime.

贩梦商人 2024-10-15 11:56:46

在 CompareTo(Object other) 有一个 if 语句说

`if(other instanceof Blabla) Return compareTo((Blabla)other)`

in the CompareTo(Object other) have an if statement saying

`if(other instanceof Blabla) Return compareTo((Blabla)other)`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文