Polymorphism: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 568: | Line 568: | ||
=={{header|C}}== |
=={{header|C}}== |
||
* See [[Polymorphism/C]] |
* See [[Polymorphism/C]] |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
class Point |
|||
{ |
|||
protected int x, y; |
|||
public Point() : this(0) {} |
|||
public Point(int x) : this(x,0) {} |
|||
public Point(int x, int y) { this.x = x; this.y = y; } |
|||
public int X { get { return x; } set { x = value; } } |
|||
public int Y { get { return y; } set { y = value; } } |
|||
public virtual void print() { System.Console.WriteLine("Point"); } |
|||
} |
|||
public class Circle : Point |
|||
{ |
|||
private int r; |
|||
public Circle(Point p) : this(p,0) { } |
|||
public Circle(Point p, int r) : base(p) { this.r = r; } |
|||
public Circle() : this(0) { } |
|||
public Circle(int x) : this(x,0) { } |
|||
public Circle(int x, int y) : this(x,y,0) { } |
|||
public Circle(int x, int y, int r) : base(x,y) { this.r = r; } |
|||
public int R { get { return r; } set { r = value; } } |
|||
public override void print() { System.Console.WriteLine("Circle"); } |
|||
public static void main(String args[]) |
|||
{ |
|||
Point p = new Point(); |
|||
Point c = new Circle(); |
|||
p.print(); |
|||
c.print(); |
|||
} |
|||
}</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 694: | Line 728: | ||
c->print(); |
c->print(); |
||
return 0; |
return 0; |
||
}</lang> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
class Point |
|||
{ |
|||
protected int x, y; |
|||
public Point() : this(0) {} |
|||
public Point(int x) : this(x,0) {} |
|||
public Point(int x, int y) { this.x = x; this.y = y; } |
|||
public int X { get { return x; } set { x = value; } } |
|||
public int Y { get { return y; } set { y = value; } } |
|||
public virtual void print() { System.Console.WriteLine("Point"); } |
|||
} |
|||
public class Circle : Point |
|||
{ |
|||
private int r; |
|||
public Circle(Point p) : this(p,0) { } |
|||
public Circle(Point p, int r) : base(p) { this.r = r; } |
|||
public Circle() : this(0) { } |
|||
public Circle(int x) : this(x,0) { } |
|||
public Circle(int x, int y) : this(x,y,0) { } |
|||
public Circle(int x, int y, int r) : base(x,y) { this.r = r; } |
|||
public int R { get { return r; } set { r = value; } } |
|||
public override void print() { System.Console.WriteLine("Circle"); } |
|||
public static void main(String args[]) |
|||
{ |
|||
Point p = new Point(); |
|||
Point c = new Circle(); |
|||
p.print(); |
|||
c.print(); |
|||
} |
|||
}</lang> |
}</lang> |
||
Line 1,332: | Line 1,332: | ||
<lang ela>c = circleX 12 |
<lang ela>c = circleX 12 |
||
c.x //Evaluates to 12</lang> |
c.x //Evaluates to 12</lang> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
Line 1,385: | Line 1,386: | ||
Circle |
Circle |
||
</pre> |
</pre> |
||
=={{header|F Sharp|F#}}== |
|||
Polymorphism is achieved by defining an interface <code>Printable</code> which is implemented by <code>Point</code> and <code>Circle</code>. (In real code, you should override the <code>ToString</code> method which every class inherits from <code>Object</code>.) |
|||
Due to the use of optional parameters, we only need one constructor for every class. No accessors are necessary because we use public read-only properties. (Mutable properties are possible, too, but should be avoided in idiomatic code.) |
|||
<lang fsharp>type Printable = |
|||
abstract member Print : unit -> unit |
|||
type Point(?x, ?y) = |
|||
member t.x = defaultArg x 0.0 |
|||
member t.y = defaultArg y 0.0 |
|||
interface Printable with |
|||
member t.Print() = printfn "Point(x:%f, y:%f)" t.x t.y |
|||
type Circle(?center, ?radius) = |
|||
member t.center = defaultArg center (new Point()) |
|||
member t.radius = defaultArg radius 1.0 |
|||
interface Printable with |
|||
member t.Print() = |
|||
printfn "Circle(x:%f, y:%f, r:%f)" t.center.x t.center.y t.radius</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 1,685: | Line 1,707: | ||
</lang> |
</lang> |
||
=={{header|F Sharp|F#}}== |
|||
Polymorphism is achieved by defining an interface <code>Printable</code> which is implemented by <code>Point</code> and <code>Circle</code>. (In real code, you should override the <code>ToString</code> method which every class inherits from <code>Object</code>.) |
|||
Due to the use of optional parameters, we only need one constructor for every class. No accessors are necessary because we use public read-only properties. (Mutable properties are possible, too, but should be avoided in idiomatic code.) |
|||
<lang fsharp>type Printable = |
|||
abstract member Print : unit -> unit |
|||
type Point(?x, ?y) = |
|||
member t.x = defaultArg x 0.0 |
|||
member t.y = defaultArg y 0.0 |
|||
interface Printable with |
|||
member t.Print() = printfn "Point(x:%f, y:%f)" t.x t.y |
|||
type Circle(?center, ?radius) = |
|||
member t.center = defaultArg center (new Point()) |
|||
member t.radius = defaultArg radius 1.0 |
|||
interface Printable with |
|||
member t.Print() = |
|||
printfn "Circle(x:%f, y:%f, r:%f)" t.center.x t.center.y t.radius</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
<lang go>package main |
<lang go>package main |
||
Line 2,350: | Line 2,352: | ||
return Circle:new{x = self.x, y = self.y, r = self.r} |
return Circle:new{x = self.x, y = self.y, r = self.r} |
||
end</lang> |
end</lang> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Line 3,024: | Line 3,025: | ||
A circle of radius 6 centered at location (0,2) |
A circle of radius 6 centered at location (0,2) |
||
</pre> |
</pre> |
||
=={{header|OxygenBasic}}== |
=={{header|OxygenBasic}}== |
||
Line 3,225: | Line 3,225: | ||
print $c->r, "\n"; # accessor autogenerated |
print $c->r, "\n"; # accessor autogenerated |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
{{Works with|rakudo|2015-11-30}} |
|||
All appropriate constructors, initializers, accessors, and destructors are provided by default, but may be explicitly declared for flexibility. |
|||
To create only readonly accessors for better encapsulation, leave out all the "is rw" traits. |
|||
Here we demonstrate that accessors can behave like variables and may be assigned. |
|||
<lang perl6>class Point { |
|||
has Real $.x is rw = 0; |
|||
has Real $.y is rw = 0; |
|||
method Str { $.perl } |
|||
} |
|||
class Circle { |
|||
has Point $.p is rw = Point.new; |
|||
has Real $.r is rw = 0; |
|||
method Str { $.perl } |
|||
} |
|||
my $c = Circle.new(p => Point.new(x => 1, y => 2), r => 3); |
|||
say $c; |
|||
$c.p.x = (-10..10).pick; |
|||
$c.p.y = (-10..10).pick; |
|||
$c.r = (0..10).pick; |
|||
say $c;</lang> |
|||
In this case we define the Str coercion method polymorphically, which is used by say or print to format the contents of the object. |
|||
We could also have defined print methods directly. |
|||
We could have factored this method out to a common role and composed it into each class. |
|||
We could also have defined multi subs outside of the class, like this: |
|||
<lang perl6>multi print (Point $p) { $p.perl.print } |
|||
multi print (Circle $c) { $c.perl.print }</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 3,884: | Line 3,854: | ||
"<circle% <point% 3 0> 5>" |
"<circle% <point% 3 0> 5>" |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{Works with|rakudo|2015-11-30}} |
|||
All appropriate constructors, initializers, accessors, and destructors are provided by default, but may be explicitly declared for flexibility. |
|||
To create only readonly accessors for better encapsulation, leave out all the "is rw" traits. |
|||
Here we demonstrate that accessors can behave like variables and may be assigned. |
|||
<lang perl6>class Point { |
|||
has Real $.x is rw = 0; |
|||
has Real $.y is rw = 0; |
|||
method Str { $.perl } |
|||
} |
|||
class Circle { |
|||
has Point $.p is rw = Point.new; |
|||
has Real $.r is rw = 0; |
|||
method Str { $.perl } |
|||
} |
|||
my $c = Circle.new(p => Point.new(x => 1, y => 2), r => 3); |
|||
say $c; |
|||
$c.p.x = (-10..10).pick; |
|||
$c.p.y = (-10..10).pick; |
|||
$c.r = (0..10).pick; |
|||
say $c;</lang> |
|||
In this case we define the Str coercion method polymorphically, which is used by say or print to format the contents of the object. |
|||
We could also have defined print methods directly. |
|||
We could have factored this method out to a common role and composed it into each class. |
|||
We could also have defined multi subs outside of the class, like this: |
|||
<lang perl6>multi print (Point $p) { $p.perl.print } |
|||
multi print (Circle $c) { $c.perl.print }</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 3,968: | Line 3,969: | ||
}</lang> |
}</lang> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
[http://seed7.sourceforge.net/manual/objects.htm Seed7 object orientation] works via interfaces. |
[http://seed7.sourceforge.net/manual/objects.htm Seed7 object orientation] works via interfaces. |