8. Classes

Start felix section to tut/tutorial/tut-7-0.flx[1 /1 ]
     1: #line 5733 "./lpsrc/flx_tutorial.pak"
     2: //Check classes
     3: #import <flx.flxh>
     4: open Long;
     5: 
     6: class Y {
     7:   // variables
     8:   val c : int;
     9:   var x : int;
    10:   var y : long;
    11: 
    12:   // accessor methods
    13:   fun fetchc():int =>c;
    14: 
    15:   // mutators
    16:   proc setx(a:int) { x = a; }
    17:   proc setxy( a:int, b:long) { x = a; y = b; }
    18: 
    19:   // accessor procedures
    20:   proc yprint() {
    21:     print "This is a Y object, with x = ";
    22:     print x; print ", y = "; print y;
    23:     print ", and c = "; print c; endl;
    24:   }
    25: 
    26:   // test methods calling methods
    27: 
    28:   fun f(a:int):int => g a;
    29:   fun g(a:int):int => a + x;
    30: 
    31:   // constructors
    32:   ctor () {}
    33:   ctor (a:int): c(20000) { x = a; }
    34: };
    35: 
    36: var ob <- new Y(99);
    37: ob.x = 2;
    38: ob.y = 3L;
    39: 
    40: print ob.c; endl;
    41: print ob.x; endl;
    42: print ob.y; endl;
    43: print$ ob.fetchc(); endl;
    44: 
    45: ob.setx 22;
    46: ob.yprint();
    47: 
    48: ob.setxy (12,33L);
    49: ob.yprint;
    50: 
    51: print$ ob.f 1; endl;
    52: 
    53: proc ncl() {
    54:   var x = 42;
    55:   class J {
    56:     var j:int;
    57:     ctor (a:int) { j = a; }
    58:     fun h(q:int):int => q + j + x;
    59:   };
    60:   var b <- new J(100);
    61:   print$ b.h 1000; endl;
    62: };
    63: 
    64: ncl;
End felix section to tut/tutorial/tut-7-0.flx[1]
Start data section to tut/tutorial/tut-7-0.expect[1 /1 ]
     1: 20000
     2: 2
     3: 3
     4: 20000
     5: This is a Y object, with x = 22, y = 3, and c = 20000
     6: This is a Y object, with x = 12, y = 33, and c = 20000
     7: 13
     8: 1142
End data section to tut/tutorial/tut-7-0.expect[1]


8.1. Polymorphic Classes
8.2. Polymorphic Methods