zoukankan      html  css  js  c++  java
  • 递归类继承错误

    <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]# 


    
                                        
    
  • 相关阅读:
    一个网站的诞生 MagicDict开发总结8 [页面优化 能省一点是一点]
    一个网站的诞生 MagicDict开发总结6 [划词 检索]
    一个网站的诞生 MagicDict开发总结4 [如果有阶层数据库就完美了]
    一个网站的诞生 MagicDict开发总结9 [日语单词检索策略]
    一个网站的诞生 MagicDict开发总结10 [第一阶段的检索流程]
    一个网站的诞生 MagicDict 网站源码
    一个网站的诞生 MagicDict未来予想図1 [水平分割数据表的构想]
    一个网站的诞生 MagicDict开发总结3 [日语字典数据结构]
    一个网站的诞生 MagicDict开发总结5 [检索候补列表的生成]
    xcode 4 制作静态库
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350786.html
Copyright © 2011-2022 走看看