Sealed classes and methods: Difference between revisions

m
→‎{{header|Phix}}: added "more sensible way"
m (→‎{{header|Phix}}: added "more sensible way")
Line 66:
Sorry, Fred, you are too young to watch the movie.
</pre>
Of course you would get the same output from and it is usually much more sensible to do something like this:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">class</span> <span style="color: #000000;">Parent</span>
<span style="color: #008080;">private</span> <span style="color: #004080;">string</span> <span style="color: #000000;">name</span>
<span style="color: #008080;">private</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">age</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">watch_movie</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s is watching the movie...\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
<span style="color: #008080;">class</span> <span style="color: #000000;">Child</span> <span style="color: #008080;">extends</span> <span style="color: #000000;">Parent</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">watch_movie</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">age</span><span style="color: #0000FF;"><</span><span style="color: #000000;">15</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Sorry, %s, you are too young to watch the movie.\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s is watching the movie...\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
<!--</syntaxhighlight>-->
Note however in Phix there is no possibility of invoking parent.watch_movie() from the else branch, because you have completely replaced that routine in the child instance. Should you want to share code in that kind of fashion you would need to give it a different and completely unambiguous name.
 
=={{header|Wren}}==
7,794

edits