Perl多重继承同名方法调用难题:如何正确调用目标方法?
Perl多重继承下的构造逻辑集成问题
尽管Perl官方不推荐使用多重继承,但实际开发中仍会遇到需要使用该特性的场景。现有代码结构如下:
package A; sub f { ... } sub _init { ...; $self->f; ... } sub new { ... $self->_init ... } package B; sub _init { ... } sub new { ... $self->_init ... } package C; sub f { ... } our @ISA = qw(A B); sub new { my $class = $_[0]; my $self = ...; bless $self, $class; ??? call A:_init using $self, call B:_init using $self, setup C's attributes }
C类的new方法面临两大核心问题:
- 多父类构造逻辑集成:
_init负责对象分配与bless之外的初始化操作,但使用$self->SUPER::_init仅能调用其中一个父类的方法,且无法明确指定调用对象 - 动态绑定有效性:调用A::_init时,其中的
f方法需要触发C::f而非A::f;同时C类自身也可能定义_init方法,A、B类也可能存在多层继承关系
以下是可编译的示例代码(已修正原代码笔误):
#!/usr/bin/perl require 5.018_000; use strict; use warnings; package P1; sub f($) { $_[0]->[0] = 1; } sub _init($) { $_[0]->f(); } sub new($) { my $class = $_[0]; my $self = []; $#$self = 0; bless $self, $class; $self->_init(); return $self; } package P2; sub _init($) { $_[0]->[0] = 2; } sub new($) { my $class = $_[0]; my $self = []; $#$self = 0; bless $self, $class; $self->_init(); # 修正原代码笔误:_self改为$self return $self; } package P3; our @ISA = qw(P1 P2); BEGIN { use Exporter qw(import); } sub f($) { $_[0]->[1] = 3; } sub _init($) { $_[0]->[2] = 4; } sub new($) { my $class = $_[0]; my $self = []; $#$self = 2; bless $self, $class; #$self->P1::_init(); #$self->P2::_init(); $self->_init(); return $self; } package min; my $o = P3->new(); $DB::single = 1; print $o, "\n"; # 避免$o被过早优化掉
其中P1::f会将$self->[0]设为1,而P3::f会将$self->[1]设为3。因此当P3对象调用P1的_init时,应该触发P3::f而非P1::f,预期调试结果如下:
DB<1> x $o 0 P3=ARRAY(0xbd27e8) 0 2 1 3 2 4
内容的提问来源于stack exchange,提问作者U. Windl
相关产品推荐
相关产品推荐

