Call an object method: Difference between revisions

Content deleted Content added
Objeck (talk | contribs)
Hansoft (talk | contribs)
Added Forth (4tH) version
Line 80:
 
In E, there are no distinguished "static methods". Instead, it is idiomatic to place methods on the maker of the object. This is very similar to methods on constructors in JavaScript, or class methods in Objective-C.
=={{header|Forth}}==
{{works with|4tH|3.62.0}}
{{trans|D}}
There are numerous, mutually incompatible object oriented frameworks for Forth. This one works with the FOOS preprocessor extension of [[4tH]].
<lang forth>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</lang>
 
=={{header|Go}}==