Call an object method: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: added solution) |
m (→{{header|Phix}}: added syntax colouring, marked p2js incompatible) |
||
Line 1,459: | Line 1,459: | ||
Phix does not demand that all routines be inside classes; traditional standalone routines are "static" in every sense.<br> |
Phix does not demand that all routines be inside classes; traditional standalone routines are "static" in every sense.<br> |
||
There is no way to call a class method without an instance, since "this" will typecheck even if not otherwise used. |
There is no way to call a class method without an instance, since "this" will typecheck even if not otherwise used. |
||
<!--<lang Phix>(notonline)--> |
|||
Needs 0.8.1+ |
|||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no class in p2js)</span> |
|||
<lang Phix>class test |
|||
<span style="color: #008080;">class</span> <span style="color: #000000;">test</span> |
|||
string msg = "this is a test" |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">msg</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"this is a test"</span> |
|||
procedure show() ?this.msg end procedure |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">()</span> <span style="color: #0000FF;">?</span><span style="color: #7060A8;">this</span><span style="color: #0000FF;">.</span><span style="color: #000000;">msg</span> <span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
procedure inst() ?"this is dynamic" end procedure |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">inst</span><span style="color: #0000FF;">()</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"this is dynamic"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
end class |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span> |
|||
test t = new() |
|||
<span style="color: #000000;">test</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">()</span> |
|||
t.show() -- prints "this is a test" |
|||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">show</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- prints "this is a test"</span> |
|||
t.inst() -- prints "this is dynamic" |
|||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">inst</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- prints "this is dynamic"</span> |
|||
t.inst = t.show |
|||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">inst</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">show</span> |
|||
t.inst() -- prints "this is a test"</lang> |
|||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">inst</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- prints "this is a test"</span> |
|||
<!--</lang>--> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |