![]() Chapter Contents |
![]() Previous |
![]() Next |
| _NEW_ |
| Category: | Object Oriented |
| Syntax | |
| Examples | |
| Example 1: Creating an Instance of an Object and Running a Class Constructor | |
| Example 2: Using a Class That Has an Overloaded Constructor | |
| See Also | |
Syntax |
| object-id=_NEW_ class-name(<arg1, arg2, . . . , argn>); |
Type: Numeric, Classes, and Object
Type: Character
| Examples |
The _NEW_ operator enables you to create an instance of an object and run a class constructor all in one step.
X.SCL
class Complex;
private num x y;
Complex: method a1: num a2: num;
x = c.x;
y = c.y;
endmethod;
endclass;
Y.SCL
init:
dcl Complex c = _new_ Complex(1,2);
dcl Complex c2 = _new_ Complex(c);
return;
Where you have a class, Complex, that has an overloaded constructor, one version takes two numeric arguments, and the other takes a complex number.
In the first _NEW_ statement, you create a complex number, 1 + 2i, by passing in the real and imaginary parts (1 and
2) as arguments to the _NEW_ operator. _NEW_ first creates an instance of
the Complex class, and then calls the appropriate constructor based on its
argument list. In this case, it calls the constructor that takes two numeric
arguments.
In the second _NEW_ statement, you create a complex number by passing
in another complex number (in this case, the first one you created, 1 + 2i). _NEW_ calls the second constructor in the Complex
class -- the one that takes a complex number as an argument.
Constructors must always have the same name as the class in which they are located, and they must always be void methods (that is, they do not return a value). Constructors provide a convenient way to initialize a class before you begin using it.
For example, if you didn't use constructors in the above example, you would have to create another set of methods to initialize the complex number and call those separately, as in the following example:
X.SCL
class Complex;
private num x y;
set: method a1: num a2: num;
x = a1;
y = a2;
endmethod;
set: method c: Complex;
x = c.x;
y = c.y;
endmethod;
endclass;
Y.SCl
init:
dcl Complex c = _new_ Complex();
dcl Complex c2 = _new_ Complex();
c.set(1,2);
c2.set(c);
return;You can overload constructors as shown above, or you can have only
one constructor, or you can have none. If you don't supply a constructor in
a class, the _NEW_ operator still attempts to run a parent class constructor.
In the following example, the _NEW_ operator for the class Y calls the constructor
in X, since X is the parent of Y.
X.SCL
class x;
x: method n: num;
put n=;
endmethod;
endclass;
Y.SCL
class y extends x;
endclass;
Y.SCL
init:
dcl y y = _new_ y(100);
return;This behavior applies to all classes that are instantiated by using
_NEW_ -- even those that have no explicit constructors.
If you do not supply a constructor for your classes, you can still use _NEW_ with no arguments because OBJECT.CLASS (which all classes extend) contains a constructor that takes no arguments and performs no actions. This "dummy" constructor needs to be overridden if you want to supply a constructor that does not take an argument by using the following:
class x;
x: method /(state='0');
put 'in x constructor';
endmethod;
endclass;Constructors can be used only in conjunction with the _NEW_
operator. They are not called if the object is instantiated in some other
way, such as by using the _NEW_ method in CALL SEND, or by dropping the object
in a frame. In particular, in the case of a visual object, you cannot run
a constructor after the object is instantiated, but only before it is displayed.
Therefore, you cannot use a constructor to initialize the object. For these
types of objects, you must either use the _NEO_ operator or use the _NEW_
method with CALL
SEND.
| See Also |
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.