/* Please try the NoviceExamples before the MultiMethodExamples
To compile:
nicec --sourcepath=.. -a binarymethod.jar binarymethod
To run:
java -jar binarymethod.jar
*/
equals(#Point p1, #Point p2) =
(p1.x==p2.x) && (p1.y==p2.y);
equals(#ColorPoint p1, #ColorPoint p2) =
(p1.x==p2.x) && (p1.y==p2.y) && (p1.c==p2.c);
class Point {
double _x = 0.0;
double _y = 0.0;
double x() = _x;
double y() = _y;
double dist() = Math.sqrt(_x**2 + _y**2);
alike closer(alike p){
if (p.dist < this.dist)
return p;
else
return this;
}
}
class ColorPoint extends Point {
double _c = 0.0;
double c() = _c;
}
void main(String[] args){
var s = " ";
let p = new Point(_x: 3.2, _y: 4.5);
let q = new Point(_x: 3.2, _y: 4.5);
let cp = new ColorPoint(_x: 3.2, _y: 4.5);
let cpq = new ColorPoint(_x: 3.2, _y: 4.5);
if (p.equals(q)) s = "equal"; else s = "NOT equal ";
println("Point Point " + s);
if (p.equals(cp)) s = "equal"; else s = "NOT equal ";
println("Point ColorPoint " + s);
if (cp.equals(p)) s = "equal"; else s = "NOT equal ";
println("ColorPoint Point " + s);
if (cp.equals(cpq)) s = "equal"; else s = "NOT equal ";
println("ColorPoint ColorPoint " + s);
if (p.equals( p.closer(q) )) s = "equal"; else s = "NOT equal ";
println("\nPoint closer Point " + s);
if (cp.equals( cp.closer(cpq) )) s = "equal"; else s = "NOT equal ";
println("ColorPoint closer ColorPoint " + s);
}
/* Notes - language
See the detailed discussion of binary methods "On Binary Methods"
http://citeseer.nj.nec.com/bruce95binary.html
equals(#Point p1, #Point p2)
Implement "equals" for the case where both objects are instances
of Point (excluding subclasses of Point).
In a single dispatch language, like Java, we would have to explicitly
test that both objects were instances of Point, like this:
boolean equals(Object o){
if (o instanceof Point)
return (_x==p2.x) && (_y==p2.y);
else
return false;
}
alike closer(alike p){
See the discussion of selftype in "On the Interaction of Object
Design Patterns and Programming Languages" p9
http://citeseer.nj.nec.com/baumgartner96interaction.html
*/
-- IsaacGouy - 02 Feb 2004
| Topic BinaryMethodsMultiMethodExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.7 | > | r1.6 | > | r1.5 | More } |
|
Revision r1.7 - 28 Apr 2005 - 11:41 GMT - TWikiGuest Parents: WebHome > CodeExamples |
Copyright © 1999-2003 by the contributing authors.
All material on this collaboration platform is the property of the contributing authors. Ideas, requests, problems regarding TWiki? Send feedback. |