以下陈述:
URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> uclc = ucl.getClass();
因错误而失败:
Type mismatch: cannot convert from Class<capture#2-of ? extends URLClassLoader> to Class<URLClassLoader>
这里为什么需要演员表?
Why do I need a cast, here?
我发现了几篇文章解释了为什么你不能做相反的事情(将 T 分配给 a ),但这(有点)显而易见并且可以理解.
I found several posts explaining why You can't do the reverse (assign T to a ), but that's (kind of) obvious and understood.
注意:我是在 eclipse Luna 下编写的,所以我不知道这是 Luna 的怪癖还是我真的不理解泛型.
NOTE: I am coding this under eclipse Luna, so I don't know if it's a Luna Quirk or if there's something I really dont understand in generics.
协变 vs 逆变 vs 不变
类 是不变的.因此,
类 不是 Class<URLClassLoader>
在 Java 中,变量可以保存对相同类型或子类型的实例的引用.
In Java a variable can hold a reference of an instance of same type or subtype.
因此,
Class<URLClassLoader> uclc = ucl.getClass();
无效.
另一方面,
Class<? extends URLClassLoader> uclc = ucl.getClass();
将是有效.
这篇关于为什么不能分配 I <?扩展类型>到<类型>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
“Char 不能被取消引用"错误quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch 语句 - 是“或"/“和"可能的?Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java替换字符串特定位置的字符?Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
具有 int 和 char 操作数的三元表达式的类型是什么What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
读取文本文件并存储出现的每个字符Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
为什么我需要在 byte 和 short 上显式转换 char 原语Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)