在groovy中输入cast变量取决于另一个变量类?

时间:2014-04-24 15:38:12

标签: groovy

例如

def a = "567"
def b = 0

现在我想检查'a'是否可以转换为int(因为b的类是int)?

我能做到

def x =  a as int

但正在做

def x = a as b.getClass()

给出错误。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:5)

说你有:

def a = '567'
def type = Integer

你可以use asType to do

assert a.asType( type ) == 567

或者如果你想使用其他变量的类型;

def a = '567'
def b = 0

assert a.asType( b.getClass() ) == 567
相关问题