September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
52
Task/Polymorphism/Ceylon/polymorphism.ceylon
Normal file
52
Task/Polymorphism/Ceylon/polymorphism.ceylon
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
import ceylon.language {
|
||||
consolePrint = print
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
|
||||
class Point {
|
||||
|
||||
shared variable Integer x;
|
||||
shared variable Integer y;
|
||||
|
||||
shared new(Integer x = 0, Integer y = 0) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
shared new copy(Point p) {
|
||||
this.x = p.x;
|
||||
this.y = p.y;
|
||||
}
|
||||
|
||||
shared default void print() {
|
||||
consolePrint("[Point ``x`` ``y``]");
|
||||
}
|
||||
}
|
||||
|
||||
class Circle extends Point {
|
||||
|
||||
shared variable Integer r;
|
||||
|
||||
shared new(Integer x = 0, Integer y = 0, Integer r = 0) extends Point(x, y) {
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
shared new copy(Circle c) extends Point.copy(c){
|
||||
this.r = c.r;
|
||||
}
|
||||
|
||||
shared actual void print() {
|
||||
consolePrint("[Circle ``x`` ``y`` ``r``]");
|
||||
}
|
||||
}
|
||||
|
||||
value shapes = [
|
||||
Point(), Point(1), Point(1, 2), Point {y = 3;}, Point.copy(Point(4, 5)),
|
||||
Circle(), Circle(1), Circle(2, 3), Circle(4, 5, 6), Circle {y = 7; r = 8;}, Circle.copy(Circle(9, 10, 11))
|
||||
];
|
||||
|
||||
for(shape in shapes) {
|
||||
shape.print();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
Point: x = 0 y = 0
|
||||
Circle: x = 0 y = 0 r = 0
|
||||
Point: x = 10 y = 15
|
||||
Circle: x = 20 y = 25 r = 5
|
||||
Circle: x = 30 y = 35 r = 10
|
||||
Circle: x = 20 y = 25 r = 35
|
||||
14
Task/Polymorphism/Jq/polymorphism-1.jq
Normal file
14
Task/Polymorphism/Jq/polymorphism-1.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
def Point(x;y): {"type": "Point", "x": x, "y": y};
|
||||
def Point(x): Point(x;0);
|
||||
def Point: Point(0);
|
||||
|
||||
def Circle(x;y;r): {"type": "Circle", "x": x, "y": y, "r": r};
|
||||
def Circle(x;y): Circle(x;y;0);
|
||||
def Circle(x): Circle(x;0);
|
||||
def Circle: Circle(0);
|
||||
|
||||
def print:
|
||||
if .type == "Circle" then "\(.type)(\(.x); \(.y); \(.r))"
|
||||
elif .type == "Point" then "\(.type)(\(.x); \(.y))"
|
||||
else empty
|
||||
end;
|
||||
5
Task/Polymorphism/Jq/polymorphism-2.jq
Normal file
5
Task/Polymorphism/Jq/polymorphism-2.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# keyname should be (or evaluate to) a string
|
||||
def set(keyname; value):
|
||||
if type == "object" and .type and has(keyname) then .[keyname] = value
|
||||
else error("set: invalid type: \(.)")
|
||||
end;
|
||||
1
Task/Polymorphism/Jq/polymorphism-3.jq
Normal file
1
Task/Polymorphism/Jq/polymorphism-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
Circle(0;1;2) | .x = 1 | print
|
||||
56
Task/Polymorphism/Kotlin/polymorphism.kotlin
Normal file
56
Task/Polymorphism/Kotlin/polymorphism.kotlin
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// version 1.1.2
|
||||
|
||||
open class Point(var x: Int, var y: Int) {
|
||||
constructor(): this(0, 0)
|
||||
|
||||
constructor(x: Int) : this(x, 0)
|
||||
|
||||
constructor(p: Point) : this(p.x, p.y)
|
||||
|
||||
open protected fun finalize() = println("Finalizing $this...")
|
||||
|
||||
override fun toString() = "Point at ($x, $y)"
|
||||
|
||||
open fun print() = println(this)
|
||||
}
|
||||
|
||||
class Circle(x: Int, y: Int, var r: Int) : Point(x, y) {
|
||||
constructor(): this(0, 0, 0)
|
||||
|
||||
constructor(x: Int) : this(x, 0, 0)
|
||||
|
||||
constructor(x: Int, r: Int) : this(x, 0, r)
|
||||
|
||||
constructor(c: Circle) : this(c.x, c.y, c.r)
|
||||
|
||||
// for simplicity not calling super.finalize() below though this would normally be done in practice
|
||||
override protected fun finalize() = println("Finalizing $this...")
|
||||
|
||||
override fun toString() = "Circle at center ($x, $y), radius $r"
|
||||
|
||||
override fun print() = println(this)
|
||||
}
|
||||
|
||||
fun createObjects() {
|
||||
val points = listOf(Point(), Point(1), Point(2, 3), Point(Point(3, 4)))
|
||||
for (point in points) point.print()
|
||||
val circles = listOf(Circle(), Circle(1), Circle(2, 3), Circle(4, 5, 6), Circle(Circle(7, 8, 9)))
|
||||
for (circle in circles) circle.print()
|
||||
println()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
createObjects()
|
||||
System.gc() // try and force garbage collection
|
||||
Thread.sleep(2000) // allow time for finalizers to run
|
||||
println()
|
||||
val p = Point(5, 6)
|
||||
p.print()
|
||||
p.y = 7 // change y coordinate
|
||||
p.print()
|
||||
val c = Circle(5, 6, 7)
|
||||
c.print()
|
||||
c.r = 8
|
||||
c.print() // change radius
|
||||
/* note that finalizers for p and c are not called */
|
||||
}
|
||||
29
Task/Polymorphism/OoRexx/polymorphism-1.rexx
Normal file
29
Task/Polymorphism/OoRexx/polymorphism-1.rexx
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
p = .point~new(3,2)
|
||||
c = .circle~new(,2,6)
|
||||
|
||||
p~print
|
||||
c~print
|
||||
|
||||
::class point
|
||||
::method init
|
||||
expose x y
|
||||
use strict arg x = 0, y = 0 -- defaults to 0 for any non-specified coordinates
|
||||
|
||||
::attribute x
|
||||
::attribute y
|
||||
|
||||
::method print
|
||||
expose x y
|
||||
say "A point at location ("||x","y")"
|
||||
|
||||
::class circle subclass point
|
||||
::method init
|
||||
expose radius
|
||||
use strict arg x = 0, y = 0, radius = 0
|
||||
self~init:super(x, y) -- call superclass constructor
|
||||
|
||||
::attribute radius
|
||||
|
||||
::method print
|
||||
expose radius
|
||||
say "A circle of radius" radius "centered at location ("||self~x","self~y")"
|
||||
30
Task/Polymorphism/OoRexx/polymorphism-2.rexx
Normal file
30
Task/Polymorphism/OoRexx/polymorphism-2.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
p = .point~new(3,2)
|
||||
c = .circle~new(,2,6)
|
||||
|
||||
p~print
|
||||
c~print
|
||||
|
||||
::class point
|
||||
::method init
|
||||
expose x y
|
||||
use strict arg x = 0, y = 0 -- defaults to 0 for any non-specified coordinates
|
||||
|
||||
::attribute x
|
||||
::attribute y
|
||||
|
||||
::method print
|
||||
expose x y
|
||||
say "A point at location ("||x","y")"
|
||||
|
||||
::class circle
|
||||
::method init
|
||||
expose x y radius
|
||||
use strict arg x = 0, y = 0, radius = 0
|
||||
|
||||
::attribute radius
|
||||
::attribute x
|
||||
::attribute y
|
||||
|
||||
::method print
|
||||
expose radius x y
|
||||
say "A circle of radius" radius "centered at location ("||x","y")"
|
||||
|
|
@ -15,10 +15,12 @@ function new_circle(object x=0, atom y=0, atom r=0)
|
|||
r = y -- assume r got passed in y
|
||||
return {x,r} -- {point,r}
|
||||
end if
|
||||
return {{x,y},r}
|
||||
return {{x,y},r} -- {point,r}
|
||||
-- (or {new_point(x,y),r} if you prefer)
|
||||
end function
|
||||
|
||||
point p = new_point(4,5)
|
||||
circle c = new_circle(p,6)
|
||||
?p
|
||||
?c
|
||||
circle c1 = new_circle(p,6),
|
||||
c2 = new_circle(4,5,6}
|
||||
?c1
|
||||
?c2
|
||||
|
|
|
|||
27
Task/Polymorphism/Zkl/polymorphism.zkl
Normal file
27
Task/Polymorphism/Zkl/polymorphism.zkl
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
class Point{var x,y;
|
||||
fcn init(xyOrPoint=0,_=0){
|
||||
if(Point.isInstanceOf(xyOrPoint)) set(xyOrPoint);
|
||||
else x,y=vm.arglist.apply("toFloat")}
|
||||
fcn set(p){x=p.x;y=p.y}
|
||||
fcn toString{"(%d,%d)".fmt(x,y)}
|
||||
}
|
||||
class Circle{var center, radius;
|
||||
fcn init(a=0.0,b=0.0,r=1.0){
|
||||
switch [arglist]{
|
||||
case(Circle){ center=Point(a.center); radius=a.radius }
|
||||
case(Point) { center=Point(a); radius=b.toFloat(); }
|
||||
else { center=Point(a,b); radius=r.toFloat(); }
|
||||
}
|
||||
}
|
||||
fcn copy{self(self)}
|
||||
fcn toString{"(%s,%d)".fmt(center.toString(),radius)}
|
||||
}
|
||||
// see if various constructors work
|
||||
Point(); Point(1); Point(1,2), Point(Point());
|
||||
Circle(); Circle(1); Circle(1,2); Circle(1,2,3);
|
||||
Circle(Point()); Circle(Point(),1);
|
||||
Circle(Circle());
|
||||
|
||||
c:=Circle(1,2,3);
|
||||
c.println(); c.center.println();
|
||||
c.copy().println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue