perl use base 继承

centos6.5:/root/podinns/lib#cat First.pm 
package First;
use base qw(Second);
sub new {  
    my $self = {};  
    my $invocant = shift;      
my $class = ref($invocant) || $invocant;  
    my ($name)=@_;      
      my $self = {      
         "name" =>"luotuo",
         @_ # 覆盖以前的属性      
                 };    
    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;
centos6.5:/root/podinns/lib#cat Second.pm 
package Second;
sub sum_all {  
       $self=shift;  
       my $a=shift;  
       my $b=shift;  
       return $a * $b;  
};  


sub sum_x {


       my $a=shift;

       my $b=shift;

       return $a * $b;

};
1; 
centos6.5:/root/podinns/lib#cat a2.pl 
unshift(@INC,"/root/podinns/lib");  
use First;
$ed=First->new('name'=>'poddinns'); 
print "111111111111111\n";
print $ed->sum(12,34);
print "\n";
print "2222222222222222\n";
print $ed->sum_all(12,34);
print "\n";
print "333333333333333333\n";
print Second::sum_x(12,21);
print "\n";
centos6.5:/root/podinns/lib#perl a2.pl 
111111111111111
46
2222222222222222
408
333333333333333333
252



   
posted @ 2016-12-02 12:48  czcb  阅读(130)  评论(0编辑  收藏  举报