Java:删除“Comparable 是原始类型”警告
假设我有一个名为 foo 的方法,以 2 个对象作为参数。两个对象具有相同的类型并且都实现类似的接口。
void foo(Object first, Object second){
if (!first.getClass().isInstance(second)) //first and second of the same type
return;
Comparable firstComparable = (Comparable)first; //WARNING
Comparable secondComparable = (Comparable)second; //WARNING
int diff = firstComparable.compareTo(secondComparable); //WARNING
}
前 2 个警告是:
Comparable 是原始类型。对泛型类型 Comparable 的引用 应该参数化
最后一个警告:
类型安全:compareTo(Object)方法属于原始类型 可比。对泛型类型 Comparable 的引用应该是 参数化
如何重构我的代码以删除这些警告?
编辑: 我可以在不更改 foo 方法的签名的情况下做到这一点吗?
Suppose I have a method called foo taking 2 Object as parameter. Both objects are of the same type and both implements comparable interface.
void foo(Object first, Object second){
if (!first.getClass().isInstance(second)) //first and second of the same type
return;
Comparable firstComparable = (Comparable)first; //WARNING
Comparable secondComparable = (Comparable)second; //WARNING
int diff = firstComparable.compareTo(secondComparable); //WARNING
}
The first 2 warning are:
Comparable is a raw type. References to generic type Comparable
should be parameterized
The last warning:
Type safety: The method compareTo(Object) belongs to the raw type
Comparable. References to generic type Comparable should be
parameterized
How could I refactor my code in order to remove these warnings?
EDIT:
Can I do that without changing foo method's signature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须告诉编译器它们是相同类型并且具有可比性。如果您无法更改签名,您可以添加向后兼容的方法。
You have to tell the compiler that they are the same type and comparable. If you can't change the signature you can add a method for backward compatibility.
无需更改签名,您可以
但是您仍然得到:
类型安全:未检查从 Object 到 Comparable
但的引用应该参数化
Comparable 不是原始类型。对泛型类型 Comparable的引用应该参数化
并且没有类型安全性:compareTo(Object) 方法属于原始类型 Comparable。对泛型类型 Comparable
Without changeing Signature you can do
But you still got :
Type safety: Unchecked cast from Object to Comparable<Object>
but no
Comparable is a raw type. References to generic type Comparable<T> should be parameterized
and no
Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized
你必须使用
Comparable
,其中 Type 是实现Comparable
的对象。首先,为什么你的方法参数是
Objects
的实例?如果确定参数类型相同,则应使用特定的类作为参数。如果可以有类的层次结构,则将类放在层次结构中最高的类。使用Object
来实现一般功能从来都不是一个好主意。You have to do use
Comparable<Type>
where Type is the object that is implementingComparable
.First, why are your method parameters instance of
Objects
? If you are sure the types of parameters are same, you should use the specific class as the parameter. If you can have an hierarchy of classes, have the class highest in the hierarchy. HavingObject
to acheive general functionality is never a good idea.编辑:既然你说你不能改变方法的签名,那么如果没有不安全的(对编译器)强制转换和@SuppressWarnings,你真的无法逃脱:
EDIT: Since you said you can't change the method's signature, then you really can't get away without an unsafe (to the compiler) cast, and a
@SuppressWarnings
:添加@SuppressWarnings注释。
Add @SuppressWarnings annotation.