All "BUILD" methods in the inheritance chain will be called, so subclasses need not know about parents:
class Dot {
has $.x;
has $.y;
has $!z;
submethod BUILD(:$!x, :$!y, :$!z) {
say "Initializing!";
}
method get {
return ($!x, $!y, $!z);
}
method getz {
return $!z;
}
}
class Dot2 is Dot {
has $!w;
submethod BUILD(:$!w) {
say "Initializing 2!";
}
method get {
# No access to $!z here of course (it is private)
# Also note use of public $.x, $.y not private $!x, $!y
return ($.x, $.y, $!w);
}
}
my $a = Dot2.new(x => 23, y => 42, z => 2, w => 12);
say $a.get;
say $a.getz;