| 1 | = Class Definitions = |
| 2 | |
| 3 | Classes are declared using the '''class''' keyword. It may have any of the usual [wiki:AccessModifiers access modifiers]. |
| 4 | The Superclass inheritance and Interface conformance are indicated with the '''inherits''' and '''implements''' keywords. |
| 5 | |
| 6 | Interfaces are declared in much the same way except the keyword is '''interface''' and method and property bodies are not defined. |
| 7 | Interface hierarchies (SuperInterfaces) can also be specified using '''inherits''' |
| 8 | |
| 9 | Instance variables are declared within the class using keyword '''var'''. |
| 10 | {{{ |
| 11 | var <variableName> [ as <Type>] [ is <AccessModifiers>] |
| 12 | var _<variableName> [as <Type>] |
| 13 | |
| 14 | var x as String |
| 15 | var _y as int |
| 16 | var z as int is private |
| 17 | }}} |
| 18 | |
| 19 | _ prefix - private ( and directly accessable in methods) |
| 20 | without prefix - public (by default ) override using is syntax access using . |
| 21 | |
| 22 | class variable access modifier '''shared''' as in '''is shared''' |
| 23 | - how initialize?? |
| 24 | |
| 25 | |
| 26 | Initializer/constructor |
| 27 | |
| 28 | properties |
| 29 | |
| 30 | accessing baseclass methods |
| 31 | |
| 32 | ==Example == |
| 33 | {{{ |
| 34 | #Interfaces |
| 35 | interface Audible |
| 36 | def makesNoise as String |
| 37 | |
| 38 | interface Mateable |
| 39 | pro mateName as String |
| 40 | |
| 41 | interface Breedable |
| 42 | inherits Mateable |
| 43 | |
| 44 | pro childName as String |
| 45 | |
| 46 | |
| 47 | # class declaration - note indentation |
| 48 | class Bovidae |
| 49 | def diet as String |
| 50 | return 'Herbivorous Cud Chewing' |
| 51 | |
| 52 | def nHooves as String |
| 53 | return 'four' |
| 54 | |
| 55 | |
| 56 | class Sheep |
| 57 | is public, nonvirtual # public is default anyway |
| 58 | inherits Bovidae # Superclass |
| 59 | implements Audible, Breedable |
| 60 | |
| 61 | var _lifeSpan as int is shared # years average |
| 62 | # or var lifeSpan as int private shared |
| 63 | |
| 64 | var _age as int =1 |
| 65 | var liveWeight as int is private |
| 66 | var _isAlive as bool |
| 67 | |
| 68 | #shared |
| 69 | # _lifespan = 8 |
| 70 | |
| 71 | def init |
| 72 | _age = 1 |
| 73 | .liveWeight = 2 |
| 74 | _isAlive = true |
| 75 | |
| 76 | def makesNoise as String |
| 77 | return 'Baaaaa' |
| 78 | |
| 79 | pro mateName as String |
| 80 | get |
| 81 | return 'Ram' |
| 82 | set |
| 83 | pass |
| 84 | |
| 85 | pro childName as String |
| 86 | get |
| 87 | return 'lamb' |
| 88 | set |
| 89 | pass |
| 90 | |
| 91 | def age( byNYears as int) |
| 92 | _age += byNYears |
| 93 | if _age > _lifeSpan |
| 94 | _isAlive = false |
| 95 | |
| 96 | def gainWgt( wgt as int) |
| 97 | .liveWeight += wgt |
| 98 | |
| 99 | def toString as String is new |
| 100 | return 'Sheep:age=[_age], wgt=[.liveWeight], living=[_isAlive]' |
| 101 | |
| 102 | def main is shared |
| 103 | s = Sheep() |
| 104 | print 's=[s.toString]' |
| 105 | s.age(1) |
| 106 | |
| 107 | }}} |