Has-a relationship methods are generally not directly usable by callers of the delegating class. calling scratch directly on cat doesn't indicate which paw should scratch. Hence, the delegating class has-a method that uses it parameter(s) to determine which instance of paw should do the scratching.
#! perl -slw use strict; package paw; sub new { my $class = shift; return bless { @_ }, $class; } sub scratch { my $self = shift; print "Scratching with $self->{ which } paw"; } package cat; sub new { my $class = shift; my %self = @_; $self{ paws } = { map{ $_ => paw->new( which => $_ ) } 'left front', 'right front', 'left hind', 'right hind' }; return bless \%self, $class; } sub scratchesWith{ my( $self, $paw ) = @_; $self->{ paws }{$paw}->scratch(); } sub speaks { my $self = shift; print $self->{name}, ' says: ', $self->{voice}; } 1; package main; my $cat = cat->new( name => 'Tiddles', voice=> 'Meow!', ); $cat->scratchesWith( 'right front' ); $cat->speaks(); __END__ [ 4:08:26.92] P:\test>405381 Scratching with right front paw Tiddles says: Meow!
In reply to Re^5: Help me understand inheritance please!
by BrowserUk
in thread Help me understand inheritance please!
by jdtoronto
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |