![]() Chapter Contents |
![]() Previous |
![]() Next |
| SAS Component Language: Reference |
In Constructors _ Level One, the DDATA class implicitly extended OBJECT.CLASS. In fact, any class without an explicit EXTENDS clause in the CLASS statement extends OBJECT.CLASS. To explicitly extend a class, add the EXTENDS clause shown below:
class y extends x; endclass;In this case, class Y extends the class X. Alternatively, Y is a subclass of X, and X is the parent class of Y.
This enables Y to share X's functionality. For example, if the class X were
class x;
m: method;
put 'Hello';
endmethod;
endclass;and the class y were
class y extends x; endclass;then you could call the method M using an example of the class Y:
init: dcl y y = _new_ y(); y.m(); return;
| Access Modifiers _ Level Two |
| The DDATA Class as a Subclass _ Level Two |
class data;
public num type;
public string dname;
public string mode;
protected num fid;
data: method f: num n: string m:string;
fid = f;
dname = n;
mode = m;
endmethod;
endclass;In addition to the name, mode and file id, a type variable
is stored to indicate whether FDATA is an external file or a SAS data set.
class ddata extends data;
/* Class data */
protected num nvars;
/* Constructor method */
ddata: method n: string m:string nv:num;
fid = open(n, m);
_super(fid, n, m);
nvars = nv;
type = 1;
endmethod;In this example, the DDATA constructor will call the data constructor
via the _super call. This sets the name, mode and file identifier that are
stored in the parent class data. The DDATA constructor still sets nvars and also sets the type field to indicate that the file
is a data set. The rest of the class will remain the same.
| The FDATA Class _ Level Two |
class fdata extends data;
/* Constructor method */
fdata: method n: string m: string;
dcl string ref = "";
dcl num rc = filename(ref, n);
fid = fopen(ref, m);
_super(fid, n, m);
type = 2;
endmethod;
/* FREAD method */
read: method return=num;
dcl num rc = fread(fid);
return rc;
endmethod;
/* FGET method */
cpy: method n: num return=string;
dcl string c = "";
dcl num rc = fget(fid, c);
return c;
endmethod;
/* FCLOSE method */
_term: method /(state='O');
if (fid) then fclose(fid);
_super();
endmethod;
endclass;Use FDATA to read an external class by instantiating it and looping
through the data:
init:
dcl fdata f = _new_ fdata("some_file", "i");
dcl num more = ^f.read();
do while(more);
dcl string s s2;
s = f.cpy(1);
s2 = f.cpy(2);
put s s2;
more = ^f.read();
end;
f._term();
return;This code assumes that the external file is formatted with each line
containing two character variables separated by a blank. For example:
Geoffrey Chaucer Samuel Johnson Henry Thoreau George Eliot Leo Tolstoy
| Overloaded Methods _ Level Two |
cpy: method return=string;
dcl string c="";
dcl num rc = fget(fid, c);
return c;
endmethod;
cpy: method n: num return=string;
return cpy();
endmethod;In this example, the original CPY method ignores the parameter and
calls a CPY method that returns the character value. By doing this, you have
defined two methods that have the same name but different parameter types.
With this simple change, you do not have to worry about which method to call.
The CPY method can be used as follows:
s = f.cpy();Overloaded methods can be used any time you need to have multiple methods with the same name but different parameter lists. For example, you may have several methods that are conceptually related but which operate on different types of data, or you may want to create a method with an optional parameter, as in the CPY example.
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.