Groovy / grails如何确定数据类型? [英] Groovy / grails how to determine a data type?

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

问题描述

在groovy中确定数据类型的最佳方法是什么?



我想格式化输出,如果它是一个日期等。

解决方案<为了确定一个对象的类,只需调用:

  someObject.getClass()

/ code>

在大多数情况下,您可以缩写为 someObject.class 。但是,如果您在 Map 上使用它,它将尝试使用键'class'检索值。正因为如此,我总是使用 getClass(),即使它稍微长一点。



如果您想检查如果一个对象实现了一个特定的接口或者扩展了一个特定的类(例如Date),使用:

pre $ (somObject instanceof Date)

或者检查一个对象的类是否正好一个特定的类(不是
$ b

 (somObject.getClass()== Date)


What is the best way to determine the data type in groovy?

I'd like to format the output differently if it's a date, etc.

解决方案

To determine the class of an object simply call:

someObject.getClass()

You can abbreviate this to someObject.class in most cases. However, if you use this on a Map it will try to retrieve the value with key 'class'. Because of this, I always use getClass() even though it's a little longer.

If you want to check if an object implements a particular interface or extends a particular class (e.g. Date) use:

(somObject instanceof Date)

or to check if the class of an object is exactly a particular class (not a subclass of it), use:

(somObject.getClass() == Date)

这篇关于Groovy / grails如何确定数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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