Java类类型 [英] Java Class Type
问题描述
Class<?>< p> ; normalFormClass = null;
---添加---
通配符<?>
是令我困惑的部分。为什么会这样使用而不是使用它(泛型)?
这意味着它可以是任何类型的类。 ?是表示所有类型集合的通配符,或者任何。所以你可以稍后做
整数normalForm = new Integer();
normalFormClass = normalForm.getClass();
或
String normalForm = new String();
normalFormClass = normalForm.getClass();
如果您不知道Java上的泛型,请阅读 http://java.sun.com/developer/technicalArticles/J2SE/generics/
至于为什么,我认为这可能是严格地表示您在各处使用泛型,并且您的代码与旧版Java不兼容,或者可能会关闭一些触发快乐的IDE。是的,
类foo
和
Class<?> foo
是等价的。
I have a block of code that works and I wanted to ask what exactly is happening here?
Class<?> normalFormClass = null;
---added---
The wildcard "<?>
" is the part that is confusing for me. Why would this be used rather than not using it (generics)?
That means it can be a Class of any type. ? is a wildcard denoting the set of all types, or 'any'. So you can later do
Integer normalForm = new Integer();
normalFormClass = normalForm.getClass();
or
String normalForm = new String();
normalFormClass = normalForm.getClass();
If you are not aware of generics on Java, read http://java.sun.com/developer/technicalArticles/J2SE/generics/
As for the why, I think it might be to strictly express that you are using generics everywhere and your code is not compatible with older Java versions, or maybe to shut up some trigger happy IDEs. And yes,
Class foo
and
Class<?> foo
are equivalent.
这篇关于Java类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!