是否有某种模式或技巧可以强制对 OR 条件语句中的两个表达式进行求值?
return method1() || 的最佳方式(模式)是什么?如果
不会调用 method1()
返回 true
,method2()method2()
?
示例
我使用此类来绑定表:
class Bounds {
// return true iff bounds changed
boolean set(int start, int end);
}
并且我希望此函数调整行和列的大小,并在任一被修改时返回 true:
public boolean resizeToFirstCell(Bounds rows, Bounds columns) {
return rows.set(0, 1) || columns.set(0, 1);
}
What is the best way (pattern) around return method1() || method2()
not invoking method2()
if method1()
returns true
?
Example
I'm using this class to bound a table:
class Bounds {
// return true iff bounds changed
boolean set(int start, int end);
}
and I want this function to resize both the rows and columns and return true iff either was modified:
public boolean resizeToFirstCell(Bounds rows, Bounds columns) {
return rows.set(0, 1) || columns.set(0, 1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用非短路(有时称为“Eager”)运算符
|
。您可以在操作员文档< /a> 代表
||
(C# 特定链接,但对于 Java 和 C++ 仍然适用)。Use a non-short circuiting (sometimes called "Eager") operator,
|
.You can read more about that in the operator documentation for
||
(C# specific link, but still holds true for Java and C++).这是一个可以很好地扩展更多语句的“模式”:
Here's a "pattern" that scales well for more statements:
请使用 |急切求值而不是短路运算符 http://en.wikipedia.org/wiki/Short- Circuit_evaluation
Please use the | eager evaluation instead of shortcircuit operator http://en.wikipedia.org/wiki/Short-circuit_evaluation