User-Defined Constructors


Like other object-oriented languages, you can define your own constructors to initialize the attributes of an object. You can define your own constructor to do things like programmatically default one or more attributes of an object.

The following example creates a type named person_typ2 that declares two constructor method signatures:

 CREATE OR REPLACE TYPE person_typ2 AS OBJECT (id NUMBER,  first_name VARCHAR2(10),  last_name VARCHAR2(10),  dob DATE,  phone VARCHAR2(12),  CONSTRUCTOR FUNCTION person_typ2(p_id NUMBER,  p_first_name VARCHAR2,  p_last_name VARCHAR2) RETURN SELF AS RESULT,  CONSTRUCTOR FUNCTION person_typ2(p_id NUMBER,  p_first_name VARCHAR2,  p_last_name VARCHAR2,  p_dob DATE,  p_phone VARCHAR2) RETURN SELF AS RESULT); / 

Notice the following about the constructors:

  • The keywords CONSTRUCTOR FUNCTION are used to identify constructors.

  • The keywords RETURN SELF AS RESULT indicate an object of the same type as person_typ2 is returned by each constructor.

  • The first constructor accepts three parameters, and the second constructor accepts five parameters.

The constructor signatures don t contain the actual code bodies for the constructors; the code is contained in the following statement:

 CREATE OR REPLACE TYPE BODY person_typ2 AS  CONSTRUCTOR FUNCTION person_typ2(p_id NUMBER,  p_first_name VARCHAR2,  p_last_name VARCHAR2) RETURN SELF AS RESULT IS  BEGIN  SELF.id := p_id;  SELF.first_name := p_first_name;  SELF.last_name := p_last_name;  SELF.dob := SYSDATE;  SELF.phone := '555-1212';  RETURN;  END;  CONSTRUCTOR FUNCTION person_typ2(p_id NUMBER,  p_first_name VARCHAR2,  p_last_name VARCHAR2,  p_dob DATE,  p_phone VARCHAR2) RETURN SELF AS RESULT IS  BEGIN  SELF.id := p_id;  SELF.first_name := p_first_name;  SELF.last_name := p_last_name;  SELF.dob := p_dob;  SELF.phone := p_phone;  RETURN;  END; END; / 

Notice the following:

  • The constructors use SELF to set the attributes of the object. For example, SELF.id := p_id sets the id attribute of the object to the p_id parameter that is passed to the constructor.

  • The first constructor sets the dob attribute to the datetime returned by the SYSDATE() function, and sets phone to 555-1212. The second constructor simply sets the attributes to the parameters passed to the constructor.

The following example describes person_typ2 :

  DESC person_typ2;  Name Null? Type  ----------------------------------------- -------- -------------------  ID NUMBER  FIRST_NAME VARCHAR2(10)  LAST_NAME VARCHAR2(10)  DOB DATE  PHONE VARCHAR2(12) METHOD ------  FINAL MEMBER FUNCTION PERSON_TYP2 RETURNS PERSON_TYP2  Argument Name Type In/Out Default?  ------------------------------ ----------------------- ------ --------  P_ID NUMBER IN  P_FIRST_NAME VARCHAR2 IN  P_LAST_NAME VARCHAR2 IN METHOD ------  FINAL MEMBER FUNCTION PERSON_TYP2 RETURNS PERSON_TYP2  Argument Name Type In/Out Default?  ------------------------------ ----------------------- ------ --------  P_ID NUMBER IN  P_FIRST_NAME VARCHAR2 IN  P_LAST_NAME VARCHAR2 IN  P_DOB DATE IN  P_PHONE VARCHAR2 IN 

You can then create a table of type person_typ2; for example:

 CREATE TABLE object_customers2 OF person_typ2; 



Oracle Database 10g SQL
Oracle Database 10g SQL (Osborne ORACLE Press Series)
ISBN: 0072229810
EAN: 2147483647
Year: 2004
Pages: 217

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net