我无法搜索 | 在谷歌。如果您在您试图解释的软件源代码中找到它,您不知道它的作用,并且您无法向其他人寻求帮助,您将如何找出它的作用?
7 回答
在这种情况下,管道运算符意味着“使用 SWT.APPLICATION_MODAL 和 SWT.OK 作为我的弹出框的选项/标志。” 这是一个非常常用的习语,带有位域配置标识符,尤其是。在 SWT 或 Win32 等窗口系统中。
这个怎么运作
管道 ( | ) 运算符是按位 OR 运算符,也就是说,它计算两个二进制整数值的 OR 运算。如果您查看 APPLICATION_MODAL 和 OK 的定义位置,您会发现它们是这样的:
...
SWT.OK = 1, // 00000001 in binary
SWT.ABORT_RETRY_IGNORE = 2, // 00000010 in binary
SWT.OK_CANCEL = 4; // 00000100 in binary
...
SWT.APPLICATION_MODAL = 32; // 00100000 in binary
... (and so on...)
当您将这些数字中的两个(或更多)按位或在一起时,将为每个选项设置单独的位:
int style = SWT.OK | SWT.APPLICATION_MODAL = 00000001 | 00100000 = 00100001
用于解释样式的窗口工具包将能够通过按位 AND 像这样准确地告诉您想要什么(一个弹出框是 Modal 并有一个 OK 按钮):
...
if(style & SWT.OK)
{
// we want an OK box
}
if(style & SWT.ABORT_RETRY_IGNORE)
{
// we want an Abort/Retry/Ignore box
}
if(style & SWT.OK_CANCEL)
{
// we want an OK/Cancel box
}
...
if(style & SWT.APPLICATION_MODAL)
{
// We want a modal box
}
...
有点聪明,在我的拙见中。它允许您在单个变量中选择/表示多个配置选项。诀窍在于选项的整数定义,并确保它们只是 2 的幂。
在回答您关于找出它的作用的问题时,我首先要对自己进行推理,它是一个符号还是一个运算符。仅此一项就可以帮助我弄清楚要在 Google 中搜索什么。正在使用的变量类型也给了我一个线索——它是一个处理 int 类型的运算符。
希望有帮助。
It is a bitwise OR, it provides a means for you to pass a number of flags to the SWT component as a single int, rather than having to overload the type with loads of setter methods.
The two properties you list indicate you have a dialog or other window that should be displayed modally over the parent shell, and use the ok button. You could combine it with SWT.CANCEL to display an OK and Cancel button for instance
| is the OR operator. Those two values are probably flags that are ints with 1 bit set, and the resulting value is which style values to use.
With most things, there's a bit of a learning curve. If I was learning a new language, I would assume that | was an operator, and would search for 'Java operators'.
使用可以处理它的搜索引擎:A | 乙