Call an object method: Difference between revisions

Content added Content deleted
m (→‎{{header|Kotlin}}: changed syntax highlight to kotlin)
(Added Algol 68)
Line 92: Line 92:
Hi there! ... Hello World!
Hi there! ... Hello World!
Hi there! ... Hello Universe!</pre>
Hi there! ... Hello Universe!</pre>
=={{header|ALGOL 68}}==
Algol 68 doesn't do OO as such, but it has structures, the members of which can be procedures. This allows OO features to be implemented, though without syntactic sugar, "this" pointers must be passed explicitly. In Algol 68, <code>a OF b</code> is used where most other languages would use <code>b.a</code>.
<br>
This means that a call to an instance method called "print" say of a class instance "a" in Algol 68 would be written <code>( print OF a )( a )</code>, - note the explicit "this" parameter - whereas <code>a.print</code> would be used in most other languages.
<br>
Class methods could be members of the structure (as here) without a "this" parameter, or could be procedures defined outside the structure.
<syntaxhighlight lang="algol68">
BEGIN # demonstrate a possible method of simulating class & instance methods #
# declare a "class" #
MODE ANIMAL = STRUCT( STRING species
, PROC( REF ANIMAL )VOID print # instance method #
, PROC VOID cm # class method #
);
# constructor #
PROC new animal = ( STRING species )REF REF ANIMAL:
BEGIN
HEAP ANIMAL newv := ANIMAL( species
, ( REF ANIMAL this )VOID:
print( ( "[animal instance[", species OF this, "]]" ) )
, VOID: print( ( "[animal class method called]" ) )
);
HEAP REF ANIMAL newa := newv;
newa
END # new animal # ;

REF ANIMAL a
:= new animal( "PANTHERA TIGRIS" ); # create an instance of ANIMAL #
cm OF a; # call the class method #
( print OF a )( a ) # call the instance method #
END
</syntaxhighlight>
{{out}}
<pre>
[animal class method called][animal instance[PANTHERA TIGRIS]]
</pre>

=={{header|Apex}}==
=={{header|Apex}}==
<syntaxhighlight lang="java">// Static
<syntaxhighlight lang="java">// Static
Line 98: Line 134:
// Instance
// Instance
myInstance.method(someParameter);</syntaxhighlight>
myInstance.method(someParameter);</syntaxhighlight>

=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
{{works with|AutoHotkey_L}}