Gar*_*all 2 c# c++ java design-patterns
如果返回,return method1() || method2()不调用的最佳方式(模式)是什么?method2()method1()true
例
我正在使用这个类绑定一个表:
class Bounds {
// return true iff bounds changed
boolean set(int start, int end);
}
Run Code Online (Sandbox Code Playgroud)
我希望此函数调整行和列的大小,并返回true iff被修改:
public boolean resizeToFirstCell(Bounds rows, Bounds columns) {
return rows.set(0, 1) || columns.set(0, 1);
}
Run Code Online (Sandbox Code Playgroud)
使用非短路(有时称为"Eager")运算符|.
public boolean resizeToFirstCell(Bounds rows, Bounds columns) {
return rows.set(0, 1) | columns.set(0, 1);
}
Run Code Online (Sandbox Code Playgroud)
你可以阅读更多有关的在操作文档为||(C#具体环节,但对于Java和C++依然如此).
public boolean resizeToFirstCell(Bounds rows, Bounds columns) {
// Intermediate values are used to explicitly avoid short-circuiting.
bool rowSet = rows.set(0, 1);
bool columnSet = columns.set(0, 1);
return rowSet || columnSet;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
604 次 |
| 最近记录: |