Groovy / grails如何确定数据类型? [英] Groovy / grails how to determine a data type?
问题描述
在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屋!