使用参数创建实例

From Apache OpenOffice Wiki
< Zh‎ | Documentation
Revision as of 08:37, 28 July 2008 by Jirong (talk | contribs) (New page: {{Documentation/DevGuide/WritingUNOTOC/Zh |WritingUNO2c=block |ShowPrevNext=block |PrevPage=Zh/Documentation/DevGuide/WritingUNO/Storing the Service Manager for Further Use |NextPage=Zh/Do...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


工厂可以创建组件的实例并传送附加的参数。要完成此操作,客户端可以调用 com.sun.star.lang.XSingleServiceFactory 接口的 createInstanceWithArguments() 函数或 com.sun.star.lang.XSingleComponentFactory 接口的 createInstanceWithArgumentsAndContext()

  //javamaker generated interface
  //XSingleServiceFactory interface 
  public java.lang.Object createInstanceWithArguments(java.lang.Object[] aArguments) 
                              throws com.sun.star.uno.Exception;
  
  //XSingleComponentFactory
  public java.lang.Object createInstanceWithArgumentsAndContext(java.lang.Object[] Arguments,
                              com.sun.star.uno.XComponentContext Context)
                          throws com.sun.star.uno.Exception;


这两个函数都接受值的数组作为参数。组件将实现 com.sun.star.lang.XInitialization 接口以接收这些值。工厂会将数组传送到 XInitialization 支持的单个方法 initialize()

  public void initialize(java.lang.Object[] aArguments) throws com.sun.star.uno.Exception;


或者,组件还可以在其构造函数中接收这些参数。如果编写一个工厂,请明确在该工厂实例化组件时它会提供哪些参数。使用 FactoryHelper 时,请使用以下参数实现构造函数:

第一个参数 第二个参数 第三个参数
com.sun.star.uno.XComponentContext com.sun.star.registry.XRegistryKey java.lang.Object[]
com.sun.star.uno.XComponentContext com.sun.star.registry.XRegistryKey
com.sun.star.uno.XComponentContext java.lang.Object[]
com.sun.star.uno.XComponentContext
java.lang.Object[]


FactoryHelper 会自动将它从 createInstanceWithArguments[AndContext]() 调用接收到的参数数组传送到适当的构造函数。因此,不必总是实现 XInitialization 以使用这些参数。


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages