Polymorphism: Difference between revisions

Content added Content deleted
m (added whitespace before the TOC (table of contents), added a ;Task: (bold) header, added other whitespace to the task's preamble.)
Line 2,765: Line 2,765:
<lang perl6>multi print (Point $p) { $p.perl.print }
<lang perl6>multi print (Point $p) { $p.perl.print }
multi print (Circle $c) { $c.perl.print }</lang>
multi print (Circle $c) { $c.perl.print }</lang>

=={{header|Phix}}==
Phix is not object orientated, but naturally polymorphic. <br>
Destructors are not required, though you can use delete_routine if needed.<br>
Copy constructors are also not required, a plain '=' will do just fine (it uses copy on write semantics).<br>
You could embed routine_ids in the structures to emulate virtual functions.<br>
There are no private members here; for that I would write something that returns integer ids to the outside world.
<lang Phix>type point(object o)
return sequence(o) and length(o)=2 and atom(o[1]) and atom(o[2])
end type

function new_point(atom x=0, atom y=0)
return {x,y}
end function

type circle(object o)
return sequence(o) and length(o)=2 and point(o[1]) and atom(o[2])
end type

function new_circle(object x=0, atom y=0, atom r=0)
if point(x) then
r = y -- assume r got passed in y
return {x,r} -- {point,r}
end if
return {{x,y},r}
end function

point p = new_point(4,5)
circle c = new_circle(p,6)
?p
?c</lang>
{{out}}
<pre>
{4,5}
{{4,5},6}
</pre>


=={{header|PHP}}==
=={{header|PHP}}==