将 MutableClassToInstanceMap 与泛型一起使用时出现编译错误

发布于 2024-08-26 11:09:49 字数 848 浏览 7 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(2

人│生佛魔见 2024-09-02 11:09:49

与常规的 put 方法相反,putInstance 方法的目的是提供编译时类型安全性。在这种情况下,你和我可以很容易地推断出 number 必须是 number.getClass() 给出的类型,但对于编译器来说,该信息“丢失了”。 ”也就是说,据it所知,也许number是一个Integer,而number.getClass()是Long.class;它不够“聪明”,无法判断这是安全的。

解决方案:只需使用put()!您仍然可以获得运行时检查,并且之后仍然可以使用类型安全的 getInstance() 方法,例如 Long l = this.identifiers.getInstance(Long.class);.

(最后提示:请注意,因为 Long.classlong.class 可以同时存在于 ClassToInstanceMap 中并映射到不同的值!)

The purpose of the putInstance method, as opposed to the regular put, is to provide compile-time type safety. In this case, you and I can easily reason that number must be of the type given by number.getClass(), but to the compiler, that information is "lost." That is, as far as it knows, maybe number is an Integer and number.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 typesafe getInstance() method after that, such as in Long l = this.identifiers.getInstance(Long.class);.

(Final tip: watch out, because Long.class and long.class can both exist in a ClassToInstanceMap and be mapped to different values!)

暮年慕年 2024-09-02 11:09:49

凯文的回答很棒。此外,您可以从方法定义中看出编译器不会允许调用:

  • 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) a Class<Number>.
  • number.getClass() provides a Class<? extends Number>

Which is not a direct subclass. Some variance and covariance talk could be useful here.

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