日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HarmonyOS之IDL接口使用规范

發布時間:2024/5/21 编程问答 71 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HarmonyOS之IDL接口使用规范 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、接口描述語言簡介

  • 當客戶端和服務器通信時,需要定義雙方都認可的接口,以保障雙方可以成功通信,HarmonyOS IDL(HarmonyOS Interface Definition Language)則是一種定義此類接口的工具。HarmonyOS IDL 先把需要傳遞的對象分解成操作系統能夠理解的基本類型,并根據開發者的需要封裝跨邊界的對象。在 HarmonyOS 中,HarmonyOS IDL 接口包含面向應用程序的北向接口和面向硬件設備的南向接口。
  • HarmonyOS IDL 接口描述語言:

  • HarmonyOS IDL 接口描述語言主要用于:
    • 聲明系統服務對外提供的服務接口,根據接口聲明在編譯時生成跨進程調用(IPC)或跨設備調用(RPC)的代理(Proxy)和樁(Stub)的 C/C++ 代碼或 Java 代碼。
    • 聲明 Ability 對外提供的服務接口,根據接口聲明在編譯時生成跨進程調用(IPC)或跨設備調用(RPC)的代理(Proxy)和樁(Stub)的 C/C++ 代碼或 Java 代碼。
  • IPC/RPC 通信模型如下:

  • 使用 HarmonyOS IDL 接口描述語言聲明接口具有以下優點:
    • HarmonyOS IDL 中是以接口的形式定義服務,可以專注于定義而隱藏實現細節。
    • HarmonyOS IDL 中定義的接口可以支持跨進程調用或跨設備調用,根據HarmonyOS IDL 中的定義生成的信息或代碼可以簡化跨進程或跨設備調用接口的實現。
  • 采用 HarmonyOS IDL 描述的接口代碼示例如下:
interface ohos.app.IAbilityConnection;interface ohos.os.IBroker;sequenceable ohos.content.AbilityInfo;sequenceable ohos.content.Intent;interface ohos.app.IAbilityManager {int StartAbility([in] Intent intent);void SetAbilitySliceCallback([in] IBroke broker, [in] IAbilityConnection callback);[oneway]void ExitAbility([in] AbilityInfo abilityInfo);}
  • HarmonyOS IDL 接口描述文件是以“.idl”為擴展名的文件。
  • HarmonyOS IDL 接口描述文件目錄層級必須按照包名的層次進行定義,例如:IAbilityManager 類的 IDL 文件必須放在 ohos/app/ 目錄下。
  • HarmonyOS IDL 接口描述文件主要以接口類名命名,例如:IAbilityManager.idl。

二、接口描述語言構成

① 數據類型
  • HarmonyOS IDL 支持的數據類型可分為:基本數據類型、自定義類型、聲明的 Sequenceable 數據類型、聲明的接口類型、數組類型以及容器類型。
  • HarmonyOS IDL 基本數據類型與 Java 數據類型、C/C++ 數據類型的對應關系如下表所示:
HarmonyOS IDL基本數據類型Java數據類型C++數據類型C數據類型數據長度(bytes)
voidvoidvoidvoidNA
booleanbooleanboolbool1
bytebyteint8_tint8_t1
shortshortint16_tint16_t2
intintint32_tint32_t4
longlongint64_tint64_t8
floatfloatfloatfloat4
doubledoubledoubledouble8
StringStringstd::stringcstringNA
unsigned char不支持uint8_tuint8_t1
unsigned short不支持uint16_tuint16_t2
unsigned int不支持uint32_tuint32_t4
unsigned long不支持uint64_tuint64_t8
  • 自定義類型是指使用關鍵字 struct、union、enum 定義的結構體類型、聯合體類型、枚舉類型,以及這些類型組合嵌套定義的類型。自定義類型文件 MyTypes.idl 示例如下:
