加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么groovy .class返回与.getClass()不同的值

发布时间:2020-12-14 16:34:40 所属栏目:大数据 来源:网络整理
导读:根据 http://groovy.codehaus.org/Things+you+can+do+but+better+leave+undone Accessing an object’s type like a property Using .class instead of .getClass() is ok – as long as you know exactly what kind of object you have. But then you don’
根据 http://groovy.codehaus.org/Things+you+can+do+but+better+leave+undone

  1. Accessing an object’s type like a property

Using .class instead of .getClass() is ok – as long as you know
exactly what kind of object you have. But then you don’t need that at
all. Otherwise,you run in the risk of getting null or something else,
but not the class of the object.

a = [:] println a.class.simpleName // NullPointerException,because
a.class is null.

有人可以解释为什么会这样吗?为什么.class返回与getClass()不同的东西

解决方法

因为当a是地图时,a.class在Groovy中与a.get(“class”)相同.正如您在文档中的示例中可以看到的,这将返回null.这就是为什么规则趋向于使用getClass,除非你绝对确定变量不会是一个地图

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读