Java的:三元没有返回。 (对于方法调用) [英] Java: Ternary with no return. (For method calling)
问题描述
但我想知道是否有可能做一个三元操作,不返回任何东西。
如果它不是在Java中可能是有可能在其他语言,如果是哪些应用?
name.isChecked()? name.setChecked(真):name.setChecked(假);
没有,你不能。但是,什么是这一个多的if-else
语句的意义呢?难道你真的想救7个字符?
如果(name.isChecked()){
name.setChecked(真);
}其他{
name.setChecked(假);
}
或者如果你preFER不良作风:
如果(name.isChecked())name.setChecked(真);否则name.setChecked(假);
没关系的事实,你可以这样做(在这种情况下):
name.setChecked(name.isChecked());
三元或有条件的运营商的关键是条件句引入一个的前pression 的。换句话说,这样的:
INT最大= A> B' A:B;
,就是要简写本:
INT最大;
如果(A> B){
MAX = A;
}其他{
最大= B;
}
如果有正在生产没有价值,条件运算符是不是一条捷径。
I was wondering if it was possible to do a ternary operation but without returning anything.
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);
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;
is meant to be shorthand for this:
int max;
if ( a > b ) {
max = a;
} else {
max = b;
}
If there is no value being produced, the conditional operator is not a shortcut.
这篇关于Java的:三元没有返回。 (对于方法调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!