Member methods v17
A member method is a function or procedure that's defined in an object type and can be invoked only through an instance of that type. Member methods have access to, and can change the attributes of, the object instance on which they're operating.
This object type specification creates the emp_obj_typ
object type:
CREATE OR REPLACE TYPE emp_obj_typ AS OBJECT ( empno NUMBER(4), ename VARCHAR2(20), addr ADDR_OBJ_TYP, MEMBER PROCEDURE display_emp(SELF IN OUT emp_obj_typ) );
Object type emp_obj_typ
contains a member method named display_emp
. display_emp
uses a SELF
parameter, which passes the object instance on which the method is invoked.
A SELF
parameter is a parameter whose data type is that of the object type being defined. SELF
always refers to the instance that's invoking the method. A SELF
parameter is the first parameter in a member procedure or function regardless of whether it's explicitly declared in the parameter list.
The following code defines an object type body for emp_obj_typ
:
CREATE OR REPLACE TYPE BODY emp_obj_typ AS MEMBER PROCEDURE display_emp (SELF IN OUT emp_obj_typ) IS BEGIN DBMS_OUTPUT.PUT_LINE('Employee No : ' || empno); DBMS_OUTPUT.PUT_LINE('Name : ' || ename); DBMS_OUTPUT.PUT_LINE('Street : ' || addr.street); DBMS_OUTPUT.PUT_LINE('City/State/Zip: ' || addr.city || ', ' || addr.state || ' ' || LPAD(addr.zip,5,'0')); END; END;
You can also use the SELF
parameter in an object type body. Using the SELF
parameter in the CREATE TYPE BODY
command, you can write the same object type body as follows:
CREATE OR REPLACE TYPE BODY emp_obj_typ AS MEMBER PROCEDURE display_emp (SELF IN OUT emp_obj_typ) IS BEGIN DBMS_OUTPUT.PUT_LINE('Employee No : ' || SELF.empno); DBMS_OUTPUT.PUT_LINE('Name : ' || SELF.ename); DBMS_OUTPUT.PUT_LINE('Street : ' || SELF.addr.street); DBMS_OUTPUT.PUT_LINE('City/State/Zip: ' || SELF.addr.city || ', ' || SELF.addr.state || ' ' || LPAD(SELF.addr.zip,5,'0')); END; END;
Both versions of the emp_obj_typ
body are equivalent.