我想调用重写的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