繼承
?
??? 1、創(chuàng)建父類型
?
??? create
or
replace
type
person
as
object
????? (
???????
NAME
varchar2
(
10
),
??????? SEX
char
(
2
),
??????? BIRTHDATE
date
,
??????? PLACE
varchar2
(
100
),
???????
member
function
get_name
return
varchar2
????? )
not
final
;
--
必須注明,默認(rèn)為
final
????
?
??? create
or
replace
type
body
person
is
????
?
member
function
get_name
return
varchar2
is
???????
begin
?????????
return
self.name;
???????
end
get_name;
??? end
;
?
??? 2、創(chuàng)建person的子類
?
??? create
or
replace
type
employee
under
person
????? (
??????? emp_id
varchar2
(
10
),
??????? dep_id
varchar2
(
10
),
??????? job
varchar2
(
20
)
????? );
?
??? 3、應(yīng)用父類和子類
?
??? declare
????? person_one person;
????? employee_one employee;
??? begin
????? person_one:=person(
'
李四
'
,
'
男
'
,
date
'2008-10-20'
,
'
上海
'
);
????? employee_one:=employee(
'
小張
'
,
'
女
'
,
date
'2008-10-11'
,
'
杭州
'
,
'123456'
,
null
,
null
);
????? dbms_output.put_line(person_one.get_name);
????? dbms_output.put_line(employee_one.get_name);
??? end
;
?
??? 注:與其他OOP一樣,子類繼承父類的參數(shù)及函數(shù),但注意子類的參數(shù)順序,都在父類之后順序排列。
?
??? 4、父類可引用子類實例:
?
??? declare
????? person_one person;
????? employee_one employee;
??? begin
????? person_one:=employee(
'
小張
'
,
'
女
'
,
date
'2008-10-11'
,
'
杭州
'
,
'123456'
,
null
,
null
);
--
正確!
????? employee_one:=person(
'
李四
'
,
'
男
'
,
date
'2008-10-20'
,
'
上海
'
);
--
錯誤,子類不可引用父類實例!
????? dbms_output.put_line(person_one.name);
--
正確!
????? dbms_output.put_line(person_one.emp_id);
--
錯誤,父類忽略子類參數(shù)!
??? end
;
?
?
重寫
?
??? 1、建立父類
?
??? create
or
replace
type
person
as
object
????? (
???????
NAME
varchar2
(
10
),
??????? SEX
char
(
2
),
??????? BIRTHDATE
date
,
??????? PLACE
varchar2
(
100
),
???????
member
procedure
show_msg
????? )
not
final
;
????
?
??? create
or
replace
type
body
person
is
????
?
member
function
show_msg
return
varchar2
is
???????
begin
????????? dbms_output.put_line(
name
||
'/'
||sex||
'/'
||birthdate);
???????
end
show_msg;
??? end
;
?
??? 2、創(chuàng)建子類
?
??? create
or
replace
type
employee
under
person
????? (
??????? emp_id
varchar2
(
10
),
??????? dep_id
varchar2
(
10
),
??????? job
varchar2
(
20
),
???????
overriding
member
procedure
show_msg--關(guān)鍵字overriding
????? );
?
??? create
or
replace
type
body
employee
is
????
?
overriding
member
procedure
show_msg
is
???????
begin
????????? dbms_output.put_line(emp_id||
'/'
||dep_id||
'/'
||job);
???????
end
show_msg;
??? end
;
?
??? 3、重寫方法的調(diào)用
?
??? declare
????? person_one person;
????? employee_one employee;
??? begin
????? person_one:=person(
'
李四
'
,
'
男
'
,
date
'2008-10-20'
,
'
上海
'
);
????? employee_one:=employee(
'
小張
'
,
'
女
'
,
date
'2008-10-11'
,
'
杭州
'
,
'123456'
,
'11'
,
'22'
);
????? person_one.show_msg;
--
李四
/
男
/20-OCT-08
????? employee_one.show_msg;
--123456/11/22
??? end
;
?
??? 4、類多態(tài)
?
??? declare
????? person_one person;
????? person_two person;
??? begin
????? person_one:=person(
'
李四
'
,
'
男
'
,
date
'2008-10-20'
,
'
上海
'
);
????? person_two:=employee(
'
小張
'
,
'
女
'
,
date
'2008-10-11'
,
'
杭州
'
,
'123456'
,
'11'
,
'22'
);
????? person_one.show_msg;
--
李四
/
男
/20-OCT-08
????? person_two.show_msg;
--123456/11/22
??? end
;
?
?
對象表
?
??? 要區(qū)分對象表和將對象作為字段的表。
??? 對象表是基于對象類型創(chuàng)建的表,對象表的每一行都代表一個對象。
?
??? 1、創(chuàng)建對象表
?
??? create
or
replace
type
person
as
object
????? (
???????
NAME
varchar2
(
10
),
??????? SEX
char
(
2
),
??????? BIRTHDATE
date
,
??????? PLACE
varchar2
(
100
)
????? )
;
?
??? create
table
t_person
of
person;
--
注意格式!
?
??? 2、插入數(shù)據(jù)(2種方法)
?
??? insert
into
t_person
values
(
'
張三
'
,
'
男
'
,
date
'2008-10-11'
,
'
杭州
'
);
--
直接插入
?
??? insert
into
t_person
values
(person(
'
李四
'
,
'
男
'
,
date
'2008-10-20'
,
'
上海
'
));
--
插入實例
?
???
注:其實第一種方法是由系統(tǒng)隱式創(chuàng)建了對象實例,然后插入
?
??? 3、查詢數(shù)據(jù)(2種方法)
?
??? SQL
>
select
*
from
t_person;
?
??? NAME
????
SEX? BIRTHDATE PLACE
??? -------- ---- --------- --------
???
張三
?????
男
??
11
-OCT-
08
?
杭州
???
李四
????
男
??
20
-OCT-
08
?
上海
?
?
??? SQL
>
select
value
(a)
from
t_person a;
?
??? VALUE
(A)(
NAME
, SEX, BIRTHDATE, PLACE)
??? ----------------------------------------
??? PERSON(
'
張三
'
,
'
男
'
,
'11-OCT-08'
,
'
杭州
'
)
??? PERSON(
'
李四
'
,
'
男
'
,
'20-OCT-08'
,
'
上海
'
)
?
?
對象表外鍵
?
??? 1、對象表之間外鍵通過REF指針實現(xiàn)創(chuàng)建,首先創(chuàng)建外部表
?
??? create
table
t_person_f(
????? emp_id
integer
,
????? emp_msg
ref
person
scope
is
t_person
????? );
?
??? 注1:定義字段emp_msg為person對象類型的ref指針
??? 注2:scope表示實例來源,只能從表t_person中獲取
?
??? 2、向外部表中插入數(shù)據(jù)
?
??? insert
into
t_person_f
??? select
123
,
ref
(a)
from
t_person a
??? where
a.name=
'
張三
'
;
?
???
注:必須使用select語句通過ref獲取實例
?
??? 3、查詢外部表,通過deref來解析
?
??? SQL
>
select
*
from
t_person_f;
--
無法查詢真實信息
?
?????EMP_ID????? EMP_MSG
??? ----------?? ----------------------------------------------
????
?
123
????????
0000220208
C
5A
9B48F328840D7BF1B44EA41B24A11F8
?
?
??? SQL
>
select
emp_id,
deref
(emp_msg)
from
t_person_f;
?
????? EMP_ID????
DEREF
(EMP_MSG)(
NAME
, SEX, BIRTHDATE, PLACE)
??? ---------?? -------------------------------------------------
??????
123
?????? PERSON(
'
張三
'
,
'
男
'
,
'11-OCT-08'
,
'
杭州
'
)
?
??? 4、外鍵管理
?
??? 對象表要求外鍵引用的行必須存在,而ref則沒有這個限制,刪除時會指向空值,解析為NULL。
?
??? delete
from
t_person
where
name
=
'
張三
'
;
--
刪除對象
?
?
??? SQL > select emp_id,deref(emp_msg) from t_person_f;
?
????? EMP_ID???? DEREF(EMP_MSG)(NAME, SEX, BIRTHDATE, PLACE)
??? ---------?? -------------------------------------------------
?????? 123??????
?
??? 可以使用dangling關(guān)鍵字,查詢所有被懸空的ref
?
??? SQL
>?
select
emp_id
from
t_person_f
where
emp_msg
is
dangling
;
?
??????? EMP_ID
??? ----------
??????????
123
?
?
?
-The End-