Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
40
Task/Call-an-object-method/Forth/call-an-object-method-1.fth
Normal file
40
Task/Call-an-object-method/Forth/call-an-object-method-1.fth
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
include lib/compare.4th
|
||||
include 4pp/lib/foos.4pp
|
||||
|
||||
[ASSERT] \ enable assertions
|
||||
|
||||
:: Cat
|
||||
class
|
||||
method: dynamicCat \ virtual method
|
||||
end-class {
|
||||
|
||||
:static staticCat { 2 } ; \ static method
|
||||
:method { s" Mew!" } ; defines dynamicCat
|
||||
} \ for unrelated classes,
|
||||
; \ method names have to differ
|
||||
|
||||
:: Dog
|
||||
class
|
||||
method: dynamicDog \ virtual method
|
||||
end-class {
|
||||
|
||||
:static staticDog { 5 } ;
|
||||
:method { s" Woof!" } ; defines dynamicDog
|
||||
} \ for unrelated classes,
|
||||
; \ method names have to differ
|
||||
|
||||
static Cat c \ create two static objects
|
||||
static Dog d
|
||||
|
||||
: main
|
||||
assert( class -> staticCat 2 = ) \ check for valid method return
|
||||
assert( class -> staticDog 5 = ) \ of a static method
|
||||
|
||||
assert( c -> staticCat 2 = ) \ check for valid method return
|
||||
assert( d -> staticDog 5 = ) \ of a static method
|
||||
|
||||
assert( c => dynamicCat s" Mew!" compare 0= )
|
||||
assert( d => dynamicDog s" Woof!" compare 0= )
|
||||
; \ same for dynamic methods
|
||||
|
||||
main
|
||||
29
Task/Call-an-object-method/Forth/call-an-object-method-2.fth
Normal file
29
Task/Call-an-object-method/Forth/call-an-object-method-2.fth
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
include FMS-SI.f
|
||||
|
||||
:class animal
|
||||
variable cnt 0 cnt ! \ static instance variable
|
||||
:m init: 1 cnt +! ;m
|
||||
:m cnt: cnt @ . ;m
|
||||
;class
|
||||
|
||||
:class cat <super animal
|
||||
:m speak ." meow" ;m
|
||||
;class
|
||||
|
||||
:class dog <super animal
|
||||
:m speak ." woof" ;m
|
||||
;class
|
||||
|
||||
cat Frisky \ instantiate a cat object named Frisky
|
||||
dog Sparky \ instantiate a dog object named Sparky
|
||||
|
||||
\ The class method cnt: will return the number of animals instantiated
|
||||
\ regardless of which animal object is used.
|
||||
|
||||
\ The instance method speak will respond differently depending
|
||||
\ on the class of the instance object.
|
||||
|
||||
Frisky cnt: \ => 2 ok
|
||||
Sparky cnt: \ => 2 ok
|
||||
Frisky speak \ => meow ok
|
||||
Sparky speak \ => woof ok
|
||||
Loading…
Add table
Add a link
Reference in a new issue