Groovy中的变量之后的问号(?)的目的是什么? [英] What is the purpose of question mark(?) after a variable in Groovy

查看:721
本文介绍了Groovy中的变量之后的问号(?)的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ 

c $ c> boolean equals(other){
if(other?.is(this))
return true
}

上面的代码包含If其他条件中的条件以?所以我想知道那个表示的含义

解决方案

?。是一个空安全运算符,用于避免意外的NPE。

  if(a?.b){..} 

相同

  if(a!= null&& ab){..} 

但是在这种情况下, is()已经是空的安全的,所以你不需要它

  other.is(this)

应该很好。

I am new to grails I found in many examples that a variable may end with question mark (?) like this

boolean equals(other) {
    if(other?.is(this))
    return true
}

above code contains If condition in that other is ending with a ? so I want to know the meaning of that representation

解决方案

?. is a null safe operator which is used to avoid unexpected NPE.

if ( a?.b ) { .. }

is same as

if ( a != null && a.b ) { .. }

But in this case is() is already null safe, so you would not need it

other.is( this )

should be good.

这篇关于Groovy中的变量之后的问号(?)的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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