SQL> CREATE OR REPLACE TYPE empName AUTHID CURRENT_USER IS OBJECT
2 ( fname VARCHAR2(20 CHAR)
3 , lname VARCHAR2(20 CHAR)
4 , CONSTRUCTOR FUNCTION empName RETURN SELF AS RESULT
5 , CONSTRUCTOR FUNCTION empName ( fname VARCHAR2, lname VARCHAR2 )RETURN SELF AS RESULT
6 , MEMBER PROCEDURE print_instance_variable
7 , ORDER MEMBER FUNCTION equals( my_class empName ) RETURN NUMBER )
8 INSTANTIABLE NOT FINAL;
9 /
SQL>
SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE TYPE BODY empName AS
2 CONSTRUCTOR FUNCTION empName 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 empName(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 empName )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> CREATE OR REPLACE TYPE empType AUTHID CURRENT_USER IS OBJECT
2 ( ename empName
3 , address VARCHAR2(20 CHAR)
4 , city VARCHAR2(14 CHAR)
5 , state VARCHAR2(2 CHAR)
6 , postal_code VARCHAR2(5 CHAR)
7 , CONSTRUCTOR FUNCTION empType RETURN SELF AS RESULT
8 , CONSTRUCTOR FUNCTION empType(ename empName,address VARCHAR2,city VARCHAR2,state VARCHAR2,postal_code VARCHAR2 )RETURN SELF AS RESULT
9 , CONSTRUCTOR FUNCTION empType(fname VARCHAR2, lname VARCHAR2, address VARCHAR2,city VARCHAR2,state VARCHAR2,postal_code VARCHAR2 )RETURN SELF AS RESULT
10 , MEMBER PROCEDURE print_instance_variable, ORDER MEMBER FUNCTION equals( my_class empType ) RETURN NUMBER )
11 INSTANTIABLE NOT FINAL;
12 /
Type created.
SQL>
SQL>
SQL>
SQL> CREATE OR REPLACE TYPE BODY empType AS CONSTRUCTOR FUNCTION empType RETURN SELF AS RESULT IS
2 fname VARCHAR2(20 CHAR) := NULL;
3 lname VARCHAR2(20 CHAR) := NULL;
4 address VARCHAR2(20 CHAR) := NULL;
5 city VARCHAR2(14 CHAR) := NULL;
6 state VARCHAR2(2 CHAR) := NULL;
7 postal_code VARCHAR2(5 CHAR) := NULL;
8 BEGIN
9 SELF.ename := empName(fname,lname);
10 SELF.address := address;
11 SELF.city := city;
12 SELF.state := state;
13 SELF.postal_code := postal_code;
14 RETURN;
15 END;
16 CONSTRUCTOR FUNCTION empType(ename empName, address VARCHAR2, city VARCHAR2, state VARCHAR2, postal_code VARCHAR2 )
17 RETURN SELF AS RESULT IS
18 BEGIN
19 SELF.ename := ename;
20 SELF.address := address;
21 SELF.city := city;
22 SELF.state := state;
23 SELF.postal_code := postal_code;
24 RETURN;
25
26 END;
27
28 CONSTRUCTOR FUNCTION empType(fname VARCHAR2,lname VARCHAR2,address VARCHAR2,city VARCHAR2,state VARCHAR2,postal_code VARCHAR2 )
29 RETURN SELF AS RESULT IS
30 BEGIN
31 SELF.ename := empName(fname,lname);
32 SELF.address := address;
33 SELF.city := city;
34 SELF.state := state;
35 SELF.postal_code := postal_code;
36 RETURN;
37
38 END;
39 MEMBER PROCEDURE print_instance_variable IS
40 BEGIN
41 SELF.ename.print_instance_variable;
42 DBMS_OUTPUT.PUT_LINE('First Name : '||SELF.ename.fname);
43 DBMS_OUTPUT.PUT_LINE('Last Name : '||SELF.ename.lname);
44 DBMS_OUTPUT.PUT_LINE('Street Address: '||SELF.address);
45 DBMS_OUTPUT.PUT_LINE('City : '||SELF.city);
46 DBMS_OUTPUT.PUT_LINE('State : '||SELF.state);
47 DBMS_OUTPUT.PUT_LINE('Postal Code : '||SELF.postal_code);
48
49 END;
50
51 ORDER MEMBER FUNCTION equals( my_class empType)RETURN NUMBER IS
52 false_value NUMBER := 0;
53 true_value NUMBER := 1;
54 BEGIN
55 IF SELF.ename.equals(my_class.ename) = true_value AND SELF.address = my_class.address AND SELF.city = my_class.city AND SELF.state = my_class.state AND
56 SELF.postal_code = my_class.postal_code THEN
57 RETURN true_value;
58 ELSE
59 RETURN false_value;
60
61 END IF;
62
63 END;
64
65 END;
66 /
Type body created.
SQL>
SQL>
SQL>
SQL>
SQL> DECLARE
2 obj1 empType := empType('A','B','1 str','CA','E','111');
3 obj2 empType := empType(empName('A','B'),'1 rd','city','TX','222');
4 BEGIN
5 obj1.print_instance_variable;
6
7 obj2.print_instance_variable;
8
9 IF obj1.equals(obj2) = 1 THEN
10 DBMS_OUTPUT.PUT_LINE('equal.');
11 ELSE
12 DBMS_OUTPUT.PUT_LINE('unequal.');
13 END IF;
14 END;
15 /
Name [A, B]
First Name : A
Last Name : B
Street Address: 1 str
City : CA
State : E
Postal Code : 111
Name [A, B]
First Name : A
Last Name : B
Street Address: 1 rd
City : city
State : TX
Postal Code : 222
unequal.
PL/SQL procedure successfully completed.
SQL>
|