zoukankan      html  css  js  c++  java
  • perl 访问类方法的几种方式

    [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 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 "111111111111
    ";
    $code=Horse->sum1(4,5);
    
    print "$str is $code
    ";
    print "222222222222
    ";
    
    $code=Horse->sum2(4,5);
    print "$str is $code
    ";
    
    print "33333333333333
    ";
    print Horse::sum1($ua,1,2);
    print "
    ";
    
    
    
    print "4444444444444444
    ";
    print Critter::sum2($ua,1,2);
    print "
    ";
    
    
    print "55555555555555555
    ";
    print $ua->sum1(4,5);
    print "
    ";
    
    
    print "66666666666666666
    ";
    print $ua->sum2(4,5);
    print "
    ";

  • 相关阅读:
    POJ 3114 Tarjan+Dijkstra
    278. First Bad Version
    209. Minimum Size Subarray Sum
    154. Find Minimum in Rotated Sorted Array II
    153. Find Minimum in Rotated Sorted Array
    710. Random Pick with Blacklist
    767. Reorganize String
    524. Longest Word in Dictionary through Deleting
    349. Intersection of Two Arrays
    350. Intersection of Two Arrays II
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350779.html
Copyright © 2011-2022 走看看