This script builds a sample object type with member variables and functions
SQL>
SQL> CREATE OR REPLACE TYPE myType AUTHID CURRENT_USER IS OBJECT
2 ( fname VARCHAR2(20 CHAR)
3 , lname VARCHAR2(20 CHAR)
4 , CONSTRUCTOR FUNCTION myType RETURN SELF AS RESULT
5 , CONSTRUCTOR FUNCTION myType ( fname VARCHAR2, lname VARCHAR2 )RETURN SELF AS RESULT
6 , MEMBER PROCEDURE print_instance_variable
7 , ORDER MEMBER FUNCTION equals( my_class myType ) RETURN NUMBER )
8 INSTANTIABLE NOT FINAL;
9 /
Type created.
SQL>
SQL> CREATE OR REPLACE TYPE BODY myType AS
2 CONSTRUCTOR FUNCTION myType RETURN SELF AS RESULT IS
3 fname VARCHAR2(20 CHAR) := NULL;
4 lname VARCHAR2(20 CHAR) := NULL;
5 BEGIN
6 SELF.fname := fname;
7 SELF.lname := lname;
8 RETURN;
9 END;
10 CONSTRUCTOR FUNCTION myType(fname VARCHAR2, lname VARCHAR2)RETURN SELF AS RESULT IS
11 BEGIN
12 SELF.fname := fname;
13 SELF.lname := lname;
14 RETURN;
15 END;
16 MEMBER PROCEDURE print_instance_variable IS
17 BEGIN
18 DBMS_OUTPUT.PUT_LINE('Name ['||SELF.fname||', '||SELF.lname||']');
19 END;
20
21 ORDER MEMBER FUNCTION equals( my_class myType )RETURN NUMBER IS
22 false_value NUMBER := 0;
23 true_value NUMBER := 1;
24 BEGIN
25 IF SELF.fname = my_class.fname AND SELF.lname = my_class.lname THEN
26 RETURN true_value;
27 ELSE
28 RETURN false_value;
29 END IF;
30 END;
31 END;
32 /
Type body created.
SQL>
SQL> DECLARE
2 obj1 myType := myType;
3 obj2 myType := myType('A','B');
4 BEGIN
5
6 obj1.print_instance_variable;
7
8 obj2.print_instance_variable;
9
10 IF obj1.equals(obj2) = 1 THEN
11 DBMS_OUTPUT.PUT_LINE('equal.');
12 ELSE
13 DBMS_OUTPUT.PUT_LINE('unequal.');
14 END IF;
15 END;
16 /
Name [, ]
Name [A, B]
unequal.
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL>
Related examples in the same category