Java:对compareTo(T)的未经检查的调用
1 class test {
2 public static int compare0(Comparable x, Comparable y) {
3 return x.compareTo(y);
4 }
5 public static int compare1(Object x, Object y) {
6 return ((Comparable) x).compareTo((Comparable) y);
7 }
8 public static int compare2(Object x, Object y) {
9 Comparable p = (Comparable) x;
10 Comparable q = (Comparable) y;
11 return (p).compareTo(q);
12 }
13 public static void main(String[] args) {
14 Comparable zero = new Integer(0);
15 Comparable one = new Integer(1);
16 int c = (zero).compareTo(one);
17 }
18 }
编译上面的代码会产生 4 个警告:
% javac -Xlint:unchecked test.java
test.java:3: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return x.compareTo(y);
^
test.java:7: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return ((Comparable) x).compareTo((Comparable) y);
^
test.java:13: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return (p).compareTo(q);
^
test.java:19: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
int c = (zero).compareTo(one);
^
4 warnings
我尝试了更多变体,但警告仍然存在。编写和调用上面的 test.compare 方法的正确方法是什么?
谢谢!
PS:test.compare只是一个例子;我不需要这样的功能;但我需要实现一个函数,就像 test.compare 一样,需要在其签名中包含可比较的实现对象。
PS2:我已经编程了 25 年以上,大约 10 年前我什至编程了一段时间 Java,但现在使用 Java(由于我的工作需要)让我发疯。对于经验丰富的程序员来说,学习 Java 比看起来要困难得多。有这么多\ 那里有很多关于学习 Java 的东西,其中 99% 充其量是过时的,或者是为了给编程新手排名(即非常冗长),最坏的情况是彻头彻尾的垃圾……我还没有找到关于 Java 的参考资料,可以让我很快就把上面问题的答案归零了。
1 class test {
2 public static int compare0(Comparable x, Comparable y) {
3 return x.compareTo(y);
4 }
5 public static int compare1(Object x, Object y) {
6 return ((Comparable) x).compareTo((Comparable) y);
7 }
8 public static int compare2(Object x, Object y) {
9 Comparable p = (Comparable) x;
10 Comparable q = (Comparable) y;
11 return (p).compareTo(q);
12 }
13 public static void main(String[] args) {
14 Comparable zero = new Integer(0);
15 Comparable one = new Integer(1);
16 int c = (zero).compareTo(one);
17 }
18 }
Compiling the code above produces 4 warnings:
% javac -Xlint:unchecked test.java
test.java:3: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return x.compareTo(y);
^
test.java:7: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return ((Comparable) x).compareTo((Comparable) y);
^
test.java:13: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
return (p).compareTo(q);
^
test.java:19: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
int c = (zero).compareTo(one);
^
4 warnings
I tried many more variants, but the warnings remain. What's the correct way to write and call the test.compare method above?
Thanks!
PS: test.compare is only an example; I don't need such a function; but I need to implement a function that, like test.compare, needs to have Comparable-implementing objects in its signature.
PS2: I've been programming for 25+ years, I even programmed Java for a while around 10 years ago, but using Java now (required by my job) is driving me berserk. For an experienced programmer, learning Java is much harder than it looks. There's so mu\
ch stuff on learning Java out there, 99% of which is at best outdated, or pitched to rank programming novices (i.e. massively verbose), and at worst is outright garbage... I have yet to find a reference on Java that will let me quickly zero in the answer to the question above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用通用参数声明
compare
方法。You should declare the
compare
method with a generic argument.可比较
是通用 - 您应该使用Comparable
定义变量Comparable
is generic - you should define the variables withComparable<Integer>
真正的问题是您试图在静态方法中进行比较。使进行比较的方法成为非静态的,实例化一个或多个“测试器”对象并为每个对象提交一种类型。例如:
然后调用 String 对象的比较方法:
如果您想比较其他类型的对象(例如 Integers),您将需要一个新的测试器对象,例如:
如果您愿意这样做,那么您的类可以沿着行:
The real problem is that you are trying to do your comparisons within a static method. Make the method that does the comparison non-static, instantiate one or more "tester" objects and commit to a type for each. For example:
then to invoke the comparison method for String objects:
If you want to compare other types of objects such as Integers you will need a new tester object like:
If you are willing to do this, then your class can be defined along the lines of:
将
zero
和one
声明为Integer
或Comparable
而不是原始类型。Angelika Langer 制作了关于仿制药的一个很好的参考。它被组织为分层常见问题解答,并且应该允许您可以快速找到大多数通用类型问题的具体答案。在这种情况下,您可能会发现有关原始类型的部分很有用。
Declare
zero
andone
asInteger
orComparable<Integer>
rather than raw types.Angelika Langer has produced a great reference for generics. It's organized as a hierarchical FAQ and should allow you to find specific answers to most generic type problems quickly. In this case, you may find the section on raw types useful.