生活随笔
收集整理的這篇文章主要介紹了
HarmonyOS之IDL接口使用规范
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、接口描述語言簡(jiǎn)介
當(dāng)客戶端和服務(wù)器通信時(shí),需要定義雙方都認(rèn)可的接口,以保障雙方可以成功通信,HarmonyOS IDL(HarmonyOS Interface Definition Language)則是一種定義此類接口的工具。HarmonyOS IDL 先把需要傳遞的對(duì)象分解成操作系統(tǒng)能夠理解的基本類型,并根據(jù)開發(fā)者的需要封裝跨邊界的對(duì)象。在 HarmonyOS 中,HarmonyOS IDL 接口包含面向應(yīng)用程序的北向接口和面向硬件設(shè)備的南向接口。 HarmonyOS IDL 接口描述語言:
HarmonyOS IDL 接口描述語言主要用于: 聲明系統(tǒng)服務(wù)對(duì)外提供的服務(wù)接口,根據(jù)接口聲明在編譯時(shí)生成跨進(jìn)程調(diào)用(IPC)或跨設(shè)備調(diào)用(RPC)的代理(Proxy)和樁(Stub)的 C/C++ 代碼或 Java 代碼。 聲明 Ability 對(duì)外提供的服務(wù)接口,根據(jù)接口聲明在編譯時(shí)生成跨進(jìn)程調(diào)用(IPC)或跨設(shè)備調(diào)用(RPC)的代理(Proxy)和樁(Stub)的 C/C++ 代碼或 Java 代碼。 IPC/RPC 通信模型如下:
使用 HarmonyOS IDL 接口描述語言聲明接口具有以下優(yōu)點(diǎn): HarmonyOS IDL 中是以接口的形式定義服務(wù),可以專注于定義而隱藏實(shí)現(xiàn)細(xì)節(jié)。 HarmonyOS IDL 中定義的接口可以支持跨進(jìn)程調(diào)用或跨設(shè)備調(diào)用,根據(jù)HarmonyOS IDL 中的定義生成的信息或代碼可以簡(jiǎn)化跨進(jìn)程或跨設(shè)備調(diào)用接口的實(shí)現(xiàn)。 采用 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”為擴(kuò)展名的文件。 HarmonyOS IDL 接口描述文件目錄層級(jí)必須按照包名的層次進(jìn)行定義,例如:IAbilityManager 類的 IDL 文件必須放在 ohos/app/ 目錄下。 HarmonyOS IDL 接口描述文件主要以接口類名命名,例如:IAbilityManager.idl。
二、接口描述語言構(gòu)成
① 數(shù)據(jù)類型
HarmonyOS IDL 支持的數(shù)據(jù)類型可分為:基本數(shù)據(jù)類型、自定義類型、聲明的 Sequenceable 數(shù)據(jù)類型、聲明的接口類型、數(shù)組類型以及容器類型。 HarmonyOS IDL 基本數(shù)據(jù)類型與 Java 數(shù)據(jù)類型、C/C++ 數(shù)據(jù)類型的對(duì)應(yīng)關(guān)系如下表所示:
HarmonyOS IDL基本數(shù)據(jù)類型Java數(shù)據(jù)類型C++數(shù)據(jù)類型C數(shù)據(jù)類型數(shù)據(jù)長度(bytes) void void void void NA boolean boolean bool bool 1 byte byte int8_t int8_t 1 short short int16_t int16_t 2 int int int32_t int32_t 4 long long int64_t int64_t 8 float float float float 4 double double double double 8 String String std::string cstring NA unsigned char 不支持 uint8_t uint8_t 1 unsigned short 不支持 uint16_t uint16_t 2 unsigned int 不支持 uint32_t uint32_t 4 unsigned long 不支持 uint64_t uint64_t 8
自定義類型是指使用關(guān)鍵字 struct、union、enum 定義的結(jié)構(gòu)體類型、聯(lián)合體類型、枚舉類型,以及這些類型組合嵌套定義的類型。自定義類型文件 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
; } ;
在定義枚舉類型時(shí),需要指定枚舉類型的基本數(shù)據(jù)類型,可以使用 byte、short、int、long、unsigned char、unsigned short、unsigned int、unsigned long。如果未指定,則默認(rèn)基礎(chǔ)類型為 int 類型。 一個(gè)模塊在定義 HarmonyOS IDL 接口時(shí)使用的自定義類型應(yīng)該定義在一個(gè)獨(dú)立的文件中,不要分散到每個(gè)接口定義的文件中。自定義類型所在文件的名稱可以根據(jù)實(shí)際情況進(jìn)行定義,在接口定義文件中需要導(dǎo)入自定義類型文件,例如:MyInterface.idl 接口使用了 MyTypes.idl 中定義的自定義類型,代碼示例如下:
sequenceable ohos
. module
. MyTypes
; interface ohos
. module
. MyInterface
{ ……
}
HarmonyOS IDL 自定義數(shù)據(jù)類型與 Java 數(shù)據(jù)類型、C/C++ 數(shù)據(jù)類型的對(duì)應(yīng)關(guān)系如下表所示:
自定義數(shù)據(jù)類型Java數(shù)據(jù)類型C++數(shù)據(jù)類型C數(shù)據(jù)類型 struct 不支持 struct struct union 不支持 union union enum 不支持 enum enum
Sequenceable 數(shù)據(jù)類型是指用“Sequenceable”關(guān)鍵字聲明的非基本數(shù)據(jù)類型,表明該數(shù)據(jù)類型可以通過 Parcel 進(jìn)行跨進(jìn)程(或跨設(shè)備)傳遞。聲明放在文件的頭部,形式為 “sequenceable namespace.typename;”,namespace 是該類型所屬的命名空間,內(nèi)部采用“.”連接不同層級(jí)的命名空間, typename 是類型名。例如:“sequenceable com.huawei.samples.ApplicationInfo;”表示 ApplicationInfo 類型可以通過 Parcel 進(jìn)行跨進(jìn)程傳遞。 Sequenceable 數(shù)據(jù)類型并不在 HarmonyOS IDL 文件中定義,而是定義在 C++ 文件或 Java 文件中,C 語言文件不支持。因此,HarmonyOS IDL 工具將根據(jù)聲明在生成的 C++ 代碼文件中加入“#include “com/huawei/samples/ApplicationInfo.h””語句,在生成的 Java 代碼文件中加入“import com.huawei.samples.ApplicationInfo;”語句。 HarmonyOS IDL Sequenceable 數(shù)據(jù)類型與 Java 數(shù)據(jù)類型、C/C++ 數(shù)據(jù)類型的對(duì)應(yīng)關(guān)系如下表所示:
HarmonyOS IDL數(shù)據(jù)類型Java數(shù)據(jù)類型C++數(shù)據(jù)類型C數(shù)據(jù)類型 Sequenceable sequenceable 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 文件中定義的接口。對(duì)于當(dāng)前 IDL 文件中定義的接口,可以直接使用它作為方法參數(shù)類型或返回值類型。而在其它 HarmonyOS IDL 文件中定義的接口,則需要在文件的頭部進(jìn)行前置聲明。聲明的形式為“interface namespace.interfacename;”,namespace 是該接口所屬的命名空間,內(nèi)部采用“.”連接不同層級(jí)的命名空間,interfacename 是接口名。例如:“interface com.huawei.samples.IApplication;”聲明了在其他 HarmonyOS IDL 文件定義的 IApplication 接口,該接口可以作為當(dāng)前定義中方法的參數(shù)類型或返回值類型使用。 HarmonyOS IDL 工具將根據(jù)該聲明在生成的 C++ 代碼文件中加入“#include “com/huawei/samples/IApplication.h””語句,在生成的 Java 代碼文件中加入“import com.huawei.samples.IApplication;”語句。IBroker 也是接口類型的一種,使用它無需前置聲明。 HarmonyOS IDL 接口數(shù)據(jù)類型與Java數(shù)據(jù)類型、C/C++ 數(shù)據(jù)類型的對(duì)應(yīng)關(guān)系如下表所示:
HarmonyOS IDL數(shù)據(jù)類型Java數(shù)據(jù)類型C++數(shù)據(jù)類型C數(shù)據(jù)類型 Interface sequenceable 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; 不支持
數(shù)組類型用“T[]”表示,T可以是基本數(shù)據(jù)類型、自定義類型、Sequenceable數(shù)據(jù)類型、接口類型和數(shù)組類型。 在 C++ 代碼中,數(shù)組類型生成為 std::vector,在Java代碼中,數(shù)組類型生成為 T[]。 HarmonyOS IDL 數(shù)組數(shù)據(jù)類型與 Java 數(shù)據(jù)類型、C/C++ 數(shù)據(jù)類型的對(duì)應(yīng)關(guān)系如下表所示:
HarmonyOS IDL數(shù)據(jù)類型Java數(shù)據(jù)類型C++數(shù)據(jù)類型C數(shù)據(jù)類型 T[] T[] std::vector T*,int size
容器類型當(dāng)前支持兩種容器:List 和 Map。List 容器用法為“List”, Map 容器用法為“Map<KT, VT>”,T、KT 和 VT 可以為基本數(shù)據(jù)類型、自定義類型、Sequenceable 類型、接口類型、數(shù)組類型或容器類型。 在 C++ 代碼中,List 容器類型生成為 std::list,Map 容器類型生成為 std::map。在 Java 代碼中,List 容器類型生成為 ArrayList,Map 容器類型生成為 HashMap。 HarmonyOS IDL 容器數(shù)據(jù)類型與 Java 數(shù)據(jù)類型、C/C++ 數(shù)據(jù)類型的對(duì)應(yīng)關(guān)系如下表所示:
HarmonyOS IDL數(shù)據(jù)類型Java數(shù)據(jù)類型C++數(shù)據(jù)類型C數(shù)據(jù)類型 List List std::list或std::vector T*,int size Map<KT, VT> HashMap std::map或std::unordered map 不支持
② 接口定義
一個(gè) HarmonyOS IDL 文件只能定義一個(gè)接口類,接口定義的形式使用 BNF 范式描述,示例如下:
< * interface_attributes_declaration
* > ? interface
< * interface_name_with_namespace
* > { < * method_declaration
* > ? }
<interface_attributes_declaration>是接口屬性聲明,當(dāng)前支持 oneway、callback、full、lite 屬性: oneway:表示該接口中的方法都是單向方法,即調(diào)用方法后不用等待該方法執(zhí)行即可返回,該屬性為可選項(xiàng),如果無聲明,則默認(rèn)為同步調(diào)用方法; callback:表示該接口為callback接口,即從下層實(shí)現(xiàn)中回調(diào)到上層服務(wù)中; full、lite:表示該接口在重量級(jí)部署或輕量級(jí)部署中使用。如果無聲明,則默認(rèn)為重量級(jí)、輕量級(jí)部署中都會(huì)使用。 <interface_name_with_namespace>是接口名聲明,接口名需包含完整的命名空間,且必須包含方法聲明,不允許出現(xiàn)空接口。接口示例:
[ oneway
] interface ohos
. app
. IAbilityManager
{ ……
} ;
<method_declaration>是方法聲明,形式為:
< * method_attributes_declaration
* > ? < * result type
* > < * method declarator
* >
<method_attributes_declaration>是方法屬性聲明,當(dāng)前支持 oneway 、full、lite 屬性: oneway:表示該方法是單向方法,即調(diào)用方法后不用等待該方法執(zhí)行即可返回; full、lite:表示該方法在重量級(jí)部署或輕量級(jí)部署中使用。如果無聲明,則默認(rèn)為重量級(jí)、輕量級(jí)部署中都會(huì)使用。 <result type>是返回類型,<method declarator>是方法名和各個(gè)參數(shù)聲明,參數(shù)聲明的形式為:
\
[ < * formal_parameter_attribute
* > \
] < * type
* > < * identifier
* >
<formal_parameter_attribute>的值為“in”、“out”或“inout”,分別表示該參數(shù)是輸入?yún)?shù)、輸出參數(shù)或輸入輸出參數(shù)。oneway 方法不允許有輸出參數(shù)(包含輸入輸出參數(shù))和返回值。方法示例:
void SetBundles ( [ in ] Map
< String
, BundleInfo
> bundleInfos
, [ in , out
] int number
) ; [ oneway
] void Dump ( [ in ] ParcelableFileDescriptor fd
, [ in ] long flags
) ;
在 HarmonyOS IDL 接口描述文件中可以使用的關(guān)鍵字如下表所示:
HarmonyOS IDL keywords詳細(xì)說明 void、boolean、byte、short、int、long、float、double 基本數(shù)據(jù)類型,void類型、bool型、整型、浮點(diǎn)型等 unsigned 在定義無符號(hào)類型時(shí)使用,如unsigned char、unsigned short、unsigned int、unsigned long String 定義字符串時(shí)使用 struct 結(jié)構(gòu)體類型 union 聯(lián)合體類型 enum 枚舉類型 T[] T可以是基本數(shù)據(jù)類型、自定義類型、Sequenceable數(shù)據(jù)類型、接口類型和數(shù)組類型 List<> 容器類型List,T為基本數(shù)據(jù)類型、自定義類型、Sequenceable類型、接口類型、數(shù)組類型或容器類型 Map<> 容器類型Map<KT, VT>: KT和VT為基本數(shù)據(jù)類型、自定義類型、Sequenceable類型、接口類型、數(shù)組類型或容器類型 Sequenceable 該類型并不在idl文件中定義,而是定義在C++文件或Java文件中 extends 接口繼承 interface 接口類型 [oneway] 表示單向接口或方法 oneway方法不允許有輸出參數(shù)(包含輸入輸出參數(shù))和返回值 [in] 輸入?yún)?shù) [out] 輸出參數(shù) [inout] 輸入輸出參數(shù) [callback] 標(biāo)識(shí)一個(gè)接口為回調(diào)接口定義(明示工具如何生成service、client代碼) [full] 僅在HarmonyOS重量級(jí)部署中使用的參數(shù)、接口、方法 [lite] 僅在HarmonyOS輕量級(jí)部署中使用的參數(shù)、接口、方法
③ 注釋說明
HarmonyOS IDL 文件中的注釋采用與 C/C++ 相同的方式,可以采用//或者/* … */的方式進(jìn)行注釋說明。
三、開發(fā)步驟
① 創(chuàng)建 .idl 文件
可以使用 Java 或 C++ 編程語言構(gòu)建 .idl 文件。.idl 示例如下:
sequenceable com
. exampleinterface com
. example
. IRemoteAbility
{ int plus ( [ in ] int num1
, [ in ] int num2
) ; }
HarmonyOS SDK 工具支持生成 Java、C++ 語言的接口類、樁類和代理類。以 Java 語言開發(fā)為例,開發(fā)者只需將 .idl 文件保存至 DevEco Studio 項(xiàng)目的 src/ 目錄內(nèi),工具則會(huì)在構(gòu)建應(yīng)用時(shí),在項(xiàng)目的 generated/ 目錄中生成 IRemoteObject 接口文件、Stub 文件、Proxy 文件。生成的接口類文件名稱和.idl文件名稱保持一致,區(qū)別在于其使用 .java 擴(kuò)展名。例如,IRemoteAbility.idl 生成的文件名是 IRemoteAbility.java、RemoteAbilityProxy.java、RemoteAbilityStub.java。
② 服務(wù)端公開接口
HarmonyOS IDL 工具生成的 Stub 類是接口類的抽象實(shí)現(xiàn),并且會(huì)聲明 .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 ; ………
}
要實(shí)現(xiàn) .idl 生成的接口,請(qǐng)擴(kuò)展生成的 RemoteObject 接口,并實(shí)現(xiàn)繼承自 .idl 文件的方法。MyRemote 定義了服務(wù)的遠(yuǎn)程過程調(diào)用接口,示例代碼如下:
class MyRemote extends RemoteAbilityStub
{ public
MyRemote ( String descriptor
) { super ( descriptor
) ; } @ Overridepublic
int plus ( int num1
, int num2
) throws RemoteException
{ return num1
+ num2
; } }
在服務(wù)實(shí)現(xiàn)接口后,需要向客戶端公開該接口,以便客戶端進(jìn)程綁定。如果開發(fā)者的服務(wù)要公開該接口,請(qǐng)擴(kuò)展 Ability 并實(shí)現(xiàn) onConnect() 從而返回 IRemoteObject,以便客戶端能與服務(wù)進(jìn)程交互。服務(wù)端向客戶端公開 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 ( ) ; } }
③ 客戶端調(diào)用 IPC 方法
客戶端調(diào)用 connectAbility() 以連接服務(wù)時(shí),客戶端的 onAbilityConnectDone() 回調(diào)會(huì)接收服務(wù)的 onConnect() 方法返回的 IRemoteObject 實(shí)例。由于客戶端和服務(wù)在不同應(yīng)用內(nèi),所以客戶端應(yīng)用的 src/ 目錄內(nèi)必須包含 .idl 文件(SDK 工具會(huì)自動(dòng)生成 Proxy 代理類)的副本。當(dāng)客戶端 onAbilityConnectDone() 回調(diào)中收到 IRemoteObject,調(diào)用 RemoteAbilityStub.asInterface(IRemoteObject),以將返回的參數(shù)轉(zhuǎn)換為 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
; } } ;
如果要斷開連接,請(qǐng)調(diào)用 Ability.disconnectAbility()。
④ IPC 傳遞 Sequenceable 對(duì)象
開發(fā)者可以通過 IPC 接口,將某個(gè)類從一個(gè)進(jìn)程發(fā)送至另一個(gè)進(jìn)程。但是,必須確保 IPC 通道的另一端可使用該類的代碼,并且該類必須支持 Sequenceable 接口。HarmonyOS 需要通過該 Sequenceable 接口將對(duì)象反序列化成各進(jìn)程能識(shí)別的對(duì)象。 如需創(chuàng)建支持 Sequenceable 接口的類,開發(fā)者必須執(zhí)行以下操作: 自定義對(duì)象實(shí)現(xiàn) Sequenceable 接口; 實(shí)現(xiàn) marshalling 方法,它會(huì)獲取對(duì)象的當(dāng)前狀態(tài)并將其序列化后寫入 Parcel; 實(shí)現(xiàn) unmarshalling 方法,它會(huì)從 Parcel 中反序列化出對(duì)象; ElementName.java 類實(shí)現(xiàn) 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 語法規(guī)則
HarmonyOS IDL語法規(guī)則(BNF范式描述)HarmonyOS IDL語法規(guī)則(文字描述)示例 ### Programs一個(gè)HarmonyOS IDL接口定義文件由三部分組成:包名、類型聲明、接口定義。 IDL接口文件擴(kuò)展名為*.idl。 一個(gè)文件只能定義一個(gè)接口,多個(gè)接口需要定義在多個(gè)不同的文件中。 文件名必須和接口名一致。 自定義類型單獨(dú)成一個(gè)文件,該文件名可以按需定義。- <*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*>+ };接口定義,包括:接口屬性、接口名稱、接口繼承聲明、接口函數(shù)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*> ;接口函數(shù)聲明,包括:函數(shù)屬性、返回值、函數(shù)名稱、參數(shù)列表- <*method_attributes_declaration*> ::= \ [ <*method_attributes*> \ ]函數(shù)屬性- <*method_attributes*> ::= <*method_attribute*> | <*method_attributes*> , <*method_attribute*>-- <*method_attribute*> ::= <*java_like_method_attribute*> | <*c_like_method_attribute*>-- <*java_like_method_attribute*> ::= oneway類Java語言的函數(shù)屬性支持:oneway- <*c_like_method_attribute*> ::= <*java_like_method_attribute*> | full | lite類C語言的函數(shù)屬性,支持:oneway、full、lite- <*result_type*> ::= <*type*> | void返回值:可以沒有返回值(void),返回一個(gè)指定類型的值- <*method_declarator*> ::= <*method_name*> ( <*formal_parameters*>? )接口函數(shù):可以沒有參數(shù)- <*formal_parameters*> ::= <*formal_parameter*> | <*formal_parameters*> , <*formal_parameter*>-- <*formal_parameter*> ::= \ [ <*formal_parameter_attribute*> \ ] <*type*> <*formal_parameter_name*>每個(gè)函數(shù)參數(shù)由三個(gè)部分組成:參數(shù)屬性、參數(shù)類型、參數(shù)名稱- <*formal_parameter_attribute*> ::= in | out | inout<參數(shù)屬性,支持: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語言的數(shù)據(jù)類型,包括:基本類型、String類型、sequenceable類型、接口類型、List容器類型、Map容器類型、數(shù)組類型- <*c_like_type*> ::= <*primitive_type*> | <*string_type*> | <*interface_type*> | <*list_type*> | <*array_type*> | <*custom_type*>類C語言的數(shù)據(jù)類型,包括:基本類型、String類型、接口類型、List容器類型、數(shù)組類型、自定義類型- <*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語言的基本類型外,還可以使用無符號(hào)類型- <*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*> \ [ \ ] 數(shù)組類型- <*custom_type*> ::= <*enum_type*> | <*struct_type*> | <*union_type*>自定義類型,包括:枚舉類型、結(jié)構(gòu)體類型、聯(lián)合體類型、三種類型的嵌套使用- <*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*>+ };結(jié)構(gòu)體類型:自定義類型屬性、類型名、結(jié)構(gòu)體成員- <*union_type*> ::= <*custom_type_attributes_declaration*>? union <*custom_type_name*> { <*custom_type_members_declaration*>+ };聯(lián)合體類型:自定義類型屬性、類型名、聯(lián)合體成員- <*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容器類型、數(shù)組類型、自定義類型- <*primitive_type_member*> ::= <*custom_type_attributes_declaration*>? <*c_like_primitive_type*> <*custom_type_member_name*> ;基本類型成員:自定義類型屬性、數(shù)據(jù)類型、自定義成員變量名- <*list_type_member*> ::= <*custom_type_attributes_declaration*>? <*custom_list_type*> <*custom_type_member_name*> ;List容器類型成員:自定義類型屬性、數(shù)據(jù)類型、自定義成員變量名- <*array_type_member*> ::= <*custom_type_attributes_declaration*>? <*custom_array_type*> <*custom_type_member_name*> ;數(shù)組類型成員:自定義類型屬性、數(shù)據(jù)類型、自定義成員變量名- <*custom_type_member*> ::= <*custom_type_attributes_declaration*>? <*custom_type*> <*custom_type_member_name*> ;自定義類型成員:自定義類型屬性、數(shù)據(jù)類型、自定義成員變量名- <*custom_list_type*> ::= List < <*custom_member_type*> >自定義類型中使用的List容器類型- <*custom_array_type*> ::= <*custom_member_type*> \ [ \ ]自定義類型中使用的數(shù)組類型- <*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*>接函數(shù)口名- <*formal_parameter_name*> ::= <*identifier*>參數(shù)名- <*custom_type_name*> ::= <*identifier*>自定義類型名- <*enum_member_name*> ::= <*enum_letter*>+枚舉成員名- <*custom_type_member_name*> ::= <*identifier*>自定義類型成員名- ### identifier-- <*identifier*> ::= <*letter*>+ | <*letter_number*>標(biāo)識(shí)符由字母和數(shù)字組成,第一個(gè)字符不能使用數(shù)字- <*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十進(jìn)制數(shù)字- <*oct_number*> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7八進(jìn)制數(shù)字- <*number_value*> ::= <*hex_prefix*> <*hex_value*> | <*dec_value*> | 0 <*oct_value*>數(shù)值:枚舉類型成員的值- <*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十六進(jìn)制數(shù)字- <*enum_letter*> ::= _ | <*upper_case*>枚舉類型成名的命名,可以使用:下滑線、大寫字母-
語法文檔使用的規(guī)則如下: 尖括號(hào) < > 內(nèi)包含的為必選項(xiàng); 豎線 | 表示在其左右兩邊任選一項(xiàng),相當(dāng)于"OR"的含義; ? 操作符左邊的符號(hào)是可選項(xiàng)(可以出現(xiàn) 0 到多次);
總結(jié)
以上是生活随笔 為你收集整理的HarmonyOS之IDL接口使用规范 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。