当调用 Parent::call_foo() 时,我希望它始终调用 Parent::foo(),无论 $self 是受祝福的父母还是受祝福的孩子,也不管孩子是否重载了 foo()。
此代码打印:
parent foo
parent call_foo, parent foo
child foo
child call_foo, parent call_foo, child foo
但我希望它打印出来:(最后一行的父 foo)
parent foo
parent call_foo, parent foo
child foo
child call_foo, parent call_foo, parent foo
-
use Parent;
use Child;
my $par = Parent->new();
my $chd = Child->new();
$par->foo();
$par->call_foo();
$chd->foo();
$chd->call_foo();
-
package Parent;
sub new {
my $class = shift;
return bless( {}, $class );
}
sub foo {
print "parent foo\n";
}
sub call_foo {
my $self = shift;
print "parent call_foo, ";
$self->foo();
}
1;
-
package Child;
use base qw(Parent);
sub foo {
print "child foo\n";
}
sub call_foo {
my $self = shift;
print "child call_foo, ";
$self->SUPER::call_foo();
}
1;