您的位置:首页 > 数据库

PL/SQL小技巧一个:在子类中怎么调用父类被重载的方法

2008-04-28 02:08 573 查看
在和中,这是非常容易实现的
C++是:父类名::被重载的方法(参数表),比如:
ancestorclass::name({arguments});
而在Java中,可以用super代替父类,如这样实现
Super.name({arguments});

而在9iRelease2中都没实现这样的功能,
当然我们可以用其它办法来实现这样的功能。

父类对象类型
CreateorRePLaceTypeparentasobject(
rowsIDinteger,
memberprocedureprintAttr,
finalmemberprocedureprintAttr_parent --最好加final,防止子类对此方法进行重载
)notfinal;
/

CreateorrePLaceTypebodyparentis
MemberprocedureprintAttris
Begin
printAttr_parent;
End;

finalMemberprocedureprintAttr_parentis
Begin
Super.printAttr; --此句是错地,会抛出identifier‘super.printAttr’mustbedeclared.因此要删除此句。
Dbms_output.put_line(‘父类方法,RowsID:=’||rowsID);
End;
End;
/

子类对象类型
CreateorrePLacetypechildunderparent(
OverridingmemberprocedureprintAttr
)notfinal;
/

CreateorrePLacetypebodychildis
OverridingmemberprocedureprintAttris
Begin
Dbms_output.put_line(‘子类过程---调用父类过程之前’);
--在此处我们要用self.printAttr,因为printAttr不是直接在子类中定义的过程
Self.printAttr;
Dbms_output.put_line(‘子类过程---调用父类过程之后’);
End;
End;
/

然后我们进行测试一下:
Declare
vParentparent:=parent(1);
vChildchild:=child(11);
begin
dbms_output.put_line(‘运行父类过程‘);
vParent.printAttr;
dbms_output.put_line(‘运行子类过程‘);
vChild.printAttr;
end;

运行结果:

运行父类过程
父类方法,RowsID:=1
运行子类过程
子类过程---调用父类过程之前
父类方法,RowsID:=11
子类过程---调用父类过程之后

虽说这有点儿麻烦,父类有几个被重载的方法,你就要在父类父加几个另外的方法
但也是没办法的办法,’曲线救国’嘛。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