将 MutableClassToInstanceMap 与泛型一起使用时出现编译错误
我在 putInstance 方法调用时收到以下编译错误“MutableClassToInstanceMap 类型中的方法 putInstance(Class, T) 不适用于参数 (Class, Number)”。有谁知道我做错了什么?谢谢!
public class TestMutableClassToInstanceMap {
public final MutableClassToInstanceMap<Number> identifiers = MutableClassToInstanceMap.create();
public static void main(String[] args) {
ArrayList<Number> numbers = new ArrayList<Number>();
numbers.add(new Integer(5));
TestMutableClassToInstanceMap test = new TestMutableClassToInstanceMap(numbers);
}
public TestMutableClassToInstanceMap(Collection<Number> numbers){
for (Number number : numbers) {
this.identifiers.putInstance(number.getClass(), number); //error here
}
this.identifiers.putInstance(Double.class, 5.0); // This works
}
}
I am getting the following compile error "The method putInstance(Class, T) in the type MutableClassToInstanceMap is not applicable for the arguments (Class, Number)" on the putInstance method call. Does anyone know what I am doing wrong?? Thanks!
public class TestMutableClassToInstanceMap {
public final MutableClassToInstanceMap<Number> identifiers = MutableClassToInstanceMap.create();
public static void main(String[] args) {
ArrayList<Number> numbers = new ArrayList<Number>();
numbers.add(new Integer(5));
TestMutableClassToInstanceMap test = new TestMutableClassToInstanceMap(numbers);
}
public TestMutableClassToInstanceMap(Collection<Number> numbers){
for (Number number : numbers) {
this.identifiers.putInstance(number.getClass(), number); //error here
}
this.identifiers.putInstance(Double.class, 5.0); // This works
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与常规的 put 方法相反,putInstance 方法的目的是提供编译时类型安全性。在这种情况下,你和我可以很容易地推断出
number
必须是number.getClass()
给出的类型,但对于编译器来说,该信息“丢失了”。 ”也就是说,据it所知,也许number
是一个Integer,而number.getClass()
是Long.class;它不够“聪明”,无法判断这是安全的。解决方案:只需使用
put()
!您仍然可以获得运行时检查,并且之后仍然可以使用类型安全的getInstance()
方法,例如Long l = this.identifiers.getInstance(Long.class);.
(最后提示:请注意,因为
Long.class
和long.class
可以同时存在于ClassToInstanceMap
中并映射到不同的值!)The purpose of the
putInstance
method, as opposed to the regularput
, is to provide compile-time type safety. In this case, you and I can easily reason thatnumber
must be of the type given bynumber.getClass()
, but to the compiler, that information is "lost." That is, as far as it knows, maybenumber
is an Integer andnumber.getClass()
is Long.class; it's not "smart" enough to figure out this is safe.Solution: just use
put()
! You still get the runtime checks, and you can still use the typesafegetInstance()
method after that, such as inLong l = this.identifiers.getInstance(Long.class);
.(Final tip: watch out, because
Long.class
andlong.class
can both exist in aClassToInstanceMap
and be mapped to different values!)凯文的回答很棒。此外,您可以从方法定义中看出编译器不会允许调用:
putInstance
需要(在您的情况下)一个Class
。number.getClass()
提供一个Class
这不是直接子类。一些方差和协方差的讨论在这里可能有用。
Kevin's answer is great. Besides, you can tell from the method definition that the compiler is not going to allow the call:
putInstance
requires (in your case) aClass<Number>
.number.getClass()
provides aClass<? extends Number>
Which is not a direct subclass. Some variance and covariance talk could be useful here.