java Protobuf 中的默认枚举值是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5369093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:47:11  来源:igfitidea点击:

What's the default enum value in Protobuf?

javaprotocol-buffers

提问by why

Hello What's the default enum value (if there isn't any default value defined) in Google Protocol buffer using with Java?

您好 与 Java 一起使用的 Google Protocol 缓冲区中的默认枚举值(如果没有定义任何默认值)是什么?

回答by Marc Gravell

It is the first one defined in .proto order.

它是第一个以 .proto 顺序定义的。

From the .proto language guide (since all implementations use the same logic here):

来自 .proto 语言指南(因为这里的所有实现都使用相同的逻辑):

Optional Fields And Default Values

(snip) For enums, the default value is the first value listed in the enum's type definition.

可选字段和默认值

(snip) 对于枚举,默认值是枚举类型定义中列出的第一个值。

回答by dcn

from the official spec:

来自官方规范

optional: the field may or may not be set. If an optional field value isn't set, a default value is used. For simple types, you can specify your own default value, as we've done for the phone number type in the example. Otherwise, a system default is used: zero for numeric types, the empty string for strings, false for bools. For embedded messages, the default value is always the "default instance" or "prototype" of the message, which has none of its fields set. Calling the accessor to get the value of an optional (or required) field which has not been explicitly set always returns that field's default value.

可选:该字段可以设置也可以不设置。如果未设置可选字段值,则使用默认值。对于简单类型,您可以指定自己的默认值,就像我们在示例中为电话号码类型所做的那样。否则,使用系统默认值:数字类型为零,字符串为空字符串,布尔值为 false。对于嵌入的消息,默认值始终是消息的“默认实例”或“原型”,它没有设置任何字段。调用访问器以获取尚未显式设置的可选(或必需)字段的值始终返回该字段的默认值。

You can set a default value as follows:

您可以按如下方式设置默认值:

optional PhoneType type = 2 [default = HOME];