实现类似的接口
我刚刚发现这个考试问题,但无法弄清楚:
下面描述了一个实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,第 1 部分实际上应该是:
至于第 2 部分,如果
thing
是类中唯一的数据成员,您可以简单地搭载String.compareTo
:I建议您阅读如何
compareTo< /code> 的意思是工作
(有三种可能的结果:小于、等于和大于)。
First of all, part 1 should really be:
As to part 2, if
thing
is the only data member in the class, you can simply piggyback onString.compareTo
: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).扩展一下:
比较器函数通常采用两个参数(我们称它们为 A 和 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
Also, compareTo should not be declared 'static' if you are using an instance variable.
首先,Comparable 接口是通用的;你的声明应该指定一个类型参数:
然后,你应该在
compareTo()
方法中比较类的thing
成员(这是一个实例方法,而不是类成员) )。行为良好的
Comparable
应该实现与其compareTo()
方法一致的equals()
方法:并且,当您重写
>equals()
,你也需要重写hashCode()
:如果
thing
确实允许为null
,则合适的null - 应将检查行为添加到每个方法中。First, the
Comparable
interface is generic; your declarations should specify a type parameter:Then, you should compare the
thing
members of the class in acompareTo()
method (which is an instance method, not a class member).A well-behaved
Comparable
should implement anequals()
method that is consistent with itscompareTo()
method:And, when you override
equals()
, you need to overridehashCode()
too:If
thing
is truly allowed to benull
, suitable null-checking behavior should be added to each method.好吧,这或多或少就是类应该如何声明和实现的
Well, this is more or less how the class should be declared and implemented