To get started, lets review some terminology and I'm going to assume you are familiar with Java or C++. Most terms are the same, for instance in Java class methods are still methods in PL/SQL. However, class variables or fields are called attributes in pl/sql. Enough with the talking, time for code.
- CREATE OR REPLACE TYPE user_object AS OBJECT
- (
- id number,
- first_name varchar2(250), -- attribute
- last_name varchar2(250),
- email_address varchar2(1000),
- -- constructor specification
- CONSTRUCTOR FUNCTION user_object
- (
- id number,
- first_name varchar2,
- last_name varchar2,
- email_address varchar2)
- RETURN SELF AS RESULT,
- -- member procedure
- MEMBER PROCEDURE set_first_name
- (first_name varchar2),
- -- member function
- MEMBER FUNCTION get_first_name
- RETURN varchar2)
- INSTANTIABLE NOT FINAL;
- CREATE OR REPLACE TYPE BODY user_object AS
- CONSTRUCTOR FUNCTION user_object
- (
- id number,
- first_name varchar2,
- last_name varchar2,
- email_address varchar2)
- RETURN SELF AS RESULT IS
- BEGIN
- self.id := id;
- self.first_name := first_name;
- self.last_name := last_name;
- self.email_address := email_address;
- RETURN;
- END user_object;
- MEMBER PROCEDURE set_first_name (first_name varchar2)
- is
- BEGIN
- self.first_name := first_name;
- END set_first_name;
- MEMBER FUNCTION get_first_name
- RETURN varchar2
- IS
- BEGIN
- RETURN self.first_name;
- END get_first_name;
- END;
Okay, so what are we looking at here? We're looking at an OOP representation of a data structure. Thank you, Captain Obvious. The top statement is the object specification, notice I've specified 4 attributes, a constructor and two member methods. The constructor takes 4 parameters, one for each attribute. The member methods "set" and "get" the first name attribute.
So your next question, wow, this is great and all, but how can this be useful to me. The answer is simple, lets say I have a function that I need to return several pieces of information. You can then declare your function to return an object type.
This is a huge subject, however if there are some specifics that you are wondering about, just leave a comment and I'll try to answer them.
No comments:
Post a Comment