struct Example1 {String member1;unsigned int member2;};union Example2 {String member1;unsigned int member2;};enum Example3 : int {RED,BLUE,GREEN,};struct Example1_2_3 {struct Example1;union Example2;enum Example3;};
  • 在定義枚舉類型時,需要指定枚舉類型的基本數據類型,可以使用 byte、short、int、long、unsigned char、unsigned short、unsigned int、unsigned long。如果未指定,則默認基礎類型為 int 類型。
  • 一個模塊在定義 HarmonyOS IDL 接口時使用的自定義類型應該定義在一個獨立的文件中,不要分散到每個接口定義的文件中。自定義類型所在文件的名稱可以根據實際情況進行定義,在接口定義文件中需要導入自定義類型文件,例如:MyInterface.idl 接口使用了 MyTypes.idl 中定義的自定義類型,代碼示例如下:
sequenceable ohos.module.MyTypes;interface ohos.module.MyInterface {……}
  • HarmonyOS IDL 自定義數據類型與 Java 數據類型、C/C++ 數據類型的對應關系如下表所示:
自定義數據類型Java數據類型C++數據類型C數據類型
struct不支持structstruct
union不支持unionunion
enum不支持enumenum
  • Sequenceable 數據類型是指用“Sequenceable”關鍵字聲明的非基本數據類型,表明該數據類型可以通過 Parcel 進行跨進程(或跨設備)傳遞。聲明放在文件的頭部,形式為 “sequenceable namespace.typename;”,namespace 是該類型所屬的命名空間,內部采用“.”連接不同層級的命名空間, typename 是類型名。例如:“sequenceable com.huawei.samples.ApplicationInfo;”表示 ApplicationInfo 類型可以通過 Parcel 進行跨進程傳遞。
  • Sequenceable 數據類型并不在 HarmonyOS IDL 文件中定義,而是定義在 C++ 文件或 Java 文件中,C 語言文件不支持。因此,HarmonyOS IDL 工具將根據聲明在生成的 C++ 代碼文件中加入“#include “com/huawei/samples/ApplicationInfo.h””語句,在生成的 Java 代碼文件中加入“import com.huawei.samples.ApplicationInfo;”語句。
  • HarmonyOS IDL Sequenceable 數據類型與 Java 數據類型、C/C++ 數據類型的對應關系如下表所示:
HarmonyOS IDL數據類型Java數據類型C++數據類型C數據類型
Sequenceablesequenceable namespace.typename; 例如:sequenceable com.huawei.samples.ApplicationInfo;#include namespace/typename.h" using namespace::typename 例如:#include com/huawei/samples/ApplicationInfo.h" using com::huawei::samples::ApplicationInfo不支持
  • 接口類型是指 HarmonyOS IDL 文件中定義的接口。對于當前 IDL 文件中定義的接口,可以直接使用它作為方法參數類型或返回值類型。而在其它 HarmonyOS IDL 文件中定義的接口,則需要在文件的頭部進行前置聲明。聲明的形式為“interface namespace.interfacename;”,namespace 是該接口所屬的命名空間,內部采用“.”連接不同層級的命名空間,interfacename 是接口名。例如:“interface com.huawei.samples.IApplication;”聲明了在其他 HarmonyOS IDL 文件定義的 IApplication 接口,該接口可以作為當前定義中方法的參數類型或返回值類型使用。
  • HarmonyOS IDL 工具將根據該聲明在生成的 C++ 代碼文件中加入“#include “com/huawei/samples/IApplication.h””語句,在生成的 Java 代碼文件中加入“import com.huawei.samples.IApplication;”語句。IBroker 也是接口類型的一種,使用它無需前置聲明。
  • HarmonyOS IDL 接口數據類型與Java數據類型、C/C++ 數據類型的對應關系如下表所示:
HarmonyOS IDL數據類型Java數據類型C++數據類型C數據類型
Interfacesequenceable namespace.interfacename; 例如:sequenceable com.huawei.samples.IApplication;#include namespace/interfacename.h" using namespace::interfacename; 例如:#include com/huawei/samples/IApplication.h" using com::huawei::samples::IApplication;不支持
  • 數組類型用“T[]”表示,T可以是基本數據類型、自定義類型、Sequenceable數據類型、接口類型和數組類型。
  • 在 C++ 代碼中,數組類型生成為 std::vector,在Java代碼中,數組類型生成為 T[]。
  • HarmonyOS IDL 數組數據類型與 Java 數據類型、C/C++ 數據類型的對應關系如下表所示:
