6 use Test::More no_plan => 4;
10 Scala Style Class Mixin Composition
12 L<http://scala.epfl.ch/intro/mixin.html>
14 A class can only be used as a mixin in the definition of another
15 class, if this other class extends a subclass of the superclass
16 of the mixin. Since ColoredPoint3D extends Point3D and Point3D
17 extends Point2D which is the superclass of ColoredPoint2D, the
18 code above is well-formed.
20 class Point2D(xc: Int, yc: Int) {
23 override def toString() = "x = " + x + ", y = " + y;
26 class ColoredPoint2D(u: Int, v: Int, c: String) extends Point2D(u, v) {
28 def setColor(newCol: String): Unit = color = newCol;
29 override def toString() = super.toString() + ", col = " + color;
32 class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) {
34 override def toString() = super.toString() + ", z = " + z;
37 class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
38 extends Point3D(xc, yc, zc)
39 with ColoredPoint2D(xc, yc, col);
42 Console.println(new ColoredPoint3D(1, 2, 3, "blue").toString())
44 "x = 1, y = 2, z = 3, col = blue"
52 Point2D->meta->add_attribute('$x' => (
57 Point2D->meta->add_attribute('$y' => (
64 $class->meta->new_object(@_);
69 "x = " . $self->x . ", y = " . $self->y;
72 package ColoredPoint2D;
73 our @ISA = ('Point2D');
75 ColoredPoint2D->meta->add_attribute('$color' => (
82 $self->SUPER() . ', col = ' . $self->color;
86 our @ISA = ('Point2D');
88 Point3D->meta->add_attribute('$z' => (
95 $self->SUPER() . ', z = ' . $self->z;
98 package ColoredPoint3D;
99 our @ISA = ('Point3D');
101 __PACKAGE__->meta->mixin('ColoredPoint2D');
105 my $colored_point_3d = ColoredPoint3D->new(x => 1, y => 2, z => 3, color => 'blue');
106 isa_ok($colored_point_3d, 'ColoredPoint3D');
107 isa_ok($colored_point_3d, 'Point3D');
108 isa_ok($colored_point_3d, 'Point2D');
110 is($colored_point_3d->toString(),
111 'x = 1, y = 2, z = 3, col = blue',
112 '... got the right toString method');