Wiki

Changes between Initial Version and Version 1 of Classes

Show
Ignore:
Timestamp:
05/04/08 09:01:13 (17 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Classes

    v1 v1  
     1= Class Definitions = 
     2 
     3Classes are declared using the '''class''' keyword. It may have any of the usual [wiki:AccessModifiers access modifiers].  
     4The Superclass inheritance and Interface conformance are indicated with the '''inherits''' and '''implements''' keywords. 
     5 
     6Interfaces are declared in much the same way except the keyword is '''interface''' and method and property bodies are not defined. 
     7Interface hierarchies (SuperInterfaces) can also be specified using '''inherits''' 
     8 
     9Instance 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) 
     20without prefix - public (by default ) override using is syntax access using . 
     21 
     22class variable   access modifier '''shared''' as in '''is shared''' 
     23   - how initialize?? 
     24 
     25 
     26Initializer/constructor 
     27 
     28properties 
     29 
     30accessing baseclass methods 
     31 
     32==Example == 
     33{{{ 
     34#Interfaces  
     35interface Audible 
     36    def makesNoise as String 
     37 
     38interface Mateable 
     39    pro mateName as String 
     40 
     41interface Breedable 
     42    inherits Mateable  
     43 
     44    pro childName as String 
     45 
     46         
     47# class declaration - note indentation 
     48class Bovidae 
     49    def diet as String 
     50        return 'Herbivorous Cud Chewing' 
     51      
     52    def nHooves as String 
     53        return 'four' 
     54   
     55 
     56class 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}}}