欢迎光临
我们一直在努力

PL/SQL小技巧一个:在子类中怎么调用父类被重载的方法-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

在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
子类过程—调用父类过程之后

虽说这有点儿麻烦,父类有几个被重载的方法,你就要在父类父加几个另外的方法。
但也是没办法的办法,’曲线救国’嘛。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » PL/SQL小技巧一个:在子类中怎么调用父类被重载的方法-数据库专栏,SQL Server
分享到: 更多 (0)

相关推荐

  • 暂无文章