在java中,从可比较的扩展意味着什么

发布于 2024-07-29 19:05:59 字数 113 浏览 1 评论 0 原文

我看到这样的代码

class A implements Comparable<A> {


}

这是什么意思,它的优点和缺点是什么?

I see code like this

class A implements Comparable<A> {


}

What does this mean, what are the advantages and disadvantages of it?

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

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

发布评论

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

评论(6

自由范儿 2024-08-05 19:05:59

这意味着该类致力于响应“比较

使用此声明(以及任何其他“实现”声明)的优点是您可以“摘要”对象的类型和接口的代码。

考虑一下,

class A implements Comparable {
    .... 
}

class B implements Comparable {
    .... 
}


class C implements Comparable {
    .... 
}

然后您可以编写一些可以使用 Comparable< 的代码/a> 而不是特定类型:

public void doSomethingWith( Comparable c ) {

       c.compareTo( other ); // something else...

}

并像这样调用它:

  doSomethingWith( new A() );

  doSomethingWith( new B() );

  doSomethingWith( new C() );

因为你并不真正关心类的类型是什么,你只关心它是否实现了接口。

这种(针对接口而不是针对实现进行编程)是 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

class A implements Comparable {
    .... 
}

class B implements Comparable {
    .... 
}


class C implements Comparable {
    .... 
}

You then may code something that can use Comparable instead of a specific type:

public void doSomethingWith( Comparable c ) {

       c.compareTo( other ); // something else...

}

And invoke it like:

  doSomethingWith( new A() );

  doSomethingWith( new B() );

  doSomethingWith( new C() );

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.

坚持沉默 2024-08-05 19:05:59

实现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.

浮世清欢 2024-08-05 19:05:59

这意味着可以使用 ComparablecompareTo 方法对类 A 进行排序:

A a1 = new A(1);
A a2 = new A(3);

// -1, 0, or 1 depending on whether a2 is less than, equal to, or greater than a1
int order = a1.compareTo(a2);

Comparable 使用类的自然排序。

自 Java 5 以来的另一种方法是 Comparator< /a>. 您可以传递此对象,并有多种方法来比较和排序目标类。 例如,有时您可能希望按名字对 Name 类进行排序,有时则按姓氏排序。 Comparable 只为您提供了一种方法,但您可以拥有多个 Comparator 实例。

It means that class A can be sorted using the Comparable compareTo method:

A a1 = new A(1);
A a2 = new A(3);

// -1, 0, or 1 depending on whether a2 is less than, equal to, or greater than a1
int order = a1.compareTo(a2);

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.

煮茶煮酒煮时光 2024-08-05 19:05:59

这意味着该类可以由函数操作,这些函数期望其参数是可以与相同类型的其他对象进行比较的对象(例如列表的预定义排序功能)。

实现 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

高跟鞋的旋律 2024-08-05 19:05:59

除了其他人所说的之外,通过实现接口(或扩展类),您可以让编译器强制执行超类型的契约。 在“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)

很糊涂小朋友 2024-08-05 19:05:59

这意味着此类的对象可以轻松地在集合中排序,因为它们可以相互比较。 另一种选择是实现一个 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.

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