PL / SQL:任何常见的祖先类?

时间:2014-09-08 11:29:44

标签: oracle class inheritance plsql ancestor

pl / sql中是否有共同的祖先类?

例如,在Java中," Object" class是所有类的共同祖先:

Object o;
o = new String("hi ancestor");          // a String is an Object

但是在pl / sql中我们不能说:

o Object;                               // Object class doesn't exist
o := new myclass('hi ancestor');

更新:这个问题更具理论性和实用性,只是为了知道pl / sql是否为所有类都有一个共同的祖先(root)类,因为我没有在它上面找到任何东西在文档中。答复证实,没有这样一个共同的阶级。无论如何,我将根据响应中的消息提供一些关于如何模拟公共根类的示例。

这样做的方法是Alex通过使用anydata类型给出的技巧,模拟对通用类的转换。允许任何类作为函数中的参数是有用的。使用示例:

create or replace type cla as object    -- class
(
    name  varchar2(50)
);


declare
    co  cla;         -- cla class object
    co2 cla;         -- another one
    o   anydata;     -- Object class simulation object
    ok  pls_integer; -- to take value returned by the re-cast
begin
    co := new cla('hi');                -- create cla object
    o := anydata.convertobject(co);     -- cast to anydata (not automatic)
    ok := anydata.getobject(o, co2);    -- re-cast to cla

    dbms_output.put_line('co.name: '  || co.name);
    dbms_output.put_line('co2.name: ' || co2.name);
end;

user272735提出的另一种方法是通过创建一个将成为所有根类的父类的类来模拟公共根类。例如:

create or replace type obj as object    -- Object class simulation
(
    oid  varchar2(1000)
)
not final;

create or replace type cla1  under obj  -- explicit inheritance
(
    name  varchar2(50)
);

create or replace type cla2  under obj  -- explicit inheritance
(
    money  number(6)
);


declare
    o1  cla1;       -- cla1 class object
    o1b cla1;       -- another one
    o2  cla2;       -- cla2 class object
    o2b cla2;       -- another one
    o   obj;        -- common ancestor
begin
    o1 := new cla1('1', 'hi');          -- create cla1 object
    o := o1;                            -- cast to obj (automatic)
    o1b := treat(o as cla1);            -- re-cast to cla1

    dbms_output.put_line('o1.name: '  || o1.name);
    dbms_output.put_line('o1b.name: ' || o1b.name);

    o2 := new cla2('2', 222);           -- create cla2 object
    o := o2;                            -- cast to obj (automatic)
    o2b := treat(o as cla2);            -- re-cast to cla2

    dbms_output.put_line('o2.money: '  || o2.money);
    dbms_output.put_line('o2b.money: ' || o2b.money);
end;

1 个答案:

答案 0 :(得分:3)

我不相信Java的意义。您可以使用ANYDATA

来获得类似的内容
declare
  o anydata;
begin
  o := anydata.convertobject(myclass('hi ancestor'));
end;
/

您可以阅读更多here,当然也可以阅读documentation,但这并不是很多例子。 Tom Kytes has talked about it too

但是考虑到你之前的问题,你可能最好在数据库中存储Java类来完成那些面向对象的东西,或者将它完全移出数据库......

相关问题