没事干翻译了cookbook的13.1
没事干翻译了cookbook的13.1
愿意审校的帮忙审一下。
处方 13.1 Constructing an Object 构造物件
13.1.1 问题
You want to create a way for your users to generate new objects.
你希望创造一种生成对象的方法给你的用户.
13.1.2 解决方案
Make a constructor.
创建一个构造器.
In Perl, the constructor method must not only initialize its object,
在Perl中,构造器方法不会只用于初始化对象,
but must also first allocate memory for it,
但首先也必须为对象分配空间,
typically using an anonymous hash.
例如我们经常使用一个匿名散列.
C++ constructors, on the other hand,
C++构造器,在另一个方面,
are called with memory already allocated.
在被调用时,内存里已经分配了一个对象的空间,
Some of the object-oriented world would call C++'s constructors initializers.
一些面向对象的世界倾向于把它称为C++的构造器初始化
Here's the canonical object constructor in Perl:
这里有在Perl中规范的对象构造器:
sub new {
my $class = shift;
my $self = { };
bless($self, $class);
return $self;
}
This is the equivalent one-liner:
这里有一个一句话等价物:
sub new { bless( { }, shift ) }
13.1.3 讨论
Any method that allocates and initializes a new object acts as a constructor.
一些方法在扮演构造器的角色来分配及初始化一个新的对象.
The most important thing to remember is that a reference isn't an object until bless has been called on it.
最重要的事情就是要记得一个引用并不是一个对象,直到bless被它调用后.
The simplest possible constructor, although not particularly useful, is the following:
最简单有效的构造器,虽然可用性并不显著.类似于下面的:
sub new { bless({ }) }
Let's add some initialization:
让我们加点初始化代码:
sub new {
my $self = { }; # allocate anonymous hash 分配一个匿名数组
bless($self);
# init two sample attributes/data members/fields
# 插入两个属性/数据类型/fields
$self->{START} = time( );
$self->{AGE} = 0;
return $self;
}
This constructor isn't very useful,
这个构造器的可用性并不是很强,
because it uses the single-argument form of bless,
因为它只提供给bless一个参数,
which always blesses the object into the current package.
于是它只能经常把对象bless到当前的包中.
This means it can't be usefully inherited from;
这意味着它不能有效的通过继承得到.
objects it constructs will always be blessed into the class that the new function was compiled into.
对象自己构造将总是被bless进入到一个类中,以致于新函数一起被编译进去.
With inheritance, this is not necessarily the class on whose behalf the method was invoked.
对于父类中的方法(遗产),我们的类没必要为了利益而调用方法.
To solve this, have the constructor heed its first argument.
为了解决这些,构造器必须留意第一个参数.
For a class method, this is the package name. Pass this class name as the second argument to bless:
作为一个类方法,这里是一个包名.接受这个类名作为作为bless的第二个参数:
sub new {
my $classname = shift; # What class are we constructing?
my $self = { }; # Allocate new memory
bless($self, $classname); # Mark it of the right type
$self->{START} = time( ); # init data fields
$self->{AGE} = 0;
return $self; # And give it back
}
Now the constructor can be correctly inherited by a derived class.
现在构造器可以正确的通过继承而得到一个子类.
You might also want to separate the memory allocation and blessing step from the instance data initialization step.
你也许希望分离实例数据初始化步骤里面的内存分配和bless.
Simple classes won't need this,
简单的对象不会需要这些,
but it can sometimes make inheritance easier by separating memory allocation from initialization; see Recipe 13.11.
但是有时需要分离初始化里面的内存分配使父类方法(遗产)变的更简单;看处方13.11
sub new {
my $classname = shift; # What class are we constructing?
my $self = { }; # Allocate new memory
bless($self, $classname); # Mark it of the right type
$self->_init(@_); # Call _init with remaining args
return $self;
}
# "private" method to initialize fields. It always sets START to
# the current time, and AGE to 0. If invoked with arguments, _init
# interprets them as key+value pairs to initialize the object with.
sub _init {
my $self = shift;
$self->{START} = time( );
$self->{AGE} = 0;
if (@_) {
my %extra = @_;
@$self{keys %extra} = values %extra;
}
}
13.1.4 其他资源
perltoot(1), perlboot(1), and perlobj(1); Programming Perl的第12章; 处方 13.6; 处方 13.10; 处方 13.11