home *** CD-ROM | disk | FTP | other *** search
- MODULE Obs;
-
- (* Oberon program demonstrating OOP using extensible records *)
-
- IMPORT In,Out,Strings;
-
- TYPE
- ST50 = ARRAY 50 OF CHAR;
-
-
- (* someR is the base record *)
- someR = RECORD
- datatype : ST50;
- END;
-
- (* intR and chR are descendants of the base record *)
- intR = RECORD(someR)
- i : INTEGER;
- END;
-
- chR = RECORD(someR);
- c : CHAR;
- END;
-
-
- PROCEDURE Test2( VAR obR : someR );
- BEGIN
- (* Check the actual type of the record parameter using the
- IS operator. Note that IS can only be used with a
- pointer to a variable or, as here, with a VAR parameter *)
- IF obR IS chR THEN
- Out.String( "It's a char! - " );
- Out.Char( obR(chR).c );
- Out.Ln;
- ELSIF obR IS intR THEN
- Out.String( "It's an integer! - " );
- Out.Int( obR(intR).i, 2 );
- Out.Ln;
- ELSE
- Out.String( "It's something else!" );
- Out.Ln;
- END;
- END Test2;
-
-
- PROCEDURE Test1( VAR obR : someR );
- (* Find the data type by examining the datatype field *)
- BEGIN
- IF obR.datatype = 'chR' THEN
- Out.String( "It's a char! - " );
- Out.Char( obR(chR).c );
- Out.Ln;
- ELSIF obR.datatype = 'intR' THEN
- Out.String( "It's an integer! - " );
- Out.Int( obR(intR).i, 2 );
- Out.Ln;
- ELSE
- Out.String( "It's something else!" );
- Out.Ln;
- END;
- END Test1;
-
- PROCEDURE ProgMain*;
- VAR
- sr : someR;
- ir : intR;
- cr : chR;
- BEGIN
- (* Initialise the fields of the 3 record variables *)
- sr.datatype := 'someR';
- ir.datatype := 'intR';
- ir.i := 100;
- cr.datatype := 'chR';
- cr.c := 'X';
- (* First, pass the 3 types of record variable to Test1() *)
- Out.String( 'Test1' );
- Out.Ln;
- Test1( sr );
- Test1( ir );
- Test1( cr );
- (* Then, pass the same 3 record variables to Test2() *)
- Out.String( 'Test2' );
- Out.Ln;
- Test2( sr );
- Test2( ir );
- Test2( cr );
- END ProgMain;
-
- END Obs.
-