HarmonyOS IDL數據類型Java數據類型C++數據類型C數據類型
T[]T[]std::vectorT*,int size
  • 容器類型當前支持兩種容器:List 和 Map。List 容器用法為“List”, Map 容器用法為“Map<KT, VT>”,T、KT 和 VT 可以為基本數據類型、自定義類型、Sequenceable 類型、接口類型、數組類型或容器類型。
  • 在 C++ 代碼中,List 容器類型生成為 std::list,Map 容器類型生成為 std::map。在 Java 代碼中,List 容器類型生成為 ArrayList,Map 容器類型生成為 HashMap。
  • HarmonyOS IDL 容器數據類型與 Java 數據類型、C/C++ 數據類型的對應關系如下表所示:
HarmonyOS IDL數據類型Java數據類型C++數據類型C數據類型
ListListstd::list或std::vectorT*,int size
Map<KT, VT>HashMapstd::map或std::unordered map不支持
② 接口定義
  • 一個 HarmonyOS IDL 文件只能定義一個接口類,接口定義的形式使用 BNF 范式描述,示例如下:
<*interface_attributes_declaration*> ? interface <*interface_name_with_namespace*> { <*method_declaration*> ? }
  • <interface_attributes_declaration>是接口屬性聲明,當前支持 oneway、callback、full、lite 屬性:
    • oneway:表示該接口中的方法都是單向方法,即調用方法后不用等待該方法執行即可返回,該屬性為可選項,如果無聲明,則默認為同步調用方法;
    • callback:表示該接口為callback接口,即從下層實現中回調到上層服務中;
    • full、lite:表示該接口在重量級部署或輕量級部署中使用。如果無聲明,則默認為重量級、輕量級部署中都會使用。
  • <interface_name_with_namespace>是接口名聲明,接口名需包含完整的命名空間,且必須包含方法聲明,不允許出現空接口。接口示例:
[oneway] interface ohos.app.IAbilityManager {……};
  • <method_declaration>是方法聲明,形式為:
<*method_attributes_declaration*> ? <*result type*> <*method declarator*>
  • <method_attributes_declaration>是方法屬性聲明,當前支持 oneway 、full、lite 屬性:
    • oneway:表示該方法是單向方法,即調用方法后不用等待該方法執行即可返回;
    • full、lite:表示該方法在重量級部署或輕量級部署中使用。如果無聲明,則默認為重量級、輕量級部署中都會使用。
  • <result type>是返回類型,<method declarator>是方法名和各個參數聲明,參數聲明的形式為:
\[ <*formal_parameter_attribute*> \] <*type*> <*identifier*>
  • <formal_parameter_attribute>的值為“in”、“out”或“inout”,分別表示該參數是輸入參數、輸出參數或輸入輸出參數。oneway 方法不允許有輸出參數(包含輸入輸出參數)和返回值。方法示例:
void SetBundles([in] Map<String, BundleInfo> bundleInfos, [in, out] int number);[oneway] void Dump([in] ParcelableFileDescriptor fd, [in] long flags);
  • 在 HarmonyOS IDL 接口描述文件中可以使用的關鍵字如下表所示:
HarmonyOS IDL keywords詳細說明
void、boolean、byte、short、int、long、float、double基本數據類型,void類型、bool型、整型、浮點型等
unsigned在定義無符號類型時使用,如unsigned char、unsigned short、unsigned int、unsigned long
String定義字符串時使用
struct結構體類型
union聯合體類型
enum枚舉類型
T[]T可以是基本數據類型、自定義類型、Sequenceable數據類型、接口類型和數組類型
List<>容器類型List,T為基本數據類型、自定義類型、Sequenceable類型、接口類型、數組類型或容器類型
Map<>容器類型Map<KT, VT>: KT和VT為基本數據類型、自定義類型、Sequenceable類型、接口類型、數組類型或容器類型
Sequenceable該類型并不在idl文件中定義,而是定義在C++文件或Java文件中
extends接口繼承
interface接口類型
[oneway]表示單向接口或方法
oneway方法不允許有輸出參數(包含輸入輸出參數)和返回值
[in]輸入參數
[out]輸出參數
[inout]輸入輸出參數
[callback]標識一個接口為回調接口定義(明示工具如何生成service、client代碼)
[full]僅在HarmonyOS重量級部署中使用的參數、接口、方法
[lite]僅在HarmonyOS輕量級部署中使用的參數、接口、方法
③ 注釋說明
  • HarmonyOS IDL 文件中的注釋采用與 C/C++ 相同的方式,可以采用//或者/* … */的方式進行注釋說明。

