英文:
Why use the comparable interface in Java?
问题
为什么在Java中要使用comparable
接口呢?直接创建compareTo
方法不是更简单吗?
不使用comparable
接口,而是这样做:
// 一些实现了comparable接口的类
public int compareTo(someClass someInstanceOfThatClass) {
// 执行返回-1、0或1的操作
}
为什么不能只是这样做:
// 一些类并未实现comparable接口,但我们仍然可以创建compareTo方法
public int compareTo(someClass someInstanceOfThatClass) {
// 执行返回-1、0或1的操作
}
我猜我的问题是,如果我们可以在没有被某个接口(如comparable
)强制的情况下创建compareTo
方法,为什么还要费劲去实现comparable
接口呢?
英文:
I wanted to ask why we use the comparable interface in java? Wouldn't it be simpler to just make the compareTo method without using the comparable interface?
Instead of doing this:
//some class that implements comparable
public int compareTo(someClass someInstanceOfThatClass) {
// do stuff that returns -1,0,or 1
}
Why can't we just do this:
//some class that does NOT implement comparable, but we still
//make a compareTo method
public int compareTo(someClass someInstanceOfThatClass) {
// do stuff that returns -1,0, or 1
}
I guess my question is why do we bother implementing comparable, if we could just make a compareTo method without being forced to by some interface (comparable)?
答案1
得分: 3
Comparable
是一个接口,因此它强制规定其他人可能遵循的契约。例如,调用Collections.sort(list)
仅在元素是Comparable
的实例时才有效,并在内部依赖于compareTo
方法来实现排序。
英文:
Comparable
is an interface, hence it imposes a contract that others may follow. For example, calling Collections.sort(list)
only works if the elements are instances of Comparable
, and internally relies on the compareTo
method to implement the sorting.
答案2
得分: 2
Java的类型系统是名义的,而不是结构的,因此仅仅拥有符合接口要求的方法是不足以实现它的;你还必须声明该类实现了接口。在一些其他语言中,如TypeScript,拥有正确签名的方法就足够了,但Java不是这样的。
如果你只是在自己的代码中调用compareTo
方法,那可能并不重要,但如果你在使用标准库或其他库的类或方法,这些类或方法接受Comparable
类型的对象作为参数,那么你的类就需要实现这个接口,以便你可以将你的对象传递给它们。
英文:
Java's type system is nominal, not structural, so simply having the method required by the interface is not enough to implement it; you also have to declare that the class implements the interface. In some other languages such as Typescript, having the method with the right signature would be enough, but Java is not like that.
If you are only calling the compareTo
method from your own code then this may not matter, but if you are using classes or methods from the standard library or from other libraries which take Comparable
things as arguments, then your class will need to implement the interface so you can pass your objects to them.
答案3
得分: 0
我认为它返回到了接口的内在概念。您始终确信每个实现了Comparable接口的类都具有可比较的能力,有时您需要这种保证。例如,如果您有一个具有Comparable类型参数的方法,那么您可以确信compareTo方法是针对该参数实现的,并且该参数在语义上是可比较的。但是如果没有接口,您无法获得这种保证。
英文:
I think it returns to the innate concept of the interface.
You always sure that every class which has implemented Comparable interface, has ability to be compared and sometimes you need this assurance.
For example if you have a method that have a parameter with Comparable type, then you are sure that comapreTo is implemented with that parameter and this parameter issemantically comparable.
But whitout interface you can't get this assurance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论