gpt4 book ai didi

Java,枚举 : (short) - someNumber

转载 作者:行者123 更新时间:2023-11-30 06:58:52 25 4
gpt4 key购买 nike

我正在寻找 Apache Storm 的源代码,我遇到了一些我从未见过的东西:CLOSE_MESSAGE((short)-100) 或 CLOSE_MESSAGE((short)-100 ).为什么要从 short 类型中减去 someNumber?这是什么意思?这是实际代码:

enum ControlMessage {
CLOSE_MESSAGE((short)-100),
EOB_MESSAGE((short)-201),
OK_RESPONSE((short)-200),
FAILURE_RESPONSE((short)-400),
SASL_TOKEN_MESSAGE_REQUEST((short)-202),
SASL_COMPLETE_REQUEST((short)-203);

private short code;

//private constructor
private ControlMessage(short code) {
this.code = code;
}

/**
* Return a control message per an encoded status code
* @param encoded
* @return
*/
static ControlMessage mkMessage(short encoded) {
for(ControlMessage cm: ControlMessage.values()) {
if(encoded == cm.code) return cm;
}
return null;
}

int encodeLength() {
return 2; //short
}

/**
* encode the current Control Message into a channel buffer
* @throws Exception
*/
ChannelBuffer buffer() throws IOException {
ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encodeLength()));
write(bout);
bout.close();
return bout.buffer();
}

void write(ChannelBufferOutputStream bout) throws IOException {
bout.writeShort(code);
}
}

最佳答案

不,我不认为它在减去数字。它正在转换 intshort!枚举有一个接受 short 作为参数的构造函数,所以它只是意味着将负整数传递给构造函数!

关于Java,枚举 : (short) - someNumber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202576/

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