三、開發步驟

① 創建 .idl 文件
  • 可以使用 Java 或 C++ 編程語言構建 .idl 文件。.idl 示例如下:
sequenceable com.example/** Example service ability interface */interface com.example.IRemoteAbility {int plus([in] int num1, [in] int num2);}
  • HarmonyOS SDK 工具支持生成 Java、C++ 語言的接口類、樁類和代理類。以 Java 語言開發為例,開發者只需將 .idl 文件保存至 DevEco Studio 項目的 src/ 目錄內,工具則會在構建應用時,在項目的 generated/ 目錄中生成 IRemoteObject 接口文件、Stub 文件、Proxy 文件。生成的接口類文件名稱和.idl文件名稱保持一致,區別在于其使用 .java 擴展名。例如,IRemoteAbility.idl 生成的文件名是 IRemoteAbility.java、RemoteAbilityProxy.java、RemoteAbilityStub.java。
② 服務端公開接口
  • HarmonyOS IDL 工具生成的 Stub 類是接口類的抽象實現,并且會聲明 .idl 文件中的所有方法:
public abstract class RemoteAbilityStub extends RemoteObject implements IRemoteAbility {private static final String DESCRIPTOR = "com.example.IRemoteAbility";private static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID + 0;………}
  • 要實現 .idl 生成的接口,請擴展生成的 RemoteObject 接口,并實現繼承自 .idl 文件的方法。MyRemote 定義了服務的遠程過程調用接口,示例代碼如下:
class MyRemote extends RemoteAbilityStub {public MyRemote(String descriptor) {super(descriptor);}@Overridepublic int plus(int num1, int num2) throws RemoteException {return num1 + num2;}}
  • 在服務實現接口后,需要向客戶端公開該接口,以便客戶端進程綁定。如果開發者的服務要公開該接口,請擴展 Ability 并實現 onConnect() 從而返回 IRemoteObject,以便客戶端能與服務進程交互。服務端向客戶端公開 IRemoteAbility 接口的代碼示例如下:
public class ServiceAbility extends Ability {private MyRemote remote = new MyRemote("MyRemoteAbility");class MyRemote extends RemoteAbilityStub {public MyRemote(String descriptor) {super(descriptor);}@Overridepublic int plus(int num1, int num2) throws RemoteException {return num1 + num2;}}@Overridepublic IRemoteObject onConnect(Intent intent) {return remote.asObject();}}
③ 客戶端調用 IPC 方法
  • 客戶端調用 connectAbility() 以連接服務時,客戶端的 onAbilityConnectDone() 回調會接收服務的 onConnect() 方法返回的 IRemoteObject 實例。由于客戶端和服務在不同應用內,所以客戶端應用的 src/ 目錄內必須包含 .idl 文件(SDK 工具會自動生成 Proxy 代理類)的副本。當客戶端 onAbilityConnectDone() 回調中收到 IRemoteObject,調用 RemoteAbilityStub.asInterface(IRemoteObject),以將返回的參數轉換為 IRemoteAbility 類型,例如:
IRemoteAbility proxy;private IAbilityConnection conn = new IAbilityConnection() {@Overridepublic void onAbilityConnectDone(ElementName element, IRemoteObject remote, int resultCode) {proxy = IRemoteAbilityStub.asInterface(remote);}@Overridepublic void onAbilityDisconnectDone(ElementName element, int resultCode) {proxy = null;}};
  • 如果要斷開連接,請調用 Ability.disconnectAbility()。
④ IPC 傳遞 Sequenceable 對象
  • 開發者可以通過 IPC 接口,將某個類從一個進程發送至另一個進程。但是,必須確保 IPC 通道的另一端可使用該類的代碼,并且該類必須支持 Sequenceable 接口。HarmonyOS 需要通過該 Sequenceable 接口將對象反序列化成各進程能識別的對象。
  • 如需創建支持 Sequenceable 接口的類,開發者必須執行以下操作:
    • 自定義對象實現 Sequenceable 接口;
    • 實現 marshalling 方法,它會獲取對象的當前狀態并將其序列化后寫入 Parcel;
    • 實現 unmarshalling 方法,它會從 Parcel 中反序列化出對象;
  • ElementName.java 類實現 Sequenceable 接口的代碼示例如下:
