Android 这个带问号的函数在做什么?
这个函数在getAction()之后做什么特别的问号Android 这个带问号的函数在做什么?,android,Android,这个函数在getAction()之后做什么特别的问号 如果操作不为null或空,则返回操作的意图,否则返回空字符串 也可以这样写 String strAction; if(getIntent().getAction() == null || getIntent().getAction().isEmpty()) { strAction = ""; } else { strAction = getIntent().getAction(); } 语法?:称为 你的代码行 String s
如果操作不为null或空,则返回操作的意图,否则返回空字符串 也可以这样写
String strAction;
if(getIntent().getAction() == null || getIntent().getAction().isEmpty()) {
strAction = "";
} else {
strAction = getIntent().getAction();
}
语法
?:
称为
你的代码行
String strAction = !MyRUtils.isNullOrEmpty(getIntent().getAction())
? getIntent() .getAction()
: "";
相当于:
String strAction = null;
if (!MyRUtils.isNullOrEmpty(getIntent().getAction()) {
strAction = getIntent().getAction();
} else {
strAction = "";
}
正如您所看到的,它只是一些语法上的糖分,可以用来避免冗长的if-else块
String strAction = null;
if (!MyRUtils.isNullOrEmpty(getIntent().getAction()) {
strAction = getIntent().getAction();
} else {
strAction = "";
}