Java:没有回报的三元.(用于方法调用)
我想知道是否可以进行三元运算但不返回任何内容.
I was wondering if it was possible to do a ternary operation but without returning anything.
如果在 Java 中不可能,那么在其他语言中是否可能,如果可以,哪些适用?
If it's not possible in Java is it possible in other languages, if so which ones apply?
name.isChecked() ? name.setChecked(true):name.setChecked(false);
推荐答案
不,你不能.但是,与 if-else
语句相比,这有什么意义呢?您真的要保存 7 个字符吗?
No, you can't. But what's the point of this over an if-else
statement? Are you really trying to save 7 characters?
if (name.isChecked()) {
name.setChecked(true);
} else {
name.setChecked(false);
}
或者如果你喜欢糟糕的风格:
or if you prefer bad style:
if (name.isChecked()) name.setChecked(true); else name.setChecked(false);
别介意你可以做(在这种情况下):
Never mind the fact that you can just do (in this case):
name.setChecked(name.isChecked());
三元或条件"运算符的重点是将条件引入表达式.换句话说,这是:
The point of the ternary or "conditional" operator is to introduce conditionals into an expression. In other words, this:
int max = a > b ? a : b;
是这个的简写:
int max;
if ( a > b ) {
max = a;
} else {
max = b;
}
如果没有产生值,则条件运算符不是快捷方式.
If there is no value being produced, the conditional operator is not a shortcut.
相关文章