最近在上網看些實例的時候,發現我們自已自定義的全局函數一個好玩的重載現象.
一般我們如果要對函數進行重載,則會定義多個相同名稱的函數,只是參數定義會不一樣,如果是全局函數,因為名稱都相同,而在一個PBL中不允許有同名的
對象存在,所以不能定義.而實際上PB提供了另一個未公開的方法可以實現.
如先定義一個全局函數,名稱為of_tt,無返回值,腳本為:messagebox('a','ok').
存檔后,使用Edit Source功能查看其原碼:
global type of_tt from function_object
end type
forward prototypes
global subroutine of_tt ()
end prototypes
global subroutine of_tt ();
messagebox('a', 'ok')
end subroutine
如果需要對此函數進行重載,則可以自己在此Source View畫面中直接添加腳本即可.
如我將其修改為:
global type of_tt from function_object
end type
forward prototypes
global subroutine of_tt ()
global subroutine of_tt (readonly string as_tip)
end prototypes
global subroutine of_tt ();
messagebox('a', 'ok')
end subroutine
global subroutine of_tt (readonly string as_tip);
messagebox('a', as_tip)
end subroutine
這樣同一個函數就有兩種不同的使用方式.
那我們在其它地方可以依如下方法調用:
of_tt()
of_tt('This is a Test.')
本文介紹了PowerBuilder中一個未公開的全局函數重載方法,通過編輯源代碼實現不同參數配置的同一函數,提供多種使用方式。
4103

被折叠的 条评论
为什么被折叠?



