您的位置:首页 > 其它

perl 面向对象 new方法

2016-07-14 20:10 274 查看
[root@wx03 test]# cat Scan.pm
package Scan;
sub new{
my $class = shift;
my $self={
'a'=>11,
'b'=>22,
'c'=>33
};
bless $self,$class;
return $self;
};

sub sum_all {

my $self=shift;
my ($c,$d,$e)=@_;
return ($self->{a} + $self->{b} + $self->{c} + $c + $d + $e);
};
1;
[root@wx03 test]# cat t12.pl
unshift(@INC,"/root/test");
use Scan ;
my $ua=Scan->new();
print $ua->sum_all(1,5,8);
[root@wx03 test]# perl t12.pl
80[root@wx03 test]#

/*****************new 方法不定义内容:

[root@wx03 test]# cat Scan.pm
package Scan;
sub new{
my $class = shift;
my $self={};
bless $self,$class;
return $self;
};

sub sum_all {

my $self=shift;
my ($c,$d,$e)=@_;
return ( $c + $d + $e);
};
1;
[root@wx03 test]# cat t12.pl
unshift(@INC,"/root/test");
use Scan ;
my $ua=Scan->new();
print $ua->sum_all(1,5,8);
[root@wx03 test]# perl t12.pl
14[root@wx03 test]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: