有人能解释一下下面代码中的问号吗? INITIAL_PERMANCE 也是代码中的静态最终常量,但是语法的最后一行被称为什么?
Synapse(AbstractCell inputSource, float permanence) {
_inputSource = inputSource;
_permanence = permanence==0.0 ?
INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);
}
这 ?和 : 是 java 条件运算符的一部分。有时称为三元运算符,因为它是 Java 中唯一接受 3 个参数的运算符。
这本质上是一个内联 IF / THEN / ELSE 块。
_permanence = permanence==0.0 ?
INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);
可以重写如下:
if (permanence == 0.0)
_permanence = INITIAL_PERMANENCE;
else
_permanence = (float) Math.min(1.0,permanence);
条件运算符的一般形式是
<Test returning a boolean> ? <value for if test is true> : <value for if test is false>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)