这个函数在getAction()之后特意做了什么问题?
String strAction = !MyRUtils.isNullOrEmpty(getIntent().getAction()) ? getIntent() .getAction() : "";
答案 0 :(得分:1)
如果操作不为null也不为空,则返回操作的意图,否则返回空字符串。
它也可以这样写。
String strAction;
if(getIntent().getAction() == null || getIntent().getAction().isEmpty()) {
strAction = "";
} else {
strAction = getIntent().getAction();
}
答案 1 :(得分:0)
语法?:
称为ternary operator。
您的代码行
String strAction = !MyRUtils.isNullOrEmpty(getIntent().getAction())
? getIntent() .getAction()
: "";
相当于:
String strAction = null;
if (!MyRUtils.isNullOrEmpty(getIntent().getAction()) {
strAction = getIntent().getAction();
} else {
strAction = "";
}
如你所见,它只是一些语法糖,可以用来避免冗长的if-else阻塞。