递归类继承错误

<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 "\n";
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 "\n";
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 "\n";
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]# 


 

转载于:https://www.cnblogs.com/zhaoyangjian724/p/6200033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值