This script builds a sample object type with constructor. : Constructor « Object Oriented Database « Oracle PL / SQL






This script builds a sample object type with constructor.

   

SQL> CREATE OR REPLACE TYPE myType
  2  AUTHID CURRENT_USER IS OBJECT
  3  ( fname VARCHAR2(20 CHAR)
  4  , lname  VARCHAR2(20 CHAR)
  5  , CONSTRUCTOR FUNCTION myType RETURN SELF AS RESULT
  6  , CONSTRUCTOR FUNCTION myType ( fname VARCHAR2, lname  VARCHAR2 )RETURN SELF AS RESULT
  7  , MEMBER PROCEDURE print_instance_variable
  8  , ORDER MEMBER FUNCTION equals( my_class myType ) RETURN NUMBER )INSTANTIABLE NOT FINAL;
  9  /

Type created.

SQL>
SQL> SHOW ERRORS
No errors.
SQL>
SQL> CREATE OR REPLACE TYPE BODY myType AS
  2    CONSTRUCTOR FUNCTION myType
  3    RETURN SELF AS RESULT IS
  4      fname VARCHAR2(20 CHAR) := NULL;
  5      lname  VARCHAR2(20 CHAR) := NULL;
  6    BEGIN
  7      SELF.fname := fname;
  8      SELF.lname := lname;
  9      RETURN;
 10    END;
 11
 12    CONSTRUCTOR FUNCTION myType( fname VARCHAR2, lname  VARCHAR2 )
 13    RETURN SELF AS RESULT IS
 14    BEGIN
 15      SELF.fname := fname;
 16      SELF.lname := lname;
 17      RETURN;
 18    END;
 19    MEMBER PROCEDURE print_instance_variable IS
 20    BEGIN
 21      DBMS_OUTPUT.PUT_LINE('Name:'||SELF.fname||', '||SELF.lname);
 22
 23    END;
 24
 25    ORDER MEMBER FUNCTION equals( my_class myType )
 26    RETURN NUMBER IS
 27      false_value NUMBER := 0;
 28      true_value  NUMBER := 1;
 29    BEGIN
 30      IF SELF.fname = my_class.fname AND
 31         SELF.lname = my_class.lname   THEN
 32        RETURN true_value;
 33      ELSE
 34        RETURN false_value;
 35
 36      END IF;
 37
 38    END;
 39
 40  END;
 41  /

Type body created.

SQL>
SQL> SHOW ERRORS
No errors.
SQL>
SQL>

   
    
    
  








Related examples in the same category

1.Insert value with constructor method
2.Call object constructor in an insert statement
3.Use type Constructor to insert data to object table
4.Use constructor to create new objects
5.This script demonstrates the user-defined constructor method.
6.Demonstrates object initialization.