如何调用Oracle PL/SQL对象的超级方法

前端之家收集整理的这篇文章主要介绍了如何调用Oracle PL/SQL对象的超级方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想调用一个重写的PL / sql方法.这是一个例子:
  1. -- super class
  2. create or replace type test as object
  3. (
  4. n number,member procedure proc(SELF in out nocopy test,s varchar2)
  5. )
  6. alter type test not final
  7. /
  8.  
  9. create or replace type body test is
  10. member procedure proc(SELF in out nocopy test,s varchar2) is
  11. begin
  12. dbms_output.put_line('test1: n='||nvl(self.n,'null')||' s='||s);
  13. self.n := to_number(s);
  14. end;
  15. end;
  16. /
  17.  
  18. -- derived class
  19. create or replace type test2 under test
  20. (
  21. overriding member procedure proc(SELF in out nocopy test2,s varchar2)
  22. )
  23. /

现在我想调用proc方法的继承版本.当我尝试做一个像treat一样的显式转换(self as test).proc(s);由于PLS-00363,它不会编译:表达式’SYS_TREAT’不能用作赋值目标

当我使用局部变量时,类型体编译:

  1. create or replace type body test2 is
  2. overriding member procedure proc(SELF in out nocopy test2,s varchar2) is
  3. O test;
  4. begin
  5. O := treat(self as test);
  6. O.proc(s);
  7. end;
  8. end;
  9. /

但是,当我像这样运行我的例子

  1. declare
  2. obj test2;
  3. begin
  4. obj := test2(0);
  5. obj.proc('1');
  6. end;

…它抛出ORA-21780:超过了最大对象持续时间.

有没有办法调用test :: proc(没有序列化/反序列化)?

并且…在调用proc之后,如何将任何已更改的属性(即n)反映在obj中?

更新(谢谢,tbone):

我使用模板方法(‘之前’和’之后’)改变了方法的组织.每当我需要扩展方法时,我都会添加它们.

  1. create or replace type test as object
  2. (
  3. n number,member procedure proc (SELF in out nocopy test,s varchar2),member procedure afterProc (SELF in out nocopy test,s varchar2)
  4. member procedure beforeProc(SELF in out nocopy test,)
  5. not final
  6. /
  7.  
  8. create or replace type body test is
  9. member procedure proc(SELF in out nocopy test,s varchar2) is
  10. begin
  11. beforeProc(s);
  12. dbms_output.put_line('test1: n='||nvl(n,'null')||' s='||s);
  13. n := to_number(s);
  14. afterProc(s);
  15. end;
  16. member procedure afterProc (SELF in out nocopy test,s varchar2) is begin null; end;
  17. member procedure beforeProc(SELF in out nocopy test,s varchar2) is begin null; end;
  18. end;
  19. /
要访问超级方法,请尝试常规调用或通用表达式.例如,使用人超类型和学生子类型:
  1. CREATE OR REPLACE TYPE person_typ AS OBJECT (
  2. idno number,name varchar2(30),phone varchar2(20),MAP MEMBER FUNCTION get_idno RETURN NUMBER,MEMBER FUNCTION show RETURN VARCHAR2)
  3. NOT FINAL;
  4.  
  5. CREATE OR REPLACE TYPE BODY person_typ AS
  6. MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
  7. BEGIN
  8. RETURN idno;
  9. END;
  10. MEMBER FUNCTION show RETURN VARCHAR2 IS
  11. BEGIN
  12. -- function that can be overriden by subtypes MEMBER FUNCTION show RETURN VARCHAR2 IS BEGIN
  13. RETURN 'Id: ' || TO_CHAR(idno) || ',Name: ' || name;
  14. END;
  15. END;
  16.  
  17. CREATE TYPE student_typ UNDER person_typ (
  18. dept_id NUMBER,major VARCHAR2(30),OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2)
  19. NOT FINAL;
  20.  
  21. CREATE TYPE BODY student_typ AS
  22. OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
  23. BEGIN
  24. RETURN (self AS person_typ).show || ' -- Major: ' || major ;
  25. END;
  26. END;
  27.  
  28. -- Using Generalized Invocation
  29. DECLARE
  30. myvar student_typ := student_typ(100,'Sam','6505556666',100,'Math');
  31. name VARCHAR2(100);
  32. BEGIN
  33. name := (myvar AS person_typ).show; --Generalized invocation
  34. END;
  35.  
  36. -- Using Generalized Expression
  37. DECLARE
  38. myvar2 student_typ := student_typ(101,'Math');
  39. name2 VARCHAR2(100);
  40. BEGIN
  41. name2 := person_typ.show((myvar2 AS person_typ)); -- Generalized expression
  42. END;

编辑:

如果你是10g,你需要组织一些不同的功能,但是从孩子那里调用super方法功能相同:

  1. CREATE TYPE BODY person_typ AS
  2. MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
  3. BEGIN
  4. RETURN idno;
  5. END;
  6. -- static function that can be called by subtypes
  7. STATIC FUNCTION show_super (person_obj in person_typ) RETURN VARCHAR2 IS
  8. BEGIN
  9. RETURN 'Id: ' || TO_CHAR(person_obj.idno) || ',Name: ' || person_obj.name;
  10. END;
  11. -- function that can be overriden by subtypes
  12. MEMBER FUNCTION show RETURN VARCHAR2 IS
  13. BEGIN
  14. RETURN person_typ.show_super ( SELF );
  15. END;
  16. END;
  17.  
  18. CREATE TYPE student_typ UNDER person_typ (
  19. dept_id NUMBER,OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2)
  20. NOT FINAL;
  21.  
  22. CREATE TYPE BODY student_typ AS
  23. OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
  24. BEGIN
  25. RETURN person_typ.show_super ( SELF ) || ' -- Major: ' || major ;
  26. END;
  27. END;

现在,您可以通过student方法调用show_super(),或者只调用student方法的show().

从文档中,希望有所帮助.

猜你在找的Oracle相关文章