-- 建立对象类型
CREATE OR REPLACE TYPE person_type2 AS OBJECT
(
ID NUMBER,
NAME VARCHAR2(20),
phone VARCHAR2(20),
address VARCHAR2(64),
CONSTRUCTOR FUNCTION person_type2
(
ID NUMBER,
NAME VARCHAR2
) RETURN SELF AS RESULT,
MEMBER PROCEDURE change_address(addr VARCHAR2),
MEMBER FUNCTION get_info RETURN VARCHAR2
);
-- 建立对象类型
CREATE OR REPLACE TYPE BODY person_type2 IS
--构造方法
CONSTRUCTOR FUNCTION person_type2
(
ID NUMBER,
NAME VARCHAR2
) RETURN SELF AS RESULT
IS
BEGIN
self.id:=ID;
self.name:=NAME;
RETURN;
END;
-- 更改address的值
MEMBER PROCEDURE change_address(addr VARCHAR2)
IS
BEGIN
address := addr;
END;
-- 返回信息
MEMBER FUNCTION get_info RETURN VARCHAR2
IS
BEGIN
RETURN 'id:'||ID||',name:'||NAME||',phone:'||phone||',address:'||address;
END;
END;
DROP TABLE worker;
-- 创建包含对象类型的表
CREATE TABLE worker(
ID NUMBER PRIMARY KEY,
person person_type2,
create_time DATE DEFAULT SYSDATE,
remark VARCHAR2(256)
)
--查询包含对象的表
SELECT * FROM worker;
--测试对象
DECLARE
pt2 person_type2;
BEGIN
pt2 := person_type2(1,'huiwanpeng');
dbms_output.put_line(pt2.get_info());
pt2.change_address('成都');
dbms_output.put_line(pt2.get_info());
END;