定义结构

From Apache OpenOffice Wiki
Jump to: navigation, search


struct 是一种复合类型,它将任意 UNOIDL 类型放在一起,形成一种新的数据类型。其成员数据不进行封装,而是公开可用的。结构常用于轻松处理相关数据,以及向事件侦听器广播事件结构。


普通结构指令的开头是关键字 struct,然后是新结构类型的标识符,以及用花括号括起的结构主体,结尾是分号。结构主体包含结构成员声明的列表,这些声明由已知类型和结构成员的标识符定义。同样,成员的声明也必须以分号结尾。

  #ifndef __com_sun_star_reflection_ParamInfo_idl__ 
  #define __com_sun_star_reflection_ParamInfo_idl__ 
 
  #include <com/sun/star/reflection/ParamMode.idl> 
 
  module com { module sun { module star { module reflection {
 
  interface XIdlClass; // forward interface declaration
 
  struct ParamInfo 
  {
      string aName; 
      ParamMode aMode; 
      XIdlClass aType; 
  }; 
 
  }; }; }; }; 
 
  #endif


UNOIDL 支持 struct 类型的继承。 继承用冒号 : 表示,后面是父类型的全名。结构类型递归继承父 struct 的所有成员以及这些成员的父类型。例如,从 com.sun.star.lang.EventObject 中导出结构,将有关新事件的附加信息放入自定义的事件对象,以发送给事件侦听器。

  // com.sun.star.beans.PropertyChangeEvent inherits from com.sun.star.lang.EventObject
  // and adds property-related information to the event object
  struct PropertyChangeEvent : com::sun::star::lang::EventObject 
  {
      string PropertyName;
      boolean Further;
      long PropertyHandle;
      any OldValue;
      any NewValue;
  };


OpenOffice.org 2.0 的一项新功能是多态结构类型。多态结构类型模板与普通结构类型相似,但它有一个或多个类型参数(包含在尖括号 <> 内),并且其成员可以将这些参数用作类型:

  // A polymorphic struct type template with two type parameters:
  struct Poly<T,U> {
      T member1;
      T member2;
      U member3;
      long member4;
  };


多态结构类型模板本身不是 UNO 类型—它需要用真正的类型参数来实例化才可用作类型:

  // Using an instantiation of Poly as a type in UNOIDL:
  interface XIfc { Poly<boolean, any> fn(); };
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages