Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
51
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Defining Object Constructors

Calls to methods of an uninitialized object raise the predefined exception NULL_ SELF_DISPATCH. When passed as arguments to IN parameters, attributes of an uninitialized object evaluate to NULL. When passed as arguments to OUT or IN OUT parameters, they raise an exception if you try to write to them.

Accessing Object Attributes

You refer to an attribute by name. To access or change the value of an attribute, you use dot notation:

DECLARE

r Rational := Rational(NULL, NULL); numerator INTEGER;

denominator INTEGER; BEGIN

denominator := r.den; -- Read value of attribute r.num := numerator; -- Assign value to attribute

Attribute names can be chained, which lets you access the attributes of a nested object type. For example, suppose you define object types Address and Student, as follows:

CREATE TYPE Address AS OBJECT ( street VARCHAR2(30),

city VARCHAR2(20), state CHAR(2), zip_code VARCHAR2(5)

);

CREATE TYPE Student AS OBJECT ( name VARCHAR2(20), home_address Address, phone_number VARCHAR2(10), status VARCHAR2(10), advisor_name VARCHAR2(20),

...

);

The Address attribute is an object type that has a zip_code attribute. If s is a

Student object, you access the value of its zip_code attribute as follows:

s.home_address.zip_code

Defining Object Constructors

By default, you do not need to define a constructor for an object type. The system supplies a default constructor that accepts a parameter corresponding to each attribute.

You might also want to define your own constructor:

To supply default values for some attributes. You can ensure the values are correct instead of relying on the caller to supply every attribute value.

To avoid many special-purpose procedures that just initialize different parts of an object.

To avoid code changes in applications that call the constructor, when new attributes are added to the type. The constructor might need some new code, for

Using PL/SQL Object Types 12-13

Calling Object Constructors

example to set the attribute to null, but its signature could remain the same so that existing calls to the constructor would continue to work.

For example:

CREATE OR REPLACE TYPE rectangle AS OBJECT

(

--The type has 3 attributes. length NUMBER,

width NUMBER, area NUMBER,

--Define a constructor that has only 2 parameters. CONSTRUCTOR FUNCTION rectangle(length NUMBER, width NUMBER)

RETURN SELF AS RESULT

);

/

CREATE OR REPLACE TYPE BODY rectangle AS

CONSTRUCTOR FUNCTION rectangle(length NUMBER, width NUMBER) RETURN SELF AS RESULT

AS BEGIN

SELF.length := length; SELF.width := width;

-- We compute the area rather than accepting it as a parameter. SELF.area := length * width;

RETURN;

END;

END;

/

DECLARE

r1 rectangle;

r2 rectangle; BEGIN

--We can still call the default constructor, with all 3 parameters. r1 := NEW rectangle(10,20,200);

--But it is more robust to call our constructor, which computes

--the AREA attribute. This guarantees that the initial value is OK. r2 := NEW rectangle(10,20);

END;

/

Calling Object Constructors

Calls to a constructor are allowed wherever function calls are allowed. Like all functions, a constructor is called as part of an expression, as the following example shows:

DECLARE

r1 Rational := Rational(2, 3);

FUNCTION average (x Rational, y Rational) RETURN Rational IS BEGIN

...

END; BEGIN

r1 := average(Rational(3, 4), Rational(7, 11)); IF (Rational(5, 8) > r1) THEN

...

END IF;

12-14 PL/SQL User's Guide and Reference

Calling Object Methods

END;

When you pass parameters to a constructor, the call assigns initial values to the attributes of the object being instantiated. When you call the default constructor to fill in all attribute values, you must supply a parameter for every attribute; unlike constants and variables, attributes cannot have default values. As the following example shows, the nth parameter assigns a value to the nth attribute:

DECLARE

r Rational; BEGIN

r := Rational(5, 6); -- assign 5 to num, 6 to den -- now r is 5/6

The next example shows that you can call a constructor using named notation instead of positional notation:

BEGIN

r := Rational(den => 6, num => 5); -- assign 5 to num, 6 to den

Calling Object Methods

Like packaged subprograms, methods are called using dot notation. In the following example, you call method normalize(), which divides attributes num and den (for "numerator" and "denominator") by their greatest common divisor:

DECLARE

r Rational; BEGIN

r := Rational(6, 8); r.normalize;

dbms_output.put_line(r.num); -- prints 3 END;

As the example below shows, you can chain method calls. Execution proceeds from left to right. First, member function reciprocal() is called, then member procedure normalize() is called.

DECLARE

r Rational := Rational(6, 8); BEGIN

r.reciprocal().normalize; dbms_output.put_line(r.num); -- prints 4

END;

In SQL statements, calls to a parameterless method require an empty parameter list. In procedural statements, an empty parameter list is optional unless you chain calls, in which case it is required for all but the last call.

You cannot chain additional method calls to the right of a procedure call because a procedure is called as a statement, not as part of an expression. For example, the following statement is not allowed:

r.normalize().reciprocal; -- not allowed

Also, if you chain two function calls, the first function must return an object that can be passed to the second function.

For static methods, calls use the notation type_name.method_name rather than specifying an instance of the type.

Using PL/SQL Object Types 12-15

Соседние файлы в папке Oracle 10g