|René Nyffenegger's collection of things on the web|
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
Constructors are used to initialize an object. In most case, this initialization assigns values to the object's members.
A constructor is a function whose name is equal to its object's name. The
While an ordinary function returns some type, a constructor function returns
The following example declares an object with one member (txt) and a constructor that takes two arguments:
create type ctor_test as object ( txt varchar2(100), constructor function ctor_test(pat varchar2, repeat number) return self as result, member procedure print ); /
Here's the body of the object. The constructor initializes the object's member (txt);
create type body ctor_test as constructor function ctor_test(pat varchar2, repeat number) return self as result is begin for iii in 1 .. repeat loop txt := txt || pat; end loop; return; end; member procedure print is begin dbms_output.put_line(txt); end print; end; /
Let's see it in action:
Here's the output:
See also this link.