Difference between revisions of "Zh/Documentation/DevGuide/ProUNO/Basic/Handling UNO Objects"
m |
m |
||
Line 7: | Line 7: | ||
|NextPage=Zh/Documentation/DevGuide/ProUNO/Basic/Instantiating UNO Services | |NextPage=Zh/Documentation/DevGuide/ProUNO/Basic/Instantiating UNO Services | ||
}} | }} | ||
− | + | {{Documentation/DevGuideLanguages|Documentation/DevGuide/ProUNO/Basic/{{SUBPAGENAME}}}} | |
{{DISPLAYTITLE:处理 UNO 对象}} | {{DISPLAYTITLE:处理 UNO 对象}} | ||
Latest revision as of 02:52, 14 May 2009
访问 UNO 服务
UNO 对象的使用是通过其接口方法和属性实现的。Basic 通过将 UNO 接口和属性映射成 Basic 对象方法和属性简化了这一过程。
首先,在 Basic 中,调用方法时没有必要区分对象支持的不同接口。以下插图示意了一个 UNO 服务示例,该服务支持三个接口:
在 Java 和 C++ 中,调用接口中的一个方法之前,需要获取对每个接口的引用。在 Basic 中,可以直接调用该对象支持的任何接口的任何方法,而无需提前查询相应的接口。使用的是 '.' 运算符:
' Basic oExample = getExampleObjectFromSomewhere() oExample.doNothing()' Calls method doNothing of XFoo1 oExample.doSomething()' Calls method doSomething of XFoo2 oExample.doSomethingElse(42)' Calls method doSomethingElse of XFoo2
此外,如果 UNO 对象的 get 和 set 方法对符合以下模式,则 OpenOffice.org Basic 将它们解释为 Basic 对象属性:
SomeType getSomeProperty() void setSomeProperty(SomeType aValue)
在此模式中,OpenOffice.org Basic 提供了一个类型为 SomeType
、名称为 SomeProperty
的属性。此功能基于 高级 UNO - 语言绑定 - UNO 反射 API。
通常可以直接使用 get 和 set 方法。在上面的示例服务中,使用了方法 getIt()
和 setIt()
,或者读取和写入一个 Basic 属性 It
:
Dim x as Integer x = oExample/getIt() ' Calls getIt method of XFoo3 ' is the same as x = oExample/It ' Read property It represented by XFoo3 oExample/setIt( x ) ' Calls setIt method of XFoo3 ' is the same as oExample/It = x ' Modify property It represented by XFoo3
如果只有 get 方法,而没有关联的 set 方法,则属性被视为只读。
Dim x as Integer, y as Integer x = oExample/getMore() ' Calls getMore method of XFoo1 y = oExample/getLess() ' Calls getLess method of XFoo1 ' is the same as x = oExample/More ' Read property More represented by XFoo1 y = oExample/Less ' Read property Less represented by XFoo1 ' but oExample/More = x ' Runtime error "Property is read only" oExample/Less = y ' Runtime error "Property is read only"
使用 /
运算符可以获得对象通过 com.sun.star.beans.XPropertySet.html" class="external text">com.sun.star.beans.Introspection</code> 服务。如果需要更多信息,请参阅 高级 UNO - 语言绑定 - UNO 反射 API。
通常可以直接使用 get 和 set 方法。在上面的示例服务中,使用了方法 getIt()
和 setIt()
,或者读取和写入一个 Basic 属性 It
:
Dim x as Integer x = oExample.getIt() ' Calls getIt method of XFoo3 ' is the same as x = oExample.It ' Read property It represented by XFoo3 oExample.setIt( x ) ' Calls setIt method of XFoo3 ' is the same as oExample.It = x ' Modify property It represented by XFoo3
如果只有 get 方法,而没有关联的 set 方法,则属性被视为只读。
Dim x as Integer, y as Integer x = oExample.getMore() ' Calls getMore method of XFoo1 y = oExample.getLess() ' Calls getLess method of XFoo1 ' is the same as x = oExample.More ' Read property More represented by XFoo1 y = oExample.Less ' Read property Less represented by XFoo1 ' but oExample.More = x ' Runtime error "Property is read only" oExample.Less = y ' Runtime error "Property is read only"
使用 .
运算符可以获得对象通过 com.sun.star.beans.XPropertySet 提供的属性。也可以使用 com.sun.star.beans.XPropertySet 中的方法。以下示例中的对象 oExample2
具有三个 integer 属性:Value1
、Value2
和 Value3
:
Dim x as Integer, y as Integer, z as Integer x = oExample2.Value1 y = oExample2.Value2 z = oExample2.Value3 ' is the same as x = oExample2.getPropertyValue( "Value1" ) y = oExample2.getPropertyValue( "Value2" ) z = oExample2.getPropertyValue( "Value3" ) ' and oExample2.Value1 = x oExample2.Value2 = y oExample2.Value3 = z ' is the same as oExample2.setPropertyValue( "Value1", x ) oExample2.setPropertyValue( "Value2", y ) oExample2.setPropertyValue( "Value3", z )
使用 com.sun.star.container.XNameAccess,Basic 可通过 . 运算符提供命名元素集合。但是,XNameAccess 仅提供读取权限。如果集合可通过 com.sun.star.container.XNameReplace 或 com.sun.star.container.XNameContainer 进行写入,则显式使用相应的方法:
' oNameAccessible is an object that supports XNameAccess ' including the names "Value1", "Value2" x = oNameAccessible.Value1 y = oNameAccessible.Value2 ' is the same as x = oNameAccessible.getByName( "Value1" ) y = oNameAccessible.getByName( "Value2" ) ' but oNameAccessible.Value1 = x' Runtime Error, Value1 cannot be changed oNameAccessible.Value2 = y' Runtime Error, Value2 cannot be changed ' oNameReplace is an object that supports XNameReplace ' replaceByName() sets the element Value1 to 42 oNameReplace.replaceByName( "Value1", 42 )
Content on this page is licensed under the Public Documentation License (PDL). |