Referencing objects

更新时间:
复制 MD 格式

Use dot notation to access the attributes and call the methods of an object variable.

Syntax

Reference an attribute

<object>.<attribute>

Where:

object is the identifier assigned to the object variable.

attribute is the identifier of an object type attribute.

If attribute is itself of an object type, extend the chain to reach the nested attribute:

<object>.<attribute>.<attribute_inner>

Where:

attribute_inner is an identifier belonging to the object type that attribute references in the definition of object.

Call a member procedure or function

<object>.<prog_name>

Where:

object is the identifier assigned to the object variable.

prog_name is the identifier of the member procedure or function.

Call a static procedure or function

Static procedures and functions cannot be called through an object variable. Call them through the object type name instead:

<object_type>.<prog_name>

Where:

object_type is the identifier assigned to the object type.

prog_name is the identifier of the static procedure or function.

Examples

Access attributes with dot notation

The following anonymous block initializes an EMP_OBJ_TYPE object variable and reads its attributes, including the nested addr attribute of type ADDR_OBJ_TYPE:

DECLARE
    v_emp          EMP_OBJ_TYPE;
BEGIN
    v_emp := emp_obj_type(9001,'JONES',
        addr_obj_type('123 MAIN STREET','EDISON','NJ',08817));
    DBMS_OUTPUT.PUT_LINE('Employee No   : ' || v_emp.empno);
    DBMS_OUTPUT.PUT_LINE('Name          : ' || v_emp.ename);
    DBMS_OUTPUT.PUT_LINE('Street        : ' || v_emp.addr.street);
    DBMS_OUTPUT.PUT_LINE('City/State/Zip: ' || v_emp.addr.city || ', ' ||
        v_emp.addr.state || ' ' || LPAD(v_emp.addr.zip,5,'0'));
END;
OUTPUT
Employee No   : 9001
Name          : JONES
Street        : 123 MAIN STREET
City/State/Zip: EDISON, NJ 08817

Call a member procedure

The following anonymous block produces the same output by calling the display_emp member procedure instead of accessing attributes directly:

DECLARE
    v_emp          EMP_OBJ_TYPE;
BEGIN
    v_emp := emp_obj_type(9001,'JONES',
        addr_obj_type('123 MAIN STREET','EDISON','NJ',08817));
    v_emp.display_emp;
END;
OUTPUT
Employee No   : 9001
Name          : JONES
Street        : 123 MAIN STREET
City/State/Zip: EDISON, NJ 08817

The following anonymous block creates an instance of DEPT_OBJ_TYPE and calls its display_dept member procedure:

DECLARE
    v_dept          DEPT_OBJ_TYPE := dept_obj_type (20);
BEGIN
    v_dept.display_dept;
END;
OUTPUT
Dept No    : 20
Dept Name  : RESEARCH

Call a static function

The following block calls the static function get_dname directly on the dept_obj_type object type:

BEGIN
    DBMS_OUTPUT.PUT_LINE(dept_obj_type.get_dname(20));
END;
OUTPUT
RESEARCH