Invoking a method : Object Instance « Object Oriented « Oracle PL/SQL Tutorial






SQL>
SQL> CREATE OR REPLACE TYPE address AS OBJECT
  2              (line1 VARCHAR2(20),
  3               line2 VARCHAR2(20),
  4               city VARCHAR2(20),
  5               state_code VARCHAR2(2),
  6               zip VARCHAR2(13),
  7    MEMBER FUNCTION get_address RETURN VARCHAR2,
  8    MEMBER PROCEDURE set_address
  9              (addressLine1 VARCHAR2,
 10               addressLine2 VARCHAR2,
 11               address_city VARCHAR2,
 12               address_state VARCHAR2,
 13               address_zip VARCHAR2)
 14  );
 15  /

Type created.

SQL> CREATE OR REPLACE TYPE BODY address AS
  2    MEMBER FUNCTION get_address RETURN VARCHAR2
  3    IS
  4    BEGIN
  5      RETURN (SELF.line1||' '||SELF.line2||' '||SELF.city||', '||
  6              SELF.state_code||' '||SELF.zip);
  7    END get_address;
  8    MEMBER PROCEDURE set_address (addressLine1 VARCHAR2,
  9                  addressLine2 VARCHAR2,
 10                  address_city VARCHAR2,
 11                  address_state VARCHAR2,
 12                  address_zip VARCHAR2)
 13    IS
 14    BEGIN
 15      line1 :=addressLine1;
 16
 17      line2 :=addressLine2;
 18
 19      city :=address_city;
 20
 21      state_code :=address_state;
 22
 23      zip :=address_zip;
 24
 25    END set_address;
 26  END;
 27  /

Type body created.

SQL>
SQL>
SQL> DECLARE
  2    off_add address;
  3    home_address address;
  4  BEGIN
  5    off_add :=address('10 Avenue','Suite 111','Vancouver','NJ','00000');
  6
  7    home_address :=address(null,null,null,null,null);
  8
  9    DBMS_OUTPUT.PUT_LINE(off_add.get_address());
 10
 11    home_address.set_address('161 Rd','#000','Vancouver','NJ','00000');
 12
 13    DBMS_OUTPUT.PUT_LINE(home_address.get_address());
 14  END;
 15  /
10 Avenue Suite 111 Vancouver, NJ 00000
161 Rd #000 Vancouver, NJ 00000

PL/SQL procedure successfully completed.








32.6.Object Instance
32.6.1.NULL objects and NULL attributes
32.6.2.Check nullment for object instance
32.6.3.Invoking a method
32.6.4.Call User-defined constructors