Changes between Version 10 and Version 11 of Classes
- Timestamp:
- 05/14/10 08:44:58 (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Classes
v10 v11 1 1 = Class Definitions = 2 2 3 Classes are declared using the '''class''' keyword followed by the classname. [[BR]] 3 A class provides the definition for the type of an object and the variables, properties, events, and methods that the class comprises.[[BR]] 4 In addition it may declare tests to exercise the class and contracts or constraints the class must conform to to ensure continuing validity. 5 6 Classes are declared using the '''class''' keyword followed by the name of the class.[[BR]] 7 The classname must start with an uppercase letter. [[BR]] 8 4 9 The class may have any of the usual [wiki:AccessModifiers access modifiers]. [[BR]] 5 The Superclass inheritance and Interface conformance are indicated with the '''inherits''' and '''implements''' keywords. 10 The Superclass inheritance and interface conformance are indicated with the '''inherits''' and '''implements''' keywords. 11 6 12 === Class Grammar === 7 13 {{{ … … 15 21 16 22 [ 17 [<Variables>]18 [<Properties>]19 [<Methods>20 [<ClassEnums>]21 [<Sigs>]22 [<Invariants>]23 [<Tests>]24 [<SharedClause>]23 [<Variables>] 24 [<Properties>] 25 [<Methods> 26 [<ClassEnums>] 27 [<Sigs>] 28 [<Invariants>] 29 [<Tests>] 30 [<SharedClause>] 25 31 ]... 26 }}} 27 28 29 == Interfaces == 30 31 Interfaces are declared in much the same way except the keyword is '''interface''' and method and property bodies are not defined.[[BR]] 32 Interface hierarchies (!SuperInterfaces) can also be specified using '''inherits''' 33 34 === Interface Grammar === 35 {{{ 36 interface <InterfaceName> 32 33 # empty (placeholder) class 34 class <ClassName> 37 35 [is <AccessModifiers>] 38 36 [has <Attributes>] 39 37 [where <GenericParam> must be <GenericConstraints>] 40 [inherits <BaseInterfaceName> [, <BaseInterfaceName>]... ] 38 [inherits <BaseClassName> ] 39 [implements <InterfaceName> [, <InterfaceName>]...] 41 40 [<DocString>] 42 43 [ 44 [<Properties>] 45 [<Methods> 46 [<ClassEnums>] 47 [<Sigs>] 48 [<Invariants>] 49 [<SharedClause>] 50 ]... 41 pass 51 42 }}} 52 43 … … 54 45 55 46 == Instance and Class Variables == 56 Instance and Class variables are declared within the class using keyword '''var'''. 57 47 Instance and Class variables are declared within the class using keyword '''var''' followed by the var name.[[BR]] 48 Like local variables they may be explicitly typed ( {{{ as <Type> }}} ) and if not are defaulted to type '''dynamic'''. 49 50 An initial value may be specified (if not the var gets its types 'empty' value ( 0, "" or nil)[[BR]] 51 (Idiomatically its usually preferable to explicitly initialise the var and not explicitly specify the Type). 52 53 [wiki:AccessModifiers AccessModifiers] usually specifying visibility can also be given. 54 58 55 === Instance and Class variable definition Grammar === 59 56 {{{ … … 74 71 var t as int = 99 75 72 is protected, shared 76 """ 77 Temporary backup value 78 """ 79 80 }}} 81 82 All Class and Instance variable names must start with a lowerCase Letter[[BR]] 83 84 If the name is prefixed with an '''_''' (single underscore) its accessType defaults to ''protected'' and it is directly accessible from within the class method code.[[BR]] 85 86 If the name is prefixed with an '''_ _''' (double underscore) its accessType defaults to ''private'' and it is directly accessible from within the class method code.[[BR]] 87 88 Without that prefix on the name the variables accessType is ''public'' (by default) and 73 """t is a Temporary backup value""" 74 75 }}} 76 77 All class and instance variable names must start with a lowerCase Letter[[BR]] 78 79 If the name is prefixed with an '''_''' (single underscore) its accessType defaults to '''protected''' and it is directly accessible from within the class method code.[[BR]] 80 81 If the name is prefixed with an '''_ _''' (double underscore) its accessType defaults to '''private''' and it is directly accessible from within the class method code.[[BR]] 82 83 Without that prefix on the name the variables accessType is '''public''' (by default) and 89 84 it must be accessed from within method code by prefixing the name with either '''this.''' or just '''.''' .[[BR]] 90 85 91 In either case the default [wiki:AccessModifier access ] setting can be adjusted with an access modifier clause.[[BR]]92 93 Class (or static) variables are specified with the access modifier '''shared''' as in '''is shared'''86 In either case the default [wiki:AccessModifiers access ] setting can be adjusted with an access modifier clause.[[BR]] 87 88 Class (or static) variables are specified with the access modifier '''shared''' as in {{{ is shared }}}[[BR]] 94 89 e.g. 95 90 {{{ … … 115 110 == Properties == 116 111 Define a property for the class 112 113 ''To be expanded'' 117 114 118 115 === Properties Grammar === … … 135 132 136 133 == Methods == 134 A method is a code block that contains a series of statements. [[BR]] 135 A program causes the statements to be executed by calling the method and specifying any required method arguments. 136 137 137 Methods are defined using the keyword '''def'''. 138 138 … … 161 161 162 162 If an init method is not specified the compiler runtime generates a noArg constructor that does nothing. 163 164 Constructor chaining can be done to another constructor in the same class (usually with a different number or Type of args) 163 Because this method is 'special' it is declared using '''cue''' rather than '''def'''. See [wiki:Cues cues] 164 165 Constructor chaining must be done explicitly to another constructor in the same class (usually with a different number or Type of args) 165 166 or to a Superclass constructor (using '''base.init''') - this call must be the first executable line in the init method. 166 167 … … 199 200 }}} 200 201 201 meth takes 3 args , the first and second are inward only and are dynamic and a string respectively, the third is only returned from the method as a string 202 sum takes a variable number of integer args 202 meth takes 3 args , the first and second are inward only and are dynamic and a string respectively, the third is only returned from the method as a string.[[BR]] 203 sum takes a variable number of integer args. 203 204 204 205 … … 214 215 215 216 == Example == 217 218 {{{ 219 class SimplestClass 220 pass 221 222 class VeryMinimal 223 """ Docstring for class """ 224 225 cue init 226 base.init 227 # Above is equivalent to no initialiser at all 228 # .... rest of initialiser code 229 230 def aMethod 231 print 'in aMethod of class [this]' 232 # more method code 233 }}} 234 235 A more extended example. 236 216 237 {{{ 217 238 #Interfaces … … 253 274 #_lifespan = 8 254 275 255 definit276 cue init 256 277 _age = 1 257 278 .liveWeight = 2