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

王朝mssql·作者佚名  2006-01-09
宽屏版  字体: |||超大  

在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

子类过程---调用父类过程之后

虽说这有点儿麻烦,父类有几个被重载的方法,你就要在父类父加几个另外的方法。

但也是没办法的办法,’曲线救国’嘛。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有