Java浇铸“.class” - 在通用类型上使用的操作符。 List“,”List< List<?>>和“Class< List< Integer>>> [英] Java casting ".class"-operator used on a generic type, e.g. List, to "Class<List<?>>" and to "Class<List<Integer>>"

查看:124
本文介绍了Java浇铸“.class” - 在通用类型上使用的操作符。 List“,”List< List<?>>和“Class< List< Integer>>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 .class -operator来向通用类提供有关所包含类型的信息。对于非通用的包含类型, Integer.class ,这样工作没有任何问题。但是所包含的类型是通用的,例如。 List< Integer> .class List.class 会导致类转换的编译时错误。

有一种方法来规避错误,但我很好奇这里发生了什么。有人可以解释发生了什么吗?为什么事情就像他们?,什么是绕过这个问题的最好方法是什么?



以下几行显示了问题:
注意外部通用类型期望类< T> 作为参数,因此在这种情况下 Class< List< Integer> c $ c>。

  Class< Integer> tInt = Integer.class; //按预期工作。 
Class< List> tList = List.class; //与警告一起工作,但不是
//我在找什么。
Class< List< Integer>> tListInt1 = List.class; // Error
Class< List< Integer>> tListInt2 =(Class< List< Integer>>)List.class; // Error
Class< List<?>> tListGeneric =(Class< List< Integer>>)List.class; //错误

下一行工作原理:

  Class< List< Integer>> tListInt3 = 
(Class< List< Integer>>)((Class< Integer>)List.class)

为什么 tListInt2 code> tListGeneric 给和错误?
为什么upcast然后downcast与 tListInt3 不会产生错误?
是否有更好的方法来声明 tListInt3



b $ b Kasper van den Berg



ps。让我知道如果你喜欢看代码外部通用容器,需要这种类型的信息;

解决方案

 类< List< Integer> tListInt3 = 
(Class< List< Integer>>)((Class< Integer>)List.class);

无效。您可能意味着

 类< List< Integer>> tListInt3 = 
(Class< List< Integer>>)((Class)List.class);

我们可以随时通过转换然后下载来从一种类型转换为另一种类型

  Integer x =(Integer)(Object)string; 

List.class 的类型是 Class< List> ;它不是 Class< List< Whatever>> 的子类型/超类型,因此这两种类型之间的直接转换是非法的。



可以说, Class< List< Integer>> 不存在 - 只有一个类 List ;没有这样的类 List< Integer> (在运行时真的只是 List



然而,这是Java类型系统的缺陷;在实践中,我们需要 Class< List< Integer>> 。我们的解决方案 - 铸造和假装类< List< Int>> 退出 - 同样有缺陷 - 但这不是我们的错。


I use the .class-operator to supply information about the contained type to a generic class. For non-generic contained types, e.g. Integer.class, this works without any problems. But with the contained type being a generic, e.g. List<Integer>.class or List.class it results in compile time errors about class casting.

There is a way to circumvent the errors, but I'm curious about what is happening here. Can someone explain what is happening?, why things are as they are?, and what the best way to circumvent the problem is?

The following lines demonstrate the problem: Note the outer generic type expects Class<T> as parameter, so in this case Class<List<Integer>>.

Class<Integer> tInt = Integer.class;                     // Works as expected.
Class<List> tList = List.class;              // Works with warning, but is not
                                             // what i'm looking for.
Class<List<Integer>> tListInt1 = List.class;                          // Error
Class<List<Integer>> tListInt2 = (Class<List<Integer>>) List.class;   // Error
Class<List<?>> tListGeneric = (Class<List<Integer>>) List.class;      // Error

The next line works:

Class<List<Integer>> tListInt3 = 
                (Class<List<Integer>>) ((Class<Integer>)List.class);

Why do the declarations of tListInt2 and tListGeneric give and error? Why does upcast and then downcast with tListInt3 not produce an error? Is there a better way to declare tListInt3?

With kind regards,
Kasper van den Berg

ps. Let me know if you like to see code the outer generic container that needs this type information; i'll post it if needed.

解决方案

Class<List<Integer>> tListInt3 = 
            (Class<List<Integer>>) ((Class<Integer>)List.class);

that doesn't work. you probably meant

Class<List<Integer>> tListInt3 = 
            (Class<List<Integer>>) ((Class)List.class);

we can always cast from one type to another by up-cast then down-cast

    Integer x = (Integer)(Object)"string";

The type of List.class is Class<List>; it is not a subtype/supertype of Class<List<Whatever>> therefore direct cast between the two types is illegal.

It can be argued that Class<List<Integer>> doesn't exist - there is only a class for List; there is no such class for List<Integer> (which really is just List at runtime)

However, this is a flaw of Java type system; in practice we do need things like Class<List<Integer>>. Our solution - casting and pretending Class<List<Int>> exits - is likewise flawed - but it's not our fault.

这篇关于Java浇铸“.class” - 在通用类型上使用的操作符。 List“,”List&lt; List&lt;?&gt;&gt;和“Class&lt; List&lt; Integer&gt;&gt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