2012-05-08 99 views
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. 
+1

顺便说一句,没有必要在派生类中编写单独的构造函数,除非他们需要做一些特殊的事情。父类的构造函数将被继承。 – friedo

+1

作为一个方面说明,'base'加载了被继承的模块,所以不需要单独加载它。 – Schwern

+0

感谢您的帮助和建议。很有用。 –

回答

4

我没有看到你张贴的内容问题。问题肯定在你没有发布的内容中。你忘了加载AccessList :: Extended :: Boundary吗?

$ find -type f 
./AccessList/Extended/Boundary.pm 
./AccessList/Extended.pm 
./AccessList/Generic.pm 

$ perl -E' 
    use Test::More tests => 1; 
    use AccessList::Extended::Boundary; 
    can_ok("AccessList::Extended::Boundary", "get_acl_information"); 
' 
1..1 
ok 1 - AccessList::Extended::Boundary->can('get_acl_information')