我看到下面的代码这次提交的MongoDB的Java的连接驱动程序,它首次出现时是某种玩笑.以下代码有什么作用?
if (!((_ok) ? true : (Math.random() > 0.1))) {
return res;
}
Run Code Online (Sandbox Code Playgroud)
我已经使用Java几年了,但直到最近我还没有碰到这个结构:
int count = isHere ? getHereCount(index) : getAwayCount(index);
Run Code Online (Sandbox Code Playgroud)
这可能是一个非常简单的问题,但有人可以解释一下吗?我该怎么看?我很确定我知道它是如何工作的.
isHere
是真的,getHereCount()
被称为,isHere
是假getAwayCount()
则调用.正确?这个结构叫做什么?
有人可以解释下面代码中的问号吗?此外,INITIAL_PERMANCE是代码中的静态最终常量,但synatax的最后一行是什么?
Synapse(AbstractCell inputSource, float permanence) {
_inputSource = inputSource;
_permanence = permanence==0.0 ?
INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);
}
Run Code Online (Sandbox Code Playgroud) 我有这两种方法.我理解"getTotalSalary"但却没有真正理解"getAverageSalary"的编写方式.我不明白为什么使用问号和冒号以及"(size()!= 0)"和最后的0.
这是编码:
public double getTotalSalary() {
double total = 0;
for (Employee e : empReg) {
total = total + e.getSalary();
}
return total;
}
public double getAverageSalary() {
return (size() != 0) ? this.getTotalSalary() / this.size() : 0;
}
Run Code Online (Sandbox Code Playgroud)
empReg是ArrayList的名称.员工是由"姓名"和"薪水"组成的班级.getSalary显然是一种返回薪水的方法.