PL/SQL小技巧一個:在子類中怎么調用父類被重載的方法
2024-07-21 02:06:01
供稿:網友
國內最大的酷站演示中心!
在c++和java中,這是非常容易實現的
c++是:父類名::被重載的方法(參數表), 比如:
ancestorclass::name({arguments});
而在java中,可以用super代替父類,如這樣實現
super.name({arguments});
而在oracle 9i release2中都沒實現這樣的功能,
當然我們可以用其它辦法來實現這樣的功能。
父類對象類型
create or replace type parent as object (
rowsid integer,
member procedure printattr,
final member procedure printattr_parent --最好加final,防止子類對此方法進行重載
)not final;
/
create or replace type body parent is
member procedure printattr is
begin
printattr_parent;
end;
final member procedure printattr_parent is
begin
super.printattr; --此句是錯地,會拋出identifier ‘super.printattr’ must be declared. 因此要刪除此句。
dbms_output.put_line(‘父類方法,rowsid:=’||rowsid);
end;
end;
/
子類對象類型
create or replace type child under parent (
overriding member procedure printattr
)not final;
/
create or replace type body child is
overriding member procedure printattr is
begin
dbms_output.put_line(‘子類過程---調用父類過程之前’);
--在此處我們要用self.printattr,因為printattr不是直接在子類中定義的過程
self.printattr;
dbms_output.put_line(‘子類過程---調用父類過程之后’);
end;
end;
/
然后我們進行測試一下:
declare
vparent parent := parent(1);
vchild child := child(11);
begin
dbms_output.put_line(‘運行父類過程‘);
vparent.printattr;
dbms_output.put_line(‘運行子類過程‘);
vchild.printattr;
end;
運行結果:
運行父類過程
父類方法,rowsid:=1
運行子類過程
子類過程---調用父類過程之前
父類方法,rowsid:=11
子類過程---調用父類過程之后
雖說這有點兒麻煩,父類有幾個被重載的方法,你就要在父類父加幾個另外的方法。
但也是沒辦法的辦法,’曲線救國’嘛。