/**//**//**//** oracle对象实例及测试 */ ---(1)测试对象 createorreplace type PERSON as Object( name varchar2(100), sex char(2), birth date, pwd varchar2(32), member function geName returnvarchar2 ) createorreplace type body PERSON as member function geName returnvarcharis begin return self.birth;--self表示当前对象如同java里面的this end; end ; --(2)在plsql中使用对象 declare tuser person; begin tuser := person('joe2','01',sysdate,'123456'); dbms_output.put_line(tuser.geName);--调用方法 end; ---(3)在对象中使用构造函数,过程和静态属性和方法 /**//* 1 构造函数使用constructor声明 2 构造函数return self as result声明结尾, 3 构造函数属性不需要定义长度 eg:varchar2 static 不能用member声明 sttatic 不能声明属性,只能声明function和procedure static 的方法和函数使用的时候直接用 对象类型.XX */ createorreplace type testm as OBJECT( name varchar2(10), sex char(2), constructor function testm(--一个参数的构造函数 p_name varchar2 ) return self as result, constructor function testm(--二个参数的构造函数 p_name varchar2, p_sex char ) return self as result, member function getSex returnchar, static function getSexByCode(code inchar) returnvarchar2, member procedure getSexCN , static procedure getSexCNByCode(code inchar,str out varchar2) ); createorreplace type body testm is constructor function testm(--一个参数的构造函数 p_name varchar2 ) return self as result is begin self.name:=p_name; return; end ; constructor function testm(--一个参数的构造函数 p_name varchar2, p_sex char ) return self as result is begin self.name:=p_name; self.sex:=p_sex; return; end ; member function getSex returncharas begin return self.sex; end getSex; member procedure getSexCN as begin if self.sex='01'then dbms_output.put_line('男'); else dbms_output.put_line('女'); endif; end getSexCN; static function getSexByCode(code inchar) returnvarchar2as begin if code='01'then return'男'; elsif code ='02'then return'女'; elsereturn'中性'; endif; end getSexByCode; static procedure getSexCNByCode(code inchar,str out varchar2) as begin if code='01'then str:='男'; elsif code ='02'then str:='女'; elsestr:='中性'; endif; end getSexCNByCode; end; --测试 declare obj testm; sexstr varchar2(4); begin obj := testm('01'); dbms_output.put_line(obj.getSex); obj.getSexCN; dbms_output.put_line(testm.getSexByCode('03')); testm.getSexCNByCode('03',sexstr); dbms_output.put_line(sexstr); end; -------------------------------- --(4)类型继承--------------------------- /**//**//**//* 使用 under标示 被继承的对象必须定义为 NOT FINAL 如果类型仅仅作为超类,不被实例化(类似于java中的abstract)定义为:not instantiable */ createorreplace type PARENTOBJ as Object( name varchar2(100), sex char(2), member function geName returnvarchar2 ) NOT FINAl; create type body PARENTOBJ is member function geName returnvarchar2as begin return self.name; end; end; createorreplace type SUB_OBJ under PARENTOBJ( birth date ) --测试 declare obj SUB_OBJ; begin obj := SUB_OBJ('joe2','01',sysdate); dbms_output.put_line(obj.geName);--调用继承的方法,和继承的属性 end; --------------------------------------- --(5)重写------------------------------ /**//**//**//* 重写方法和过程需要在声明和body里面声明为overriding */ createorreplace type SUB_OBJ2 under PARENTOBJ( birth date, overriding member function geName returnvarchar2--重写父类的方法 ) createorreplace type body SUB_OBJ2 is member function geName returnvarchar2as begin return'我的名字是:'||self.name; end; end; --测试 declare obj SUB_OBJ2; begin obj := SUB_OBJ2('joe2','01',sysdate); dbms_output.put_line(obj.geName);--调用继承的方法,和继承的属性 end; --(6)使用对象定义表中的列---------- createtable test00( tuser person, remark varchar2(100) ) --insert into test00 values(person('joe','01',sysdate,'123456'),/*参数个数必须够*/'remarkcontent'); --select t.tuser.name from test00 t;--必须使用表别名 --delete from test00 t where t.tuser.name='joe'; ---------------------------------------------- --(7)使用对象定义整个表 createtable testoo1 of person; --insert into testoo1 values(person('joe2','01',sysdate,'123456')); --select t.* from testoo1 t; --delete from testoo1 where name='joe'; ------------------------------------------------ --(8)对象引用,每一列都是目标表中的列的引用 createtable testoo2( id numberprimarykey, tuesr ref person scope is testoo1 ) --insert into testoo2 values(1,(select ref(oo1) from testoo1 oo1 where oo1.name='joe')); --update testoo2 o2 set o2.tuesr=(select ref(oo1) from testoo1 oo1 where oo1.name='joe2') where o2.id=1 -------------------------------------------------