• perl use base 代替 @ISA


    packge Mule;
    use base ("Horse", "donkey"); # 声明一个超类
    它是下面东西的缩写:
    package Mule;
    BEGIN {
    our @ISA = ("Horse", "Donkey");
    require Horse;
    require Donkey;
    }
    
    
    Horse 类 :
    
    [root@wx03 test]# cat Horse.pm 
    package Horse;
    BEGIN {
    our @ISA = "Critter";
    require Critter;
    };
    sub new {
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
    my $self = {
    color => "bay",
    legs => 4,
    owner => undef,
    @_, # 覆盖以前的属性
    };
    return bless $self, $class;
    #return  $self;
    };
    sub sum1 {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b + 7;
    };
    
    our @arr=qw/1 2 3 4 5 6 7/;
    our %h1=(1,2,3,4,5,6,7,8);
    1;
    
    
    Critter类:
    
    [root@wx03 test]# 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 sum2 {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b;
    };
    
    
    sub fun1 {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a / $b;
    }
    1;
    
    
    [root@wx03 test]# cat t10.pl 
    unshift(@INC,"/root/test"); 
    use Horse;;
    $ua=Horse->new();
    #print "It's an object
    " if UNIVERSAL::isa($ua, 'UNIVERSAL');
    $code=Horse->sum1(4,5);
    
    print "$str is $code
    ";
    
    $code=Horse->sum2(4,5);
    print "$str is $code
    ";
    [root@wx03 test]# perl t10.pl 
    $str is 16
    $str is 9
    
    
    
    使用use base 代替:
    
    
    
    [root@wx03 test]# 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;
    #return  $self;
    };
    sub sum1 {
           $self=shift;
           my $a=shift;
           my $b=shift;
           return $a + $b + 7;
    };
    
    our @arr=qw/1 2 3 4 5 6 7/;
    our %h1=(1,2,3,4,5,6,7,8);
    1;
    
    
    [root@wx03 test]# cat t10.pl 
    unshift(@INC,"/root/test"); 
    use Horse;;
    $ua=Horse->new();
    #print "It's an object
    " if UNIVERSAL::isa($ua, 'UNIVERSAL');
    $code=Horse->sum1(4,5);
    
    print "$str is $code
    ";
    
    $code=Horse->sum2(4,5);
    print "$str is $code
    ";
    [root@wx03 test]# perl t10.pl 
    $str is 16
    $str is 9
    
    
    

  • 相关阅读:
    新年第一个工作日,淦一个扫码登录
    eltree 从叶子节点入手解决选中问题
    elcascader 级联选择器 :回显回显回显
    VueX Module:路过你,忘记你
    我变成了曾经最讨厌的模样:路由屎山有感,Next 你是风暴也是风暴眼
    取小数点后两位:打造一个自己的toFixed
    dayjs: 兄弟moment.js寄了,我还在
    前端鄙视题
    android WebView的配置
    springboot自定义通用枚举校验注解
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350780.html
Copyright © 2020-2023  润新知