Overloading,overriding,and extending functions and events 重載,重寫和擴展函數和事件

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.

要選擇擴展重寫,在腳本視圖中打開該腳本並在編輯或彈出式菜單中選中或清除擴展祖先腳本項。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章