public class ElementName implements Sequenceable {private String deviceId = "";private String bundleName = "";private String abilityName = "";public ElementName() {}public ElementName(String deviceId, String bundleName, String abilityName) {this.deviceId = deviceId;this.bundleName = bundleName;this.abilityName = abilityName;}@Overridepublic boolean marshalling(Parcel out) {if (!out.writeString(bundleName)) {return false;}if (!out.writeString(abilityName)) {return false;}if (!out.writeString(deviceId)) {return false;}return true;}@Overridepublic boolean unmarshalling(Parcel in) {this.bundleName = in.readString();this.abilityName = in.readString();this.deviceId = in.readString();return true;}

四、IDL 語法規則

HarmonyOS IDL語法規則(BNF范式描述)HarmonyOS IDL語法規則(文字描述)示例### Programs一個HarmonyOS IDL接口定義文件由三部分組成:包名、類型聲明、接口定義。
IDL接口文件擴展名為*.idl。
一個文件只能定義一個接口,多個接口需要定義在多個不同的文件中。
文件名必須和接口名一致。
自定義類型單獨成一個文件,該文件名可以按需定義。-<*package_declaration*> :: = package <*package_name_with_namespace*> ;包名package ohos.app;<*package_name_with_namespace*> ::= <*namespace*> . <*package_name*> | <*package_name*>--<*type_declarations*> ::= <*java_like_type_declarations*> | <*c_like_type_declarations*>類型聲明-<*java_like_type_declarations*> ::= <*sequenceable_class_declaration*>? <*package_type_declaration*>? <*interface_declaration*>?類Java語言的類型聲明,包括:sequenceable類型聲明、包類型聲明、接口類型聲明-<*sequenceable_class_declaration*> ::= sequenceable <*sequenceable_class_name_with_namespace*> ;sequenceable類型聲明sequenceable ohos.content.AbilityInfo;<*sequenceable_class_name_with_namespace*> ::= <*namespace*> . <*sequenceable_class_name*> | <*sequenceable_class_name*>--<*c_like_type_declarations*> ::= <*package_type_declaration*>? <*custom_type_declaration*>? <*interface_declaration*>?類C語言的類型聲明,包括包類型聲明、自定義類型聲明、接口類型聲明-<*custom_type_file_name_with_namespace*> ::= <*namespace*> . <*custom_type_file_name*> | <*custom_type_file_name*>--<*interface_declaration*> ::= interface <*interface_name_with_namespace*> ;接口類型聲明interface ohos.app.IAbilitySliceConnection;<*interface_name_with_namespace*> ::= <*namespace*> . <*interface_name*> | <*interface_name*>--<*interface_definition*> ::= <*interface_attributes_declaration*>? interface <*interface_name_with_namespace*> <*interface_inheritance_declaration*>? { <*method_declaration*>+ };接口定義,包括:接口屬性、接口名稱、接口繼承聲明、接口函數interface ohos.app.IAbilityManager { int StartAbility([in] Intent intent); …… };<*interface_attributes_declaration*> ::= \ [ <*interface_attributes*> \ ]接口屬性-<*interface_attributes*> ::= <*interface_attribute*> | <*interface_attributes*> , <*interface_attribute*>--<*interface_attribute*> ::= <*java_like_interface_attribute*> | <*c_like_interface_attribute*>--<*java_like_interface_attribute*> ::= oneway類Java語言的屬性支持:oneway-<*c_like_interface_attribute*> ::= <*java_like_interface_attribute*> | callback | full | lite類C語言的屬性,支持:oneway、callback、full、lite-<*interface_inheritance_declaration*> ::= extends <*parent_interface_name_with_namespace*>接口繼承聲明interface ohos.app.IAbilityManager extends ohos.app.IManager { …… };<*parent_interface_name_with_namespace*> ::= <*interface_name_with_namespace*>父接口-<*method_declaration*> ::= <*method_attributes_declaration*>? <*result_type*> <*method_declarator*> ;接口函數聲明,包括:函數屬性、返回值、函數名稱、參數列表-<*method_attributes_declaration*> ::= \ [ <*method_attributes*> \ ]函數屬性-<*method_attributes*> ::= <*method_attribute*> | <*method_attributes*> , <*method_attribute*>--<*method_attribute*> ::= <*java_like_method_attribute*> | <*c_like_method_attribute*>--<*java_like_method_attribute*> ::= oneway類Java語言的函數屬性支持:oneway-<*c_like_method_attribute*> ::= <*java_like_method_attribute*> | full | lite類C語言的函數屬性,支持:oneway、full、lite-<*result_type*> ::= <*type*> | void返回值:可以沒有返回值(void),返回一個指定類型的值-<*method_declarator*> ::= <*method_name*> ( <*formal_parameters*>? )接口函數:可以沒有參數-<*formal_parameters*> ::= <*formal_parameter*> | <*formal_parameters*> , <*formal_parameter*>--<*formal_parameter*> ::= \ [ <*formal_parameter_attribute*> \ ] <*type*> <*formal_parameter_name*>每個函數參數由三個部分組成:參數屬性、參數類型、參數名稱-<*formal_parameter_attribute*> ::= in | out | inout<參數屬性,支持:in、out、inout/th> -### Types--<*type*> ::= <*java_like_type*> | <*c_like_type*>--<*java_like_type*> ::= <*primitive_type*> | <*string_type*> | <*sequenceable_type*> | <*interface_type*> | <*list_type*> | <*map_type*> | <*array_type*>類Java語言的數據類型,包括:基本類型、String類型、sequenceable類型、接口類型、List容器類型、Map容器類型、數組類型-<*c_like_type*> ::= <*primitive_type*> | <*string_type*> | <*interface_type*> | <*list_type*> | <*array_type*> | <*custom_type*>類C語言的數據類型,包括:基本類型、String類型、接口類型、List容器類型、數組類型、自定義類型-<*primitive_type*> ::= <*java_like_primitive_type*> | <*c_like_primitive_type*>--<*java_like_primitive_type*> ::= boolean | byte | short | int | long | float | double類Java語言的基本類型-<*c_like_primitive_type*> ::= <*java_like_primitive_type*> | unsigned char | unsigned short | unsigned int | unsigned long類C語言的基本類型:除類Java語言的基本類型外,還可以使用無符號類型-<*string_type*> ::= StringString類型-<*sequenceable_type*> ::= <*sequenceable_class_name*>sequenceable類型-<*interface_type*> ::= <*interface_name*>interface接口類型-<*list_type*> ::= List < <*type*> ><*list_type*> ::= List < <*type*> >-<*map_type*> ::= Map < <*type*> , <*type*> >Map容器類型-<*array_type*> ::= <*type*> \ [ \ ] 數組類型-<*custom_type*> ::= <*enum_type*> | <*struct_type*> | <*union_type*>自定義類型,包括:枚舉類型、結構體類型、聯合體類型、三種類型的嵌套使用-<*enum_type*> ::= <*custom_type_attributes_declaration*>? enum <*custom_type_name*> { <*enum_members_declaration*>+ };枚舉類型:自定義類型屬性、類型名、枚舉成員-<*struct_type*> ::= <*custom_type_attributes_declaration*>? struct <*custom_type_name*> { <*custom_type_members_declaration*>+ };結構體類型:自定義類型屬性、類型名、結構體成員-<*union_type*> ::= <*custom_type_attributes_declaration*>? union <*custom_type_name*> { <*custom_type_members_declaration*>+ };聯合體類型:自定義類型屬性、類型名、聯合體成員-<*custom_type_attributes_declaration*> ::= \ [ <*custom_type_attributes*> \ ]--<*custom_type_attributes*> ::= <*custom_type_attribute*> | <*custom_type_attributes*> , <*custom_type_attribute*>--<*custom_type_attribute*> ::= full | lite自定義類型的屬性,支持:full、lite-<*enum_members_declaration*> ::= <*custom_type_attributes_declaration*>? <*enum_member_name*> <*enum_member_value*>? ,枚舉類型成員:自定義類型屬性、枚舉成員名、枚舉成員值(可選)-<*enum_member_value*> ::= = <*number_value*>枚舉成員值-<*custom_type_members_declaration*> ::= <*primitive_type_member*> | <*list_type_member*> | <*array_type_member*> | <*custom_type_member*>自定義類型成員,支持:基本類型、List容器類型、數組類型、自定義類型-<*primitive_type_member*> ::= <*custom_type_attributes_declaration*>? <*c_like_primitive_type*> <*custom_type_member_name*> ;基本類型成員:自定義類型屬性、數據類型、自定義成員變量名-<*list_type_member*> ::= <*custom_type_attributes_declaration*>? <*custom_list_type*> <*custom_type_member_name*> ;List容器類型成員:自定義類型屬性、數據類型、自定義成員變量名-<*array_type_member*> ::= <*custom_type_attributes_declaration*>? <*custom_array_type*> <*custom_type_member_name*> ;數組類型成員:自定義類型屬性、數據類型、自定義成員變量名-<*custom_type_member*> ::= <*custom_type_attributes_declaration*>? <*custom_type*> <*custom_type_member_name*> ;自定義類型成員:自定義類型屬性、數據類型、自定義成員變量名-<*custom_list_type*> ::= List < <*custom_member_type*> >自定義類型中使用的List容器類型-<*custom_array_type*> ::= <*custom_member_type*> \ [ \ ]自定義類型中使用的數組類型-<*custom_member_type*> ::= <*c_like_primitive_type*> | <*string_type*> | <*custom_type*>自定義成員類型:類C語言的基本類型、字符串類型、自定義類型、不支持接口類型、Map容器類型等-### Tokens--<*package_name*> ::= <*identifier*>包名-<*custom_type_file_name*> ::= <*identifier*>自定義類型所在的文件名-<*namespace*> ::= <*identifier*> | <*namespace*> . <*identifier*>命名空間-<*sequenceable_class_name*> ::= <*identifier*>sequenceable類名-<*interface_name*> ::= <*identifier*>接口名-<*method_name*> ::= <*identifier*>接函數口名-<*formal_parameter_name*> ::= <*identifier*>參數名-<*custom_type_name*> ::= <*identifier*>自定義類型名-<*enum_member_name*> ::= <*enum_letter*>+枚舉成員名-<*custom_type_member_name*> ::= <*identifier*>自定義類型成員名-### identifier--<*identifier*> ::= <*letter*>+ | <*letter_number*>標識符由字母和數字組成,第一個字符不能使用數字-<*letter*> ::= _ | <*upper_case*> | <*lower_case*>字符,包括:下滑線、大寫字母、小寫字母-<*letter_number*> ::= <*letter*>+ | <*letter_number*> <*letter*> | <*letter_number*> <*number*>--<*upper_case*> ::= A|B|C|D|E|F|G|H|I|J …… --<*lower_case*> ::= a|b|c|d|e|f|g|h|i|j ……--<*number*> ::= <*dec_number*>--<*dec_number*> ::= <*oct_number*> | 8 | 9十進制數字-<*oct_number*> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7八進制數字-<*number_value*> ::= <*hex_prefix*> <*hex_value*> | <*dec_value*> | 0 <*oct_value*>數值:枚舉類型成員的值-<*hex_prefix*> ::= 0x | 0X--<*hex_value*> ::= <*hex_number*>+--<*dec_value*> ::= <*dec_number*>+--<*oct_value*> ::= <*oct_number*>+--<*hex_number*> ::= <*dec_number*> | A | B | C | D | E | F | a | b | c | d | e | f十六進制數字-<*enum_letter*> ::= _ | <*upper_case*>枚舉類型成名的命名,可以使用:下滑線、大寫字母-
  • 語法文檔使用的規則如下:
    • 尖括號 < > 內包含的為必選項;
    • 豎線 | 表示在其左右兩邊任選一項,相當于"OR"的含義;
    • ::= 是“被定義為”的含義;
    • ? 操作符左邊的符號是可選項(可以出現 0 到多次);
    • “+ 1”次或多次;
    • \ [或\ ]是左右中括號字符本身。

總結

以上是生活随笔為你收集整理的HarmonyOS之IDL接口使用规范的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。