广告位招租
OOP 的 Perl 实现
[
2008-06-12 23:30 | by 草山狐 ]
Perl 并不是严格的面向对象的语言,Jason Stajich 说:"Perl OO is a type of hack, but it works!" 实际上 Perl OOP 对于使用者来说并不复杂,因为只是添加了很少的几个语法。
要实现OOP,有三件事情要做:
1. 要创建一个 class,就建立一个 package。
2. 要创建一个 method, 就写一个subroutine。
3. 要创建一个 object, 就使用 bless 语法把 referent 变成object。
来看一个例子:
package Bug; # 第一步,建立一个 package
use strict;
sub new # 建立 subroutine
{
my ($class) = @_;
my $objref = {};
#其他的一些代码
bless $objref, $class; #用 bless 语法产生了一个 object
}
sub print_me # 又建立了一个subroutine
{
my ($self) = @_;
#实现功能的一些代码
}
注意:在 bless 语句中, $class就是这个类的名字(Bug),由 new 函数赋值,而 $objref 则自动被返回做为 new 函数的返回值。
现在,这样来使用它们:
#! /usr/bin/perl -w
use Bug;
use strict;
my $object = Bug->new(@args); #利用 @args 中的数据创立一个对象,它属于 Bug 这个 class
$object->print_me(); #调用Bug class 中定义的print_me()函数
这个基本的格局就是我们在 BioPerl 的使用当中最常看到的情形。往后看到更多实例的时候,大家会对它更加熟悉。
下面是一个更长一些的例子:
package MyPerson;
use strict;
#use warnings;
use vars qw(@ISA); # 这一句用于 inheritance,也可以省略
sub new {
my ($class, @args) = @_;
my ($first,$last,$id,$father,$mother) = @args;
my $self = bless {
'fname' => $first,
'lname' => $last,
'id' => $id,
'mother' => $mother,
'father' => $father,
}, $class;
return $self;
}
sub father {
my ($self,$val) = @_;
return $self->{'father'};
}
sub id { return shift->{'id'} }
1;
如何使用它呢?看这段代码:
#!/usr/bin/perl -w
use strict;
use MyPerson;
my $person = MyPerson->new('jimbo', 'gumbo', 1,2,3);
my @family = (undef,$person); # 0 index needs to be empty
push @family, MyPerson->new('mom', 'gumbo', 3, 0, 0);
push @family, MyPerson->new('dad', 'gumbo', 2, 0, 0);
my $dad = $family[$person->father];
print "father id is ", $person->father, "\n";
print "father obj id is $dad id is ", $dad->id,"\n";
要实现OOP,有三件事情要做:
1. 要创建一个 class,就建立一个 package。
2. 要创建一个 method, 就写一个subroutine。
3. 要创建一个 object, 就使用 bless 语法把 referent 变成object。
来看一个例子:
package Bug; # 第一步,建立一个 package
use strict;
sub new # 建立 subroutine
{
my ($class) = @_;
my $objref = {};
#其他的一些代码
bless $objref, $class; #用 bless 语法产生了一个 object
}
sub print_me # 又建立了一个subroutine
{
my ($self) = @_;
#实现功能的一些代码
}
注意:在 bless 语句中, $class就是这个类的名字(Bug),由 new 函数赋值,而 $objref 则自动被返回做为 new 函数的返回值。
现在,这样来使用它们:
#! /usr/bin/perl -w
use Bug;
use strict;
my $object = Bug->new(@args); #利用 @args 中的数据创立一个对象,它属于 Bug 这个 class
$object->print_me(); #调用Bug class 中定义的print_me()函数
这个基本的格局就是我们在 BioPerl 的使用当中最常看到的情形。往后看到更多实例的时候,大家会对它更加熟悉。
下面是一个更长一些的例子:
package MyPerson;
use strict;
#use warnings;
use vars qw(@ISA); # 这一句用于 inheritance,也可以省略
sub new {
my ($class, @args) = @_;
my ($first,$last,$id,$father,$mother) = @args;
my $self = bless {
'fname' => $first,
'lname' => $last,
'id' => $id,
'mother' => $mother,
'father' => $father,
}, $class;
return $self;
}
sub father {
my ($self,$val) = @_;
return $self->{'father'};
}
sub id { return shift->{'id'} }
1;
如何使用它呢?看这段代码:
#!/usr/bin/perl -w
use strict;
use MyPerson;
my $person = MyPerson->new('jimbo', 'gumbo', 1,2,3);
my @family = (undef,$person); # 0 index needs to be empty
push @family, MyPerson->new('mom', 'gumbo', 3, 0, 0);
push @family, MyPerson->new('dad', 'gumbo', 2, 0, 0);
my $dad = $family[$person->father];
print "father id is ", $person->father, "\n";
print "father obj id is $dad id is ", $dad->id,"\n";
java中分别读取年月日的方法
Flex Builder 3.0 注册码





