Polymorphism: Difference between revisions
Content deleted Content added
→{{header|C++}}: includes added, pointers deleted |
Added Wren |
||
Line 4,424:
}
</lang>
=={{header|Wren}}==
Although Wren supports operator overloading, it is not possible to overload the assignment operator '='.
Objects in Wren are garbage collected when there are no longer any references to them. There is no support for destructors as such though, if you want to do some clean-up when an object is no longer needed, you can write a suitable method and then call it manually.
The following program uses the same examples as the Kotlin entry.
<lang ecmascript>class Point {
construct new(x, y) {
_x = x
_y = y
}
static new(x) { new(x, 0) }
static new() { new(0, 0) }
static fromPoint(p) { new(p.x, p.y) }
x { _x }
y { _y }
print() { System.print("Point at (%(_x), %(_y))") }
}
class Circle is Point {
construct new(x, y, r) {
super(x, y)
_r = r
}
static new(x, r) { new(x, 0, r) }
static new(x) { new(x, 0, 0) }
static new() { new(0, 0, 0) }
static fromCircle(c) { new(c.x, c.y, c.r) }
r { _r }
print() { System.print("Circle at center(%(x), %(y)), radius %(_r)") }
}
var points = [Point.new(), Point.new(1), Point.new(2, 3), Point.fromPoint(Point.new(3, 4))]
for (point in points) point.print()
var circles = [
Circle.new(), Circle.new(1), Circle.new(2, 3),
Circle.new(4, 5, 6), Circle.fromCircle(Circle.new(7, 8, 9))
]
for (circle in circles) circle.print()</lang>
{{out}}
<pre>
Point at (0, 0)
Point at (1, 0)
Point at (2, 3)
Point at (3, 4)
Circle at center(0, 0), radius 0
Circle at center(1, 0), radius 0
Circle at center(2, 0), radius 3
Circle at center(4, 5), radius 6
Circle at center(7, 8), radius 9
</pre>
=={{header|zkl}}==
|