继承和子方法

时间:2012-05-08 18:13:40

标签: perl

由于某种原因,我无法访问边界对象上的子方法。我会尽可能多地了解答案,因为我仍然对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 个答案:

答案 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')