User-Defined Constructors : Constructor « Object Oriented « Oracle PL/SQL Tutorial

Oracle PL/SQL Tutorial
1. Introduction
2. Query Select
3. Set
4. Insert Update Delete
5. Sequences
6. Table
7. Table Joins
8. View
9. Index
10. SQL Data Types
11. Character String Functions
12. Aggregate Functions
13. Date Timestamp Functions
14. Numerical Math Functions
15. Conversion Functions
16. Analytical Functions
17. Miscellaneous Functions
18. Regular Expressions Functions
19. Statistical Functions
20. Linear Regression Functions
21. PL SQL Data Types
22. PL SQL Statements
23. PL SQL Operators
24. PL SQL Programming
25. Cursor
26. Collections
27. Function Procedure Packages
28. Trigger
29. SQL PLUS Session Environment
30. System Tables Data Dictionary
31. System Packages
32. Object Oriented
33. XML
34. Large Objects
35. Transaction
36. User Privilege
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL/SQL Tutorial » Object Oriented » Constructor 
32. 2. 3. User-Defined Constructors
SQL>
SQL> CREATE OR REPLACE TYPE EmployeeType AS OBJECT (
  2    id         NUMBER,
  3    first_name VARCHAR2(10),
  4    last_name  VARCHAR2(10),
  5    dob        DATE,
  6    phone      VARCHAR2(12),
  7    CONSTRUCTOR FUNCTION EmployeeType(
  8      p_id         NUMBER,
  9      p_first_name VARCHAR2,
 10      p_last_name  VARCHAR2
 11    RETURN SELF AS RESULT,
 12    CONSTRUCTOR FUNCTION EmployeeType(
 13      p_id         NUMBER,
 14      p_first_name VARCHAR2,
 15      p_last_name  VARCHAR2,
 16      p_dob        DATE,
 17      p_phone      VARCHAR2
 18    RETURN SELF AS RESULT
 19  );
 20  /

Type created.

SQL> CREATE OR REPLACE TYPE BODY EmployeeType AS
  2    CONSTRUCTOR FUNCTION EmployeeType(
  3      p_id         NUMBER,
  4      p_first_name VARCHAR2,
  5      p_last_name  VARCHAR2
  6    RETURN SELF AS RESULT IS
  7    BEGIN
  8      SELF.id := p_id;
  9      SELF.first_name := p_first_name;
 10      SELF.last_name := p_last_name;
 11      SELF.dob := SYSDATE;
 12      SELF.phone := '555-1212';
 13      RETURN;
 14    END;
 15    CONSTRUCTOR FUNCTION EmployeeType(
 16      p_id         NUMBER,
 17      p_first_name VARCHAR2,
 18      p_last_name  VARCHAR2,
 19      p_dob        DATE,
 20      p_phone      VARCHAR2
 21    RETURN SELF AS RESULT IS
 22    BEGIN
 23      SELF.id := p_id;
 24      SELF.first_name := p_first_name;
 25      SELF.last_name := p_last_name;
 26      SELF.dob := p_dob;
 27      SELF.phone := p_phone;
 28      RETURN;
 29    END;
 30  END;
 31  /

Type body created.

SQL>
SQL> DESC EmployeeType;
 Name               Null?    Type
----------------------------------------------------------------------------------------------------
 ID                          NUMBER
 FIRST_NAME                  VARCHAR2(10)
 LAST_NAME                   VARCHAR2(10)
 DOB                         DATE
 PHONE                       VARCHAR2(12)

METHOD
------
 FINAL CONSTRUCTOR FUNCTION EMPLOYEETYPE RETURNS SELF AS RESULT
 Argument Name                  Type                    In/Out Default?
 ------------------------------ ----------------------- ------ --------
 P_ID                           NUMBER                  IN
 P_FIRST_NAME                   VARCHAR2                IN
 P_LAST_NAME                    VARCHAR2                IN

METHOD
------
 FINAL CONSTRUCTOR FUNCTION EMPLOYEETYPE RETURNS SELF AS RESULT
 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

SQL>
SQL> CREATE TABLE emp OF EmployeeType;

Table created.

SQL>
SQL> desc emp;
 Name                Null?    Type
----------------------------------------------------------------------------------------------------
 ID                           NUMBER
 FIRST_NAME                   VARCHAR2(10)
 LAST_NAME                    VARCHAR2(10)
 DOB                          DATE
 PHONE                        VARCHAR2(12)

SQL>
SQL> drop table emp;

Table dropped.

SQL>
SQL>
32. 2. Constructor
32. 2. 1. Inserting a row into an object table using constructor
32. 2. 2. Using the default constructor (the name of the class)
32. 2. 3. User-Defined Constructors
32. 2. 4. Construct user-defined type with subquery
32. 2. 5. Use constructor to create new objects
32. 2. 6. Demonstrates object initialization.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.