Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Como chamar um super método de objeto Oracle PL/SQL


Para acessar os super métodos, tente a invocação geral ou a expressão generalizada. Por exemplo, usando um supertipo de pessoa e um subtipo de aluno:
CREATE OR REPLACE TYPE person_typ AS OBJECT (
    idno number,
    name varchar2(30),
    phone varchar2(20),
    MAP MEMBER FUNCTION get_idno RETURN NUMBER,
    MEMBER FUNCTION show RETURN VARCHAR2)
NOT FINAL;

CREATE OR REPLACE TYPE BODY person_typ AS
  MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
  BEGIN
    RETURN idno;
  END;
  MEMBER FUNCTION show RETURN VARCHAR2 IS
  BEGIN
    -- function that can be overriden by subtypes MEMBER FUNCTION show RETURN VARCHAR2 IS BEGIN
    RETURN 'Id: ' || TO_CHAR(idno) || ', Name: ' || name;
  END;
END;

CREATE TYPE student_typ UNDER person_typ (
    dept_id NUMBER,
    major VARCHAR2(30),
    OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2)
NOT FINAL;

CREATE TYPE BODY student_typ AS
  OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
  BEGIN
    RETURN (self AS person_typ).show || ' -- Major: ' || major ;
  END;
END;

-- Using Generalized Invocation
DECLARE
myvar student_typ := student_typ(100, 'Sam', '6505556666', 100, 'Math');
name VARCHAR2(100); 
BEGIN
name := (myvar AS person_typ).show; --Generalized invocation 
END;

-- Using Generalized Expression
DECLARE
myvar2 student_typ := student_typ(101, 'Sam', '6505556666', 100, 'Math');
name2 VARCHAR2(100); 
BEGIN
name2 := person_typ.show((myvar2 AS person_typ)); -- Generalized expression 
END;

EDITAR:

Se você estiver no 10g, precisará organizar as funções um pouco diferente, mas a mesma funcionalidade do filho para chamar o super método:
CREATE TYPE BODY person_typ AS 
  MAP MEMBER FUNCTION get_idno RETURN NUMBER IS 
  BEGIN
    RETURN idno; 
  END;
  -- static function that can be called by subtypes 
  STATIC FUNCTION show_super (person_obj in person_typ) RETURN VARCHAR2 IS
  BEGIN 
    RETURN 'Id: ' || TO_CHAR(person_obj.idno) || ', Name: ' || person_obj.name;
  END;
  -- function that can be overriden by subtypes 
  MEMBER FUNCTION show RETURN VARCHAR2 IS 
  BEGIN
    RETURN person_typ.show_super ( SELF ); 
  END;
END;

CREATE TYPE student_typ UNDER person_typ ( 
  dept_id NUMBER,
  major VARCHAR2(30), 
  OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2) 
  NOT FINAL;

CREATE TYPE BODY student_typ AS 
  OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS 
  BEGIN
    RETURN person_typ.show_super ( SELF ) || ' -- Major: ' || major ;
  END;
END;

Agora você chamaria show_super() de student para o método person, ou apenas show() para o método student.

Dos documentos, espero que ajude.