Wiki

Changes between Version 1 and Version 2 of Classes

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

Add Grammar and redo descriptions . Add example

Legend:

Unmodified
Added
Removed
Modified
  • Classes

    v1 v2  
    11= Class Definitions = 
    22 
    3 Classes are declared using the '''class''' keyword. It may have any of the usual [wiki:AccessModifiers access modifiers].  
     3Classes are declared using the '''class''' keyword followed by the classname. [[BR]] 
     4The class  may have any of the usual [wiki:AccessModifiers access modifiers]. [[BR]] 
    45The 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  
     6=== Class Grammar === 
     7{{{ 
     8class <className>   
     9    [is <AccessModifiers>] 
     10    [has <Attributes>] 
     11    [inherits <BaseClassName> ] 
     12    [implements <InterfaceName> [, <InterfaceName>]...] 
     13    [<DocString>] 
     14 
     15    [ 
     16    [<Variables>] 
     17    [<Properties>] 
     18    [<Methods> 
     19    [<ClassEnums>] 
     20    [<Sigs>] 
     21    [<Invariants>] 
     22    [<Tests>] 
     23    [<SharedClause>] 
     24    ]... 
     25}}} 
     26 
     27== Interfaces == 
     28 
     29Interfaces are declared in much the same way except the keyword is '''interface''' and method and property bodies are not defined.[[BR]] 
     30Interface hierarchies (!SuperInterfaces) can also be specified using '''inherits''' 
     31 
     32=== Interface Grammar === 
     33{{{ 
     34interface <InterfaceName>   
     35    [is <AccessModifiers>] 
     36    [has <Attributes>] 
     37    [inherits <BaseInterfaceName> [, <BaseInterfaceName>]... ] 
     38    [<DocString>] 
     39 
     40    [  
     41    [<Properties>] 
     42    [<Methods> 
     43    [<ClassEnums>] 
     44    [<Sigs>] 
     45    [<Invariants>] 
     46    [<SharedClause>]  
     47    ]... 
     48}}} 
     49 
     50== Instance and Class Variables == 
     51Instance and Class variables are declared within the class using keyword '''var'''. 
     52 
     53=== Instance and Class variable definition Grammar ===  
     54{{{ 
     55    var <variableName> [as <Type>]  [is <AccessModifiers>] 
     56        [<DocString>] 
     57 
     58    var _<variableName> [as <Type>] = <initialValue> 
     59        [is <AccessModifiers>] 
     60        [has <Attributes>] 
     61        [<DocString>] 
     62}}} 
     63e.g. 
     64{{{ 
    1465    var x as String 
    1566    var _y as int 
     67    var _ya as int = 47 
    1668    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 == 
     69    var t as int = 99 
     70        is protected, shared 
     71        """ 
     72        Temporary backup value 
     73        """ 
     74 
     75}}} 
     76 
     77If the name is prefixed with an '''_''' its accessType defaults to ''protected'' and it is directly accessable from within the class method code.[[BR]] 
     78 
     79Without that prefix on the name the variables  accessType is ''public'' (by default) and  
     80it must be accessed from within method code by prefixing the name with either '''self.''' or just '''.'''.[[BR]] 
     81 
     82In either case the default accessModifier setting can be adjusted with an accessModifier clause. 
     83All Class and Instance variable names must start with a lowerCase Letter 
     84 
     85Class (or static) variables are specified with the access Modifier  '''shared''' as in '''is shared''' 
     86e.g. 
     87{{{ 
     88    var _x = 100 
     89    def tryx( opnd as int) as int 
     90        return _x + opnd 
     91 
     92    var y is protected  # default public 
     93    def tryy( opnd as int) as int 
     94        return .y + opnd 
     95 
     96    var statX as int = 45 
     97        is shared 
     98    def x  
     99        print .statX 
     100}}} 
     101 
     102 
     103== Properties == 
     104++TBDone 
     105 
     106=== Properties Grammar === 
     107 
     108== Methods == 
     109 
     110Methods are defined using the keyword '''def''' 
     111 
     112=== Method Grammar === 
     113{{{ 
     114}}} 
     115 
     116The initializer or constructor method of a class is a special method called '''init''' which is implicitly called when a class instance is constructed. 
     117Its responsible for setting the contents of a class instance to an initial state. 
     118It doesnt return any value and is not inherited (i.e baseclass initialization must be done with an explicit call to  
     119a baseclass constructor). 
     120 
     121If an init method is not specified the compiler runtime generates a noArg constructor that does nothing. 
     122 
     123Constructor chaining can be done to another constructor in the same class (usually with a different number or Type of args) 
     124or to a Superclass constructor  (using '''base.init''') - this call must be the first executable line in the init method. 
     125 
     126++accessing baseclass methods 
     127 
     128== Example == 
    33129{{{ 
    34130#Interfaces  
     
    59155    implements Audible, Breedable 
    60156    
    61     var _lifeSpan as int is shared      # years average 
    62      # or var lifeSpan as int private shared 
     157    var _lifeSpan as int = 8 
     158         is shared               # years average 
     159    # or var lifeSpan as int private shared 
    63160 
    64161    var _age as int =1 
     
    67164         
    68165        #shared 
    69         #       _lifespan = 8   
     166        #_lifespan = 8   
    70167         
    71168    def init 
    72169        _age = 1 
    73170        .liveWeight = 2 
    74                 _isAlive = true 
     171        _isAlive = true 
    75172 
    76173    def makesNoise as String 
     
    79176    pro mateName as String 
    80177        get 
    81                         return 'Ram' 
    82                 set  
    83                         pass 
     178            return 'Ram' 
     179        set  
     180            pass 
    84181                 
    85182    pro childName as String 
    86183        get 
    87                         return 'lamb' 
    88                 set 
    89                         pass 
     184            return 'lamb' 
     185        set 
     186            pass 
    90187 
    91188    def age( byNYears as int) 
     
    94191            _isAlive = false 
    95192 
    96         def gainWgt( wgt as int) 
    97                 .liveWeight += wgt 
     193    def gainWgt( wgt as int) 
     194        .liveWeight += wgt 
    98195 
    99196    def toString as String is new