我想調用重寫的PL/SQL方法。這裏有一個例子:如何調用Oracle PL/SQL對象超級方法
-- super class
create or replace type test as object
(
n number,
member procedure proc(SELF in out nocopy test, s varchar2)
)
alter type test not final
/
create or replace type body test is
member procedure proc(SELF in out nocopy test, s varchar2) is
begin
dbms_output.put_line('test1: n='||nvl(self.n, 'null')||' s='||s);
self.n := to_number(s);
end;
end;
/
-- derived class
create or replace type test2 under test
(
overriding member procedure proc(SELF in out nocopy test2, s varchar2)
)
/
現在我想調用的方法proc
的繼承版本。當我嘗試做這樣treat(self as test).proc(s);
有明確的轉換,將無法編譯,因爲PLS-00363:表達式「SYS_TREAT」不能作爲分配對象
當我使用一個局部變量的類型身體編譯:
create or replace type body test2 is
overriding member procedure proc(SELF in out nocopy test2, s varchar2) is
O test;
begin
O := treat(self as test);
O.proc(s);
end;
end;
/
但是當我運行像這樣
declare
obj test2;
begin
obj := test2(0);
obj.proc('1');
end;
我的例子......它拋出ORA-21780:對象持續時間的數量上限。
有什麼辦法可以調用test :: proc(不需要序列化/反序列化)嗎?
...... proc被調用後,如何更改屬性(即n
)如何反映在obj
?
更新(謝謝,TBONE):
我改變了使用模板方法( '之前' 和 '之後'),我的方法組織。無論何時我需要擴展一個方法,我都會添加它們。
create or replace type test as object
(
n number,
member procedure proc (SELF in out nocopy test, s varchar2),
member procedure afterProc (SELF in out nocopy test, s varchar2)
member procedure beforeProc(SELF in out nocopy test, s varchar2),
)
not final
/
create or replace type body test is
member procedure proc(SELF in out nocopy test, s varchar2) is
begin
beforeProc(s);
dbms_output.put_line('test1: n='||nvl(n, 'null')||' s='||s);
n := to_number(s);
afterProc(s);
end;
member procedure afterProc (SELF in out nocopy test, s varchar2) is begin null; end;
member procedure beforeProc(SELF in out nocopy test, s varchar2) is begin null; end;
end;
/
docs看起來像TREAT用於訪問子類型方法/ attribs,而不是超級。我可能是錯的,但請參閱http://docs.oracle.com/cd/E11882_01/appdev.112/e11822/adobjbas.htm – tbone 2012-02-20 15:36:40
是的,你是對的。文檔說TREAT只能訪問子類型。但我應該如何使用超類? – 2012-02-20 15:59:35