• 递归类继承错误


    <pre name="code" class="html">[root@wx03 test]# cat Horse.pm 
    
    Horse 类:
    package Horse;
    #our @ISA = "Critter";
    sub new {
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
    my $self = {
    color => "bay",
    legs => 4,
    owner => undef,
    @_, # 覆盖以前的属性
    };
    return bless $self, $class;
    };
    sub sum_arr {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b + 7;
    };
    1;
    
    
    Critter类:
    
    package Critter;
    sub new {
        my $self = {};
        my $invocant = shift;    
    my $class = ref($invocant) || $invocant;
    	my ($name)=@_;    
          my $self = {    
             "name" =>$name    
                     };  
        bless $self, $class; # Use class name to bless() reference
        return $self;
    
    };
    
    sub sum {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b;
    };
    1;
    
    
    
    此时Horse 类没有继承Critter类:
    
    [root@wx03 test]# cat t6.pl 
    unshift(@INC,"/root/test"); 
    use Horse;;
    #use base qw(Critter);
    require Critter;
    use Data::Dumper;
    $ed = Horse->new; # 四腿湾马
    print $ed->sum_arr(4,5);
    print "
    ";
    print $ed->sum(4,5);
    
    [root@wx03 test]# perl t6.pl 
    16
    Can't locate object method "sum" via package "Horse" at t6.pl line 9.
    
    
    此时无法找到sum方法
    
    
    
    
    当 Perl 搜索一个方法的时候,它确信你没有创建一个闭环的继承级别。如果两个类互相 继承则可能出现这个问
    题,甚至是间接地通过其他类这样继承也如此。
    
    
    模拟递归继承:
    
    
    [root@wx03 test]# cat Horse.pm 
    package Horse;
    our @ISA = "Critter";
    sub new {
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
    my $self = {
    color => "bay",
    legs => 4,
    owner => undef,
    @_, # 覆盖以前的属性
    };
    return bless $self, $class;
    };
    sub sum_arr {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b + 7;
    };
    1;
    [root@wx03 test]# cat Critter.pm 
    package Critter;
    our @ISA = "Horse";
    sub new {
        my $self = {};
        my $invocant = shift;    
    my $class = ref($invocant) || $invocant;
    	my ($name)=@_;    
          my $self = {    
             "name" =>$name    
                     };  
        bless $self, $class; # Use class name to bless() reference
        return $self;
    
    };
    
    sub sum {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b;
    };
    1;
    
    
    此时报错:
    [root@wx03 test]# cat t6.pl 
    unshift(@INC,"/root/test"); 
    use Horse;;
    use base qw(Critter);
    require Critter;
    use Data::Dumper;
    $ed = Horse->new; # 四腿湾马
    print $ed->sum_arr(4,5);
    print "
    ";
    print $ed->sum(4,5);
    [root@wx03 test]# perl t6.pl 
    Recursive inheritance detected in package 'Critter' at Critter.pm line 2.
    Compilation failed in require at /usr/local/perl/lib/5.22.1/base.pm line 99.
    	...propagated at /usr/local/perl/lib/5.22.1/base.pm line 108.
    BEGIN failed--compilation aborted at t6.pl line 3.
    [root@wx03 test]# 
    
    检测到递归继承 
    
    /**************************
    [root@wx03 tmp]# cat Horse.pm 
    package Horse;
    use base qw(Critter);
    
    
    sub new {
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
    my $self = {
    color => "bay",
    legs => 4,
    owner => undef,
    @_, # 覆盖以前的属性
    };
    return bless $self, $class;
    };
    sub sum_arr {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b + 7;
    };
    1;
    
    
    [root@wx03 tmp]# cat Critter.pm 
    package Critter;
    use base qw/Horse/;
    sub new {
        my $self = {};
        my $invocant = shift;    
    my $class = ref($invocant) || $invocant;
    my ($name)=@_;    
          my $self = {    
             "name" =>$name    
                     };  
        bless $self, $class; # Use class name to bless() reference
        return $self;
    
    
    };
    
    
    sub sum {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b;
    };
    1;
    [root@wx03 tmp]# ca t6.pl 
    -bash: ca: command not found
    [root@wx03 tmp]# cat t6.pl 
    unshift(@INC,"/tmp"); 
    use Horse;;
    $ed = Horse->new; # 四腿湾马
    print $ed->sum_arr(4,5);
    print "
    ";
    print $ed->sum(4,5);
    [root@wx03 tmp]# perl t6.pl 
    Recursive inheritance detected in package 'Horse' at /usr/local/perl/lib/5.22.1/base.pm line 137.
    BEGIN failed--compilation aborted at Horse.pm line 2.
    Compilation failed in require at t6.pl line 2.
    BEGIN failed--compilation aborted at t6.pl line 2.
    [root@wx03 tmp]# 


    
       
    
    
  • 相关阅读:
    Silverlight 动态调用 WebService
    一步一步学Silverlight 2系列(28):图片处理
    一步一步学Silverlight 2系列(27):使用Brush进行填充
    HTML5 Web Applications
    分享31个非常有用的 HTML5 教程
    数学分析原理 定理 6.5
    数学分析原理 定理 6.9
    RiemannStieltjes积分存在的充分条件(按照Tom M.Apostol的《数学分析》上的定义)
    数学分析原理 定理 6.4
    数学分析原理 定理 6.8
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6200032.html
Copyright © 2020-2023  润新知