Example: Creating An Object-Oriented Application in SCL |
Before beginning the tutorial, you must have a clear understanding of a simple SCL class. A CLASS statement enables you to use SCL to create a SAS/AF class and to define all the properties for the class, including attributes, methods, events, and interfaces. An SCL class is created with SCL class syntax. The simplest class is an empty class, which is defined using the following code:
class x; endclass;
Enter the above code in an SCL entry such as X.SCL and then create the class by using the SAVECLASS command. You should now see a CLASS entry for X in the current catalog.
Note: The name of the entry does not have to match the name of the class, but for beginners this is the easiest way to name an entry.
To add functionality to this class, you can create a simple method by using the following code:
class x; m: method; put 'Hello'; endmethod; endclass;
The PUT statement will write Hello to the SAS procedure output file or to a file that is specified in the most recent FILE statement. To run this method, you need to create an example of the class and call the method. In an SCL entry called Y.SCL, enter the following code:
init: dcl x x = _new_ x(); x.m(); return;
The _NEW_ operator is used to create an example of the class X and to assign it to the variable x. The _NEW_ operator provides a faster and more direct way to create an object by combining the actions of loading a class with LOADCLASS and initializing the object with the _new method, which invokes the object's _init method. You then call method M using the object variable x in the dot notation expression x.m().
Note: Dot notation provides a shortcut for invoking methods and for setting or querying attribute values. Using dot notation reduces typing and makes SCL programs easier to read. It also enhances run-time performance if you declare the object used in the dot notation as an example of a predefined class instead of a generic object. The object's class definition is then known at compile time. Because dot notation checks the method signature, it is the only way to access an overloaded method as illustrated in Overloaded Methods.
Compile Y.SCL, and then use the TESTAF command to run it. You should see the following output:
Hello
Copyright © 2009 by SAS Institute Inc., Cary, NC, USA. All rights reserved.