java 实现可比较的接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6037845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:04:26  来源:igfitidea点击:

Implementing the comparable interface

javacomparecomparable

提问by John Curtsy

I just found this exam question and cannot figure it out :

我刚刚发现了这个考试问题,无法弄清楚:

The following depicts a contrived partial class which implements the Comparable interface. The only purpose of this contrived class is comparing its instances with a given string.

下面描述了一个实现 Comparable 接口的人为部分类。这个人为的类的唯一目的是将它的实例与给定的字符串进行比较。

There are two things we need to fill in in the class to finish it. Here is the class :

我们需要在类中填写两件事才能完成它。这是课程:

public class PrivateComparableClass // FILL IN PART 1 { 
   private String thing;

    public PrivateComparableClass(String thing) {
     this.thing=thing;
    }
   //FILL IN PART 2
}

I am assuming part 1 simply corresponds to :

我假设第 1 部分仅对应于:

public class PrivateComparableClass implements Comparable {

And part 2, I assume he is expecting an implementation of the compareTo method, but I don't really know how to properly go about implementing this:

第 2 部分,我假设他期待 compareTo 方法的实现,但我真的不知道如何正确地实现这个:

public static int compareTo() {
  if this.thing.equals(thing){
  return 1;
  } else {
    return -1;
  }
}

How would I go about fixing this?

我将如何解决这个问题?

回答by NPE

First of all, part 1 should really be:

首先,第 1 部分应该是:

public class PrivateComparableClass implements Comparable<PrivateComparableClass> {

As to part 2, if thingis the only data member in the class, you can simply piggyback on String.compareTo:

至于第 2 部分,如果thing是类中唯一的数据成员,您可以简单地搭载String.compareTo

public int compareTo(PrivateComparableClass rhs) {
  return this.thing.compareTo(rhs.thing);
}

I recommend that you read up on how compareTois meant to work(there are three possible outcomes: less than, equal to and greater than).

我建议您阅读有关如何compareTo工作的内容(有三种可能的结果:小于、等于和大于)。

回答by digitalbath

To expand a bit:

稍微扩展一下:

Comparator functions typically take two arguments (let's call them A and B) and follow the convention of returning

比较器函数通常采用两个参数(让我们称它们为 A 和 B)并遵循返回约定

  • -1 if A < B
  • 0 if A == B
  • 1 if A > B
  • -1 如果 A < B
  • 0 如果 A == B
  • 1 如果 A > B

Also, compareTo should not be declared 'static' if you are using an instance variable.

此外,如果您使用的是实例变量,则不应将 compareTo 声明为“静态”。

回答by erickson

First, the Comparableinterface is generic; your declarations should specify a type parameter:

首先,Comparable接口是通用的;您的声明应指定一个类型参数:

public class PrivateComparableClass 
  implements Comparable<PrivateComparableClass> {

Then, you should compare the thingmembers of the class in a compareTo()method (which is an instance method, not a class member).

然后,您应该比较方法中thing类的成员compareTo()(这是一个实例方法,而不是类成员)。

@Override
public final int compareTo(PrivateComparableClass that) {
  return this.thing.compareTo(that.thing);
}

A well-behaved Comparableshould implement an equals()method that is consistent with its compareTo()method:

一个行为端正的人Comparable应该实现一个equals()与其compareTo()方法一致的方法:

@Override
public final boolean equals(Object obj) {
  if (obj == this)
    return true;
  if (!(obj instanceof PrivateComparableClass))
    return false;
  return compareTo((PrivateComparableClass) obj) == 0;
}

And, when you override equals(), you need to override hashCode()too:

而且,当您覆盖时equals(),您也需要覆盖hashCode()

@Override
public final int hashCode() {
  return thing.hashCode();
}

If thingis truly allowed to be null, suitable null-checking behavior should be added to each method.

如果thing确实允许null,则应为每个方法添加合适的空检查行为。

回答by james_bond

Well, this is more or less how the class should be declared and implemented

嗯,这或多或少是应该如何声明和实现类的

public class PrivateComparableClass implements Comparable<PrivateComparableClass>
{
    private String thing;
    //... other stuff

    public int compareTo(PrivateComparableClass o)
    {
       return this.thing.compareTo(o.thing);
    }
}