Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
MessageField 類別
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
MessageField 類別是用於定義訊息,以便提高整個網路或程序空間中的傳輸效率。訊息可使用欄位類別加以定義。
MessageField
由 protorpc.messages
模組提供。
建構函式
MessageField 類別的建構函式定義如下:
-
class MessageField(message_type,
number,
[required=False | repeated=False])
-
定義子訊息值的欄位。
引數
- message_type
- 欄位的訊息類型。必須是 Message 的子類別。
- number
- 欄位編號。每種訊息類別的欄位號碼皆必須與其他類別不同。
- required=False
- 這個欄位是否為必填欄位。與
repeated
引數互斥;若使用 required=True
,請勿指定 repeated=True
。
- repeated=False
- 這個欄位是否為重複欄位。與
required
引數互斥;如果使用 repeated=True
,請勿指定 required=True
。
如果 message_type
無效,則會引發 FieldDefinitionError。
類別屬性
MessageField 類別提供下列屬性:
- type
- 用於此欄位值的 Python 類型。舉例來說,如果是 DateTimeField,
type
就是 datetime.datetime
。對於使用者定義的 MessageFields,type
是指定的 Message 類型。
- message_type
- 用於序列化的基礎訊息類型。具體來說,這是您可在 Message 類別的執行個體上儲存的類型。舉例來說,如果是 DateTimeField,類型則會是
message_types.DateTimeMessage
。如果是一般訊息欄位,則會是 protorpc 訊息子類別。例如:
class Sub(messages.Message):
x = messages.IntegerField(1)
class M(messages.Message):
sub = messages.MessageField(Sub, 1)
dt = message_types.DateTimeField(2)
print 'M.sub.type =', M.sub.type
print 'M.sub.message_type =', M.sub.message_type
print 'M.dt.type =', M.dt.type
print 'M.dt.message_type =', M.dt.message_type
=== output ===
M.sub.type = <class '__main__.Sub>
M.sub.message_type = <class '__main__.Sub'>
M.dt.type = <type 'datetime.datetime'>
M.dt.message_type = <class 'protorpc.message_types.DateTimeMessage'>
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-19 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-19 (世界標準時間)。"],[[["\u003cp\u003eThe \u003ccode\u003eMessageField\u003c/code\u003e class defines fields for sub-message values, enabling the efficient transmission of messages across network or process space.\u003c/p\u003e\n"],["\u003cp\u003eThe constructor of \u003ccode\u003eMessageField\u003c/code\u003e requires the \u003ccode\u003emessage_type\u003c/code\u003e and \u003ccode\u003enumber\u003c/code\u003e arguments, with optional \u003ccode\u003erequired\u003c/code\u003e and \u003ccode\u003erepeated\u003c/code\u003e arguments that are mutually exclusive.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003emessage_type\u003c/code\u003e specifies the message type of the field, and it must be a subclass of \u003ccode\u003eMessage\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003etype\u003c/code\u003e property of \u003ccode\u003eMessageField\u003c/code\u003e indicates the Python type used for the field's values, while \u003ccode\u003emessage_type\u003c/code\u003e reveals the underlying message type used for serialization.\u003c/p\u003e\n"]]],[],null,[]]