实现类似的接口

发布于 2024-11-08 17:11:44 字数 655 浏览 4 评论 0原文

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

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

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

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

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

我假设第 1 部分简单地对应于:

public class PrivateComparableClass implements Comparable {

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

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

我将如何去做解决这个问题吗?

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.

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 :

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:

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

How would I go about fixing this?

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

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

发布评论

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

评论(4

杀お生予夺 2024-11-15 17:11:44

首先,第 1 部分实际上应该是:

public class PrivateComparableClass implements Comparable<PrivateComparableClass> {

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

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

I建议您阅读如何compareTo< /code> 的意思是工作(有三种可能的结果:小于、等于和大于)。

First of all, part 1 should really be:

public class PrivateComparableClass implements Comparable<PrivateComparableClass> {

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

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

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

春庭雪 2024-11-15 17:11:44

扩展一下:

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

  • 如果 A < 则返回 -1 的约定。 则B
  • 如果 A == B,
  • 0如果 A > 则为 1 B

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

To expand a bit:

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

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

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

世俗缘 2024-11-15 17:11:44

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

public class PrivateComparableClass 
  implements Comparable<PrivateComparableClass> {

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

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

行为良好的 Comparable 应该实现与其 compareTo() 方法一致的 equals() 方法:

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

并且,当您重写 >equals(),你也需要重写hashCode()

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

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

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

public class PrivateComparableClass 
  implements Comparable<PrivateComparableClass> {

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

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

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

@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:

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

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

十年不长 2024-11-15 17:11:44

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

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

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文