使用对象类型继承来访问父方法

时间:2013-07-10 13:18:50

标签: oracle object inheritance plsql

我有Object A的Object B子对象。在对象B中,我想从对象A调用一个方法。

在Oracle 11中我们可以使用它:

(SELF AS parent_object).parent_method

但我使用的是Oracle 10,但这不起作用。还有另一种方式吗?

谢谢

PS:我想在CONSTRUCTOR方法中做到这一点(可能会改变一些东西)

2 个答案:

答案 0 :(得分:1)

你说得对,11.1中引入了这个功能,如What's New in Object-Relational Features?中所述。

10g manual也谈到了这个限制。最好的解决方法是创建一个静态超类型方法:

  

使用PL / SQL实现方法时,不能调用超类型   使用super关键字或等效方法的对象方法   具有重写方法的派生对象。但是,你可以打电话给   静态超类型方法作为变通方法。请参阅“创建”中的示例   具有重写方法的子类型“用于定义超类型   和子类型函数。

答案 1 :(得分:0)

极端护理(这意味着,无论你将采取什么措施来预防问题,将来你无论如何都会在身体的不同部位受到很大的痛苦)你可以将孩子类型转换为父母输入并调用其方法。

需要极度小心,因为当然,通过调用此方法,您将丢失对成员字段所做的任何更改。因此,您只能使用不更改类型成员字段的基本方法。

create type type_1 as object (
    field1 number(1)
    ,member procedure init
) not final;
/

create or replace type body type_1 as

member procedure init as
begin
    field1 := 0;
    dbms_output.put_line('init works! Field1 = ' || field1);
end;

end;
/


create type type_2
under type_1 (
    field2 number(1)
    ,constructor function type_2(self in out type_2, arg number) return self as result
);
/

create or replace type body type_2 as

constructor function type_2(self in out type_2, arg number) return self as result as
    parent    type_1;
begin
    self.field2 := arg;
    self.field1 := -1;

    select self
    into parent
    from dual;

    parent.init();

    dbms_output.put_line('Child''s field1 = ' || self.field1);
    return;
end;

end;
/


declare
    child    type_2 := type_2(2);
begin
    return;
end;
/