IDL 方法
所有这些标准的类型允许编程者定义对象的属性和方法。对象方法(函数)这样声明:
module FruitsBasket { interface Apple { // eat_me 方法的声明 // 有一个 boolean 变量作为参数 void eat_me (in boolean eat_yes_or_not ); // 返回 Apple 是否被吃了 boolean eaten (); // 返回 Apple 是否被吃了 // 和吃它的人的姓和名 boolean who_ate ( out string who_surname, out string who_name ); }; }; |
除了 in 修饰,这些声明非常象 C++ 代码: 参数由 in, out, inout 三种类型之一来修饰。它们的语义如下:in 参数是客户向对象发送的数据,out 参数是对象向客户发送的数据,inout 参数先从客户发送到对象,再被返回给客户。
所有这些方法声明都是同步操作,就是说,使用这些方法意味着你的程序将等到对象应答之后才继续执行。可以定义异步的方法,这样的话调用方法的程序可以继续执行而不是在对象返回应答之前一直阻塞,异步方法的声明用关键字 oneway(单向)。
module FruitsBasket { interface Apple { // eat_me 方法的声明 // 有一个 boolean 变量作为参数 void eat_me (in boolean eat_yes_or_not ); // 返回 Apple 是否被吃了 // 异步方法 oneway boolean eaten (); // 返回 Apple 是否被吃了 // 和吃它的人的姓和名 boolean who_ate ( out string who_surname, out string who_name ); }; }; |
也可以定义例外(exception)(类似于 C++ 的例外类)。
module FruitBasket { exception no_more_fruits { string reason; }; interface Apple { // 我们所希望的各种的方法 }; }; |
最终,可以象下面这样声明有能力引发(throwing)例外的方法。
module FruitsBasket { exception no_more_fruits { string reason; }; interface Apple { void eat_me (in boolean eat_yes_or_not ) raises ( no_more_fruits ); // 我们所希望的诸如此类的方法 }; }; |
象 C++ 的类(或 Java 的接口),接口可以从其他的接口继承,并且支持多继承(multiple inheritance)。语法类似于 C++ 的语法。一个重要的不同是 IDL 不支持重载或不同的方法可以有相同的名字和不同的参数说明(signature)。
module FruitsBasket { exception no_more_fruits { string reason; }; interface generic_fruit { void eat_me (in boolean eat_yes_or_not ) raises (no_more_fruits); oneway boolean eaten (); }; interface generic_fruit_one { boolean who_ate ( out string who_surname, out string who_name ); }; interface Apple : generic_fruit, generic_fruit_one { // 这里是特定于 Apple 的方法 }; }; |