Overloading,overriding,and extending functions and events 重载,重写和扩展函数和事件

来源:互联网 发布:旅游网络投诉举报平台 编辑:程序博客网 时间:2024/05/21 03:18

Overloading,overriding,and extending functions and events

重载,重写和扩展函数和事件

In PowerBuilder,when functions are inherited,you can choose overload or override the function definition,

described in "Overloading and overriding functions" next.

PowerBuilder中,当函数是被继承的,你可以选择重载或者重写这个函数定义,在接下来的“重载和重写函数”中描述

 

When events are inherited, the scripts for those events are extended by default.

you can choose to extend or override the script,described in"Extending and overriding events" on page 106.

当事件被继承,对于那些事件的脚本默认是扩展的。您可以选择扩展或重写脚本,在第106页上的“扩展和重写事件”中描述

 

Overloading and overriding functions

重载和重写函数

 

To create an overloaded function, you declare the function as you would any function using Insert>Function.

要创建一个重载函数,你可以像其他函数一样使用 “插入”>> “函数”去声明这个函数

 

Overriding means defining a function in a descendent object that has the same name and argument list as a function in the ancestor object.

In the descendent object, the function in the descendant is always called instead of the one in the ancestor--unless you use the scope resolution operator(::).

在一个派生的对象中以重写的方式定义一个函数,它跟祖先对象的一个函数具有相同的名称和参数列表。

在派生的对象,在后代的函数总是被调用,替代在祖先的函数除非你使用范围解析操作符(::)。

 

To override a function, open the descendent object in the painter, select the function in the Script view, and code the new script. 

The icon that indicates that there is a script for a function is half shaded when the function is inherited from an ancestor.

要重写一个函数,在画板中打开派生对象,在脚本视图选择函数,并且编写新的脚本。

该图标表示他是一个函数的脚本,当这个函数是从祖先继承的,它是一半阴影

 

You can overload or override object functions only -- you can not overload global functions.

您只能重载或重写对象的函数-- 你不能重载全局函数。

 

Type promotion when matching arguments for overloaded functions

当为重载函数匹配参数的类型提升

 

When you have overloaded a function so that one version handles numeric values and another version handles strings, 

it is clear to the programmer what arguments to provide to call each version of the function.

Overloading with unrelated datatypes is a good idea and can provide needed functionality for your application.

当你有一个重载的函数,一个版本处理数值并且另一个版本的处理字符串,它是明显的告诉程序员什么样的参数提供给调用每个版本的函数

使用不同的数据类型重载是一个好主意,可以为你的应用提供所需的功能。

 

Problematic overloading

载问题

 

If different versions of a funtion have arguments of related datatypes (different numeric types or string and chars), 

you must consider how PowerBuilder promotes dataypes in determining which function is called.

This kind of overloading is undesirable because of potential confusion in determining which function is called.

如果一个函数不同版本有相关的数据类型(不同的数字类型或字符串和字符)的参数,你必须考虑的PowerBuilder如何提升数据类型确定哪个函数被调用。

这种载是不可取的因为在确定哪个函数被调用方面有潜在的混乱。

 

Because of the intricacies of type promotion for numeric datatypes, 

you might decide that you should not define overloaded functions with different numeric datatypes.

Changes someone makes later can affect the application more drastically than expected if the change causes a different function to be called.

因为对数值数据类型的类型提升的复杂性,您可能认为你不应该用不同的数值数据类型定义重载函数。

改变别人,使以后可能会比预期的更大幅度影响应用程序如果更改会导致不同的函数被调用。

 

How type promotion work

类型提升如何工作

 

When PowerBuilder evaluates an expression, it converts the datatypes of constants and varibles so that is can process or combine them correctly.

PowerBuilder计算表达式,将转换常量和变量的数据类型,这样可以处理或正确地将它们合并。

 

Numbers  When PowerBuilder evaluates numeric expressions, it promotes the datatypes of values according to the operators and the datatypes of the other operands.

For example, the datatype of the expression n/2 is double because it involves division -- the datatype of n does not matter.

 

数值  PowerBuilder计算数值表达式,它根据运算符和其他操作数的数据类型提升值的数据类型。例如,表达式n/2的数据类型是双精度,因为它涉及分割-- n的数据类型并不重要。

 

Strings when evaluating an expression that involves chars and strings, PowerBuilder promotes chars to strings.

字符串  计算涉及字符和字符串表达式,PowerBuilder提升字符为字符串。

 

For more information on type promotion, see "Datatype of PowerBuilder exprssions" on page 75.

有关类型提升的更多信息,请参阅第75页“PowerBuilder表达式的数据类型”。

 

Using conversion functions

使用转换函数

 

You can take control over the datatypes of expressions by calling a conversion function.The conversion function ensures the datatype of the expression matches the function prototype you want to call.

你可以通过调用转换函数接管表达式的数据类型控制。转换功能确保表达式的数据类型,你要调用的函数原型相匹配。

 

For example, because the expression n/2 involves division, the datatype is double .However, if the function you want to call expects a long, you can use the Long function to ensure that the function call matches the prototype:

CalculateHalf(Long(n/2))

例如,因为表达式n/2涉及分割,数据类型为双精度。不过,如果你要调用的函数需要一个长整形,您可以使用Long函数来确保函数调用的原型相匹配:

CalculateHalf(Long(n/2))

 

Extending and overriding events

扩展和重写事件

In PowerBuilder, when you write event scripts in a descendent object, you can extend or override scripts that have been written in the ancestor.Extending(the default) means executing the ancestor's script first, then executing code in the descendant's event script.

PowerBuilder中,当你写一个派生对象的事件脚本,你可以扩展或重写已写入的祖先脚本。扩展(默认值)是指先执行祖先的脚本,然后执行在后代的事件脚本代码。

 

Overriding means ignoring the ancestor's script and only executing the script in the descendant.

重写方法忽略了祖先的脚本,只执行的后裔脚本。

 

No overloaded events

没有载事件

 

you can not overload an event by defining an event with the same name but different arguments.event names must be unique.

你不能用相同的名字,但不同的参数定义事件来重载事件。事件的名称必须是唯一的。

 

To select extending or overriding, open the script in the Script view and check or clear the Extend Ancestor Script item in the Edit or pop-up menu.

要选择扩展重写,在脚本视图中打开该脚本并在编辑或弹出式菜单中选中或清除扩展祖先脚本项。

 

 

0 0
原创粉丝点击