如果object没有属性而我正在调用该属性,那么我们得到'missingPropertyException'.我能否以类似于缺少属性的方式执行类似安全null(?.)的操作,以便它不会通过异常?
谢谢
tim*_*tes 17
一种选择是:
def result = obj.hasProperty( 'b' ) ? obj.b : null
Run Code Online (Sandbox Code Playgroud)
如果对象没有属性,则返回null ...
另一个是propertyMissing像你这样添加到你的班级:
def propertyMissing( name ) {
null
}
Run Code Online (Sandbox Code Playgroud)
这意味着任何缺少的属性只会导致null.
| 归档时间: |
|
| 查看次数: |
13108 次 |
| 最近记录: |