Perl面向对象编程全解析
1. 构造函数的实现
在Perl中,构造函数是创建对象的关键。以下是一个基本构造函数的示例:
use strict;
sub new {
my $class = shift;
my $self = {@_};
bless $self, $class;
return $self;
}
1;
当调用构造函数时,Perl会自动将类名作为第一个参数传入。例如:
@_ = (
"Person3",
"lastname", "Galilei",
"firstname", "Galileo",
"address", "9.81 Pisa Apts.",
"occupation", "bombardier"
);
构造函数的第一行通过 shift 获取类名:
my $class = shift;
此时,参数数组 @_ 中剩下的内容为:
@_ = (
"lastname", "Galilei",
"firstname", "Galileo",
"address", "9.81 Pisa Apts.",
"occupation",
超级会员免费看
订阅专栏 解锁全文
704

被折叠的 条评论
为什么被折叠?



