Call an object method: Difference between revisions
Content added Content deleted
(Added Quackery.) |
|||
Line 1,576: | Line 1,576: | ||
myInstance.myClassMethod(someParameter) |
myInstance.myClassMethod(someParameter) |
||
myInstance.myStaticMethod(someParameter)</lang> |
myInstance.myStaticMethod(someParameter)</lang> |
||
=={{header|Quackery}}== |
|||
Quackery is not an Object Oriented language. However it is possible to create a zen-like "distilled essence of object-orientation" in just a few lines of code. |
|||
Note that Quackery also does not have operator overloading (but see |
|||
[https://rosettacode.org/wiki/Modular_arithmetic3Quackery Modular arithmetic#Quackery] for an illustration of how this can be achieved) so there is limited inheritance of methods possible - here only the methods <code>localise</code> and <code>delocalise</code> are common to all object types. |
|||
(It would be possible to extend this technique to a more fully-fledged object oriented system. See Dick Pountain's slim volume "Object Oriented FORTH: Implementation of Data Structures" (pub. 1987) for an example of doing so in Forth; a language to which Quackery is related, but slightly less amenable to such shenanigans.) |
|||
<lang Quackery>( ---------------- zen object orientation -------------- ) |
|||
[ immovable |
|||
]this[ swap do ]done[ ] is object ( --> ) |
|||
[ ]'[ ] is method ( --> [ ) |
|||
[ method |
|||
[ dup share |
|||
swap put ] ] is localise ( --> ) |
|||
[ method [ release ] ] is delocalise ( --> ) |
|||
( -------------- example: counter methods -------------- ) |
|||
( to create a counter object, use: |
|||
"[ object 0 ] is 'name' ( [ --> )" ) |
|||
[ method |
|||
[ 0 swap replace ] ] is reset-counter ( --> [ ) |
|||
[ method |
|||
[ 1 swap tally ] ] is increment-counter ( --> [ ) |
|||
[ method [ share ] ] is report-counter ( --> [ ) |
|||
( -------------------- demonstration ------------------- ) |
|||
say 'Creating counter object: "mycounter".' cr cr |
|||
[ object 0 ] is mycounter ( [ --> ) |
|||
say "Initial value of mycounter: " |
|||
report-counter mycounter echo cr cr |
|||
say "Incrementing mycounter three times." cr |
|||
3 times [ increment-counter mycounter ] |
|||
say "Current value of mycounter: " |
|||
report-counter mycounter echo cr cr |
|||
say "Localising mycounter." cr cr |
|||
localise mycounter |
|||
say " Current value of mycounter: " |
|||
report-counter mycounter echo cr cr |
|||
say " Resetting mycounter." cr |
|||
reset-counter mycounter |
|||
say " Current value of mycounter: " |
|||
report-counter mycounter echo cr cr |
|||
say " Incrementing mycounter six times." cr |
|||
6 times [ increment-counter mycounter ] |
|||
say " Current value of mycounter: " |
|||
report-counter mycounter echo cr cr |
|||
say "Delocalising mycounter." cr cr |
|||
delocalise mycounter |
|||
say "Current value of mycounter: " |
|||
report-counter mycounter echo cr cr |
|||
say "Resetting mycounter." cr |
|||
reset-counter mycounter |
|||
say "Current value of mycounter: " |
|||
report-counter mycounter echo cr cr</lang> |
|||
{{out}} |
|||
<pre>Creating counter object: "mycounter". |
|||
Initial value of mycounter: 0 |
|||
Incrementing mycounter three times. |
|||
Current value of mycounter: 3 |
|||
Localising mycounter. |
|||
Current value of mycounter: 3 |
|||
Resetting mycounter. |
|||
Current value of mycounter: 0 |
|||
Incrementing mycounter six times. |
|||
Current value of mycounter: 6 |
|||
Delocalising mycounter. |
|||
Current value of mycounter: 3 |
|||
Resetting mycounter. |
|||
Current value of mycounter: 0</pre> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |