在java中,从可比较的扩展意味着什么
我看到这样的代码
class A implements Comparable<A> {
}
这是什么意思,它的优点和缺点是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我看到这样的代码
class A implements Comparable<A> {
}
这是什么意思,它的优点和缺点是什么?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
这意味着该类致力于响应“比较 。
使用此声明(以及任何其他“实现”声明)的优点是您可以“摘要”对象的类型和接口的代码。
考虑一下,
然后您可以编写一些可以使用 Comparable< 的代码/a> 而不是特定类型:
并像这样调用它:
因为你并不真正关心类的类型是什么,你只关心它是否实现了接口。
这种(针对接口而不是针对实现进行编程)是 OO 编程世界中最强大的技术之一,因为它促进了 低耦合。
It means that class is committed to respond to the methods defined by the "interface" Comparable.
The advantage you have with this ( and any other "implements" declaration ) it that you can "abstract" the type of the object and code to the interface instead.
Consider this
You then may code something that can use Comparable instead of a specific type:
And invoke it like:
Because you don't really care what the type of the class is, you just care it does implement the interface.
This ( program to the interface rather to the implementation ) is one of the most powerful techniques in OO programming world, because it promotes low-coupling.
实现Comparable接口意味着A可以与A的其他实例进行比较。java
中涉及排序的许多操作都使用Comparable接口中定义的方法来确定A的实例是否大于小于或等于其他实例。
通过实现这些方法,您可以使用许多方便的功能,例如 java 排序、使用 A 的实例作为二叉树的键等等。
Implementing a comparable interface means that A can be compared with other instances of A.
Many operations in java that involve sorting use the methods defined in the Comparable interface to determine if instances of A are greater then less or equal to other instances.
By implementing these methods you are able to use a lot of handy features such as java sort, use instances of A as keys for binary trees, and more.
这意味着可以使用 ComparablecompareTo 方法对类 A 进行排序:
Comparable 使用类的自然排序。
自 Java 5 以来的另一种方法是 Comparator< /a>. 您可以传递此对象,并有多种方法来比较和排序目标类。 例如,有时您可能希望按名字对 Name 类进行排序,有时则按姓氏排序。 Comparable 只为您提供了一种方法,但您可以拥有多个 Comparator 实例。
It means that class A can be sorted using the Comparable compareTo method:
Comparable uses the natural ordering for your class.
Another way to go since Java 5 is Comparator. You can pass this object around and have more than one way to compare and sort the target class. For example, sometimes you might want to sort a Name class by first name, other times by last name. Comparable only gives you one way to do it, but you can have several Comparator instances.
这意味着该类可以由函数操作,这些函数期望其参数是可以与相同类型的其他对象进行比较的对象(例如列表的预定义排序功能)。
实现 Comparable 接口意味着该类支持该接口所需的某些函数(具体来说,compareTo() 方法),在该类上执行的排序(或其他)操作将利用这些函数来完成其任务。工作而不必关心班上的其他人。
更多细节:
http://java.sun.com/ j2se/1.4.2/docs/api/java/lang/Comparable.html
It means that the class is one which can be operated on by functions which expect their arguments to be objects that can be compared with other objects of the same type (such as the pre-defined sorting functionality for lists).
Implementing the Comparable interface means that the class supports certain functions which the interface requires (specifically, the
compareTo()
method), that the sorting (or other) operations being performed on the class will utilize to do their work without having to care about the rest of the class.For more details:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html
除了其他人所说的之外,通过实现接口(或扩展类),您可以让编译器强制执行超类型的契约。 在“Comparable”的情况下,这意味着如果您未能在实现类中实现“intcompareTo(A anA)”方法,则会出现编译器错误。 在实现方法中添加注释“@Override”可提供更高的编译时安全性; 如果您未能使用正确的签名实现该方法,编译器会告诉您。 修复编译时错误比修复运行时错误要容易得多,成本也低得多。 此外,实现接口允许实现类的任何实例被视为将接口类型作为实参或泛型参数的方法(和构造函数)的接口类型。 例如,“java.util.Collections.max(Collection coll)”方法采用其基类型必须扩展“Comparable”的集合。
http:// download.oracle.com/javase/7/docs/api/java/util/Collections.html#max(java.util.Collection)
In addition to what everyone else said, by implementing an interface (or extending a class), you get compiler enforcement of the contract of the supertype. In the case of 'Comparable', that means that you get a compiler error if you fail to implement the 'int compareTo(A anA)' method in the implementing class. Adding the annotation '@Override' to the implementing method provides even more compile-time safety; if you fail to implement the method with the right signature the compiler will tell you. Compile-time errors are much, much easier and cheaper to fix than run-time errors. Furthermore, implementing an interface allows any instance of an implementing class to be treated as the interface type for methods (and constructors) that take the interface type as an argument or generic parameter. For example, the 'java.util.Collections.max(Collection coll)' method takes a collection whose base type must extend 'Comparable'.
http://download.oracle.com/javase/7/docs/api/java/util/Collections.html#max(java.util.Collection)
这意味着此类的对象可以轻松地在集合中排序,因为它们可以相互比较。 另一种选择是实现一个 Comparator,它是一个负责对其他类进行排序的类。 Comparable将排序逻辑直接放在要排序的类中; 比较器将排序逻辑放在不同的类中。
It means that objects of this class can be easily sorted in collections because they can be compared to each other. The other option is to implement a Comparator which is a class responsible for sorting other classes. The Comparable puts the sorting logic directly in the class to be sorted; the Comparator puts the sorting logic in a different class.