Java类类型 [英] Java Class Type

查看:93
本文介绍了Java类类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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屋!

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