gpt4 book ai didi

Java 初学者 : the mechanism behind "|" operator in Shell s = new Shell(d, SWT.CLOSE | SWT.调整大小);

转载 作者:行者123 更新时间:2023-12-01 19:18:21 24 4
gpt4 key购买 nike

嗨,我想知道下一个代码中“|”运算符背后的机制。

Display d = new Display( );
Shell s = new Shell(d, SWT.CLOSE | SWT.RESIZE);

p.s: 我已经查过源码了,但是没看懂

最佳答案

SWT.CLOSESWT.RESIZE 是 int 标志。 | 表示它们之间的Binary OR,这意味着传递它们。

例如。如果 RESIZE = 1(00000001 二进制)CLOSE = 2(00000010 二进制)SWT.CLOSE | SWT.RESIZE = 3(00000011 二进制) 稍后调用的方法将知道重新设置它们。

编辑 - 下一步

如果构造函数根据标志运行,它可能如下所示:

public Shell(Display d, int flags)
{
if ((flags & SWT.CLOSE) > 0)//the close flag is on
{ /*do some stuff*/ };
if ((flags & SWT.RESIZE) > 0)
{ /*do some stuff to enable resize..*/ };
}

现在,我们不再向构造函数传递许多不必要的参数,而是用一个参数(标志)告诉它要做很多事情。例如,第一个 if 检查 flags 中是否设置了 CLOSE 标志:flags = 3 (00000011 binary) (正如我们之前设置的那样)SWT.CLOSE = 2(00000010 二进制)flags & SWT.CLOSE = 3 & 2 = 00000010 二进制,它大于零并已设置。

关于Java 初学者 : the mechanism behind "|" operator in Shell s = new Shell(d, SWT.CLOSE | SWT.调整大小);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5606805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com