第十二章 对象(下)

第十二章 对象(下)

12.5.1 通过 @ISA 继承

package Mule;
our @ISA= ("Horse", "Donkey");

Perl 将首先在Horse 里(和他的任何前辈类里,比如Critter)查找任何在Mule里找不到的方法,


找不到以后才继续在Donkey 和其父类里进行查找。


如果缺失的方法在一个基类里发现,Perl内部把该位置缓存到当前类里,依次提高效率,


当 Perl 搜索一个方法的时候,它确信你没有创建一个闭环的继承级别。如果两个类互相 继承则可能出现这个问
题

Vsftp:/root/perl/9# perl a1.pl 
Recursive inheritance detected in package 'Horse' at Critter.pm line 2.
Compilation failed in require at a1.pl line 3.
BEGIN failed--compilation aborted at a1.pl line 3.

使用use base 声明父类:

Vsftp:/root/perl/9# cat Horse.pm 
package Horse;
use base ("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;
Vsftp:/root/perl/9# cat Critter.pm 
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;
Vsftp:/root/perl/9# cat a1.pl 
unshift(@INC,"/root/perl/9"); 
use Horse;;
use Critter;
use Data::Dumper;
$ed = Horse->new; # 四腿湾马
print $ed->sum_arr(4,5);
print "
";
print $ed->sum(4,5);
print "
";
Vsftp:/root/perl/9# perl a1.pl 
16
9




12.5.2  访问被覆盖的方法:

基类就是父类:

Vsftp:/root/perl/9# cat Horse.pm 
package Horse;
use base ("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 {
       $self=shift;
       my $a=shift;
       my $b=shift;
       return $a + $b + 7;
};
1;
Vsftp:/root/perl/9# cat Critter.pm 
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;
Vsftp:/root/perl/9# cat a1.pl 
unshift(@INC,"/root/perl/9"); 
use Horse;;
use Critter;
use Data::Dumper;
$ed = Horse->new; # 四腿湾马
#print $ed->sum_arr(4,5);
print "
";
print $ed->sum(4,5);
print "
";
Vsftp:/root/perl/9# perl a1.pl 

16

此时Horse 类覆盖了基类Critter的 sum方法


Vsftp:/root/perl/9# cat a1.pl 
unshift(@INC,"/root/perl/9"); 
use Horse;;
use Critter;
use Data::Dumper;
$ed = Horse->new; # 四腿湾马
print $ed->Critter::sum(4,5);
print "
";
print $ed->sum(4,5);
print "
";
Vsftp:/root/perl/9# perl a1.pl 
9
16


调用父类Critter::sum sum的方法

Vsftp:/root/perl/9# cat a1.pl 
unshift(@INC,"/root/perl/9"); 
use Horse;;
use Critter;
use base qw(Critter);
use Data::Dumper;
$ed = Horse->new; # 四腿湾马
print $ed->SUPER::sum(4,5);
print "
";
print $ed->sum(4,5);
print "
";
Vsftp:/root/perl/9# perl a1.pl 
9
16



12.5.3  UNIVERSAL:最终的祖先类


12.5.4 方法自动装载


12.5.5 私有方法

原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6198957.html