1
出於某種原因,我無法訪問邊界對象上的子方法。我會盡可能詳細地回答一個答案,因爲我仍然對使用perl繼承,特別是保佑的部分感到困惑。任何建設性的批評對於整體設計都會很棒。繼承和子方法
Generic.pm(基類)
package AccessList::Generic;
use strict;
use warnings;
sub new {
my $class = shift;
my $self = {
rules => [],
@_
};
bless $self, $class;
return $self;
}
sub get_line_count {
my $self = shift;
return scalar @{$self->{rules}};
}
1;
Extended.pm
package AccessList::Extended;
use strict;
use warnings;
use AccessList::Generic;
use base qw(AccessList::Generic);
sub new {
my ($class, @args) = @_;
my $self = $class->SUPER::new(@args);
return $self;
}
1;
Boundary.pm
package AccessList::Extended::Boundary;
use strict;
use warnings;
use AccessList::Extended;
use base qw(AccessList::Extended);
sub new {
my ($class, @args) = @_;
my $self = $class->SUPER::new(@args);
return $self;
}
sub get_acl_information {
my ($self) = @_;
return;
}
1;
失敗測試
can_ok('AccessList::Extended::Boundary', 'get_acl_information');
錯誤消息
# Failed test 'AccessList::Extended::Boundary->can('get_acl_information')'
# at t/b1.t line 42.
# AccessList::Extended::Boundary->can('get_acl_information') failed
# Looks like you failed 1 test of 2.
順便說一句,沒有必要在派生類中編寫單獨的構造函數,除非他們需要做一些特殊的事情。父類的構造函數將被繼承。 – friedo
作爲一個方面說明,'base'加載了被繼承的模塊,所以不需要單獨加載它。 – Schwern
感謝您的幫助和建議。很有用。 –