您的位置:首页 > 其它

Perl 5 中的三大OO特性

2016-03-20 23:42 267 查看

Encapsulation

首先,将代码段进行封装,成函数。使用中可以优化,并降低代码冗余度,将先前的函数泛化。再次,总结所处理对象特性,提取重构成类。

code block —–> subroutine —–> generalization —–> rarefaction (Class)

Inheritance

继承是 OO 呈上启后的特性。在Perl 5 中使用 use parent xxx; 声明父类。等同于在 @ISA 中加入父类 XXX。继承父类后: 可以重写方法,调用父类属性。

package File::MP3;
use parent 'File'; ## sets @File::MP3::ISA = ('File');
my $mp3 = File::MP3->new('Andvari.mp3',$data);
$mp3->save();


同样,Perl 5 也支持多继承Multi-inheritance, 采用继承顺序原则是“深度优先”与“从左至右”。这个原则作用与 @ISA 这个全局变量。

Note: The SUPER modifier can only be used for method calls. You can’t use it for regular subroutine calls or class methods:

new ()

constructor, can be any name,like load(), just modern convent.

sub new{
my $class = shift;
return bless {}, $class;
}


That explicit association is created by the built-in bless function, which is typically used within the constructor subroutine of the class. The bless function then takes that reference and associates the hash with the class in $class.

除了 bless 类,还可以 bless 变量与函数。一下是bless 变量,帮助理解 bless 的本质。

bless

use Scalar::Util'blessed';
my $foo = {};
my $bar = $foo;
bless $foo,'Class';
print blessed($bar);# prints "Class"
$bar = "some other value";
print blessed($bar);# prints undef


When we call bless on a variable, we are actually blessing (refer to) the underlying data structure that the variable refers to. We are not blessing the reference itself, nor the variable that contains that reference. That’s why the second call to blessed( $bar ) returns false. At that point $bar is no longer storing a reference to an object.

Polymophism

Polymorphism means “many forms” in Greek. It means a method call can behave differently depending on the type of the object that calls it.

# class Employee
package Employee;
use Person;
use strict;
our @ISA = qw(Person);    # inherits from Person
#constructor
sub new {
my ($class) = @_;
# call the constructor of the parent class, Person.
my $self = $class->SUPER::new();
$self->{_id}    = undef;
$self->{_title} = undef;
bless$self, $class;

return$self;
}

#accessor method for  id
sub id {
my ( $self, $id ) = @_;
$self->{_id} = $idifdefined($id);
return ( $self->{_id} );
}

#accessor method for  title
sub title {
my ( $self, $title ) = @_;
$self->{_title} = $titleifdefined($title);
return ( $self->{_title} );
}

sub print {
my ($self) = @_;
# we will call the print method of the parent class
$self->SUPER::print;
$self->address->print;
}
1;
__END__


## test_polymophism.pl
use Person;
use Employee;

my $employee_obj = Employee->new();
my $person_obj = Person->new();
person_obj->print();    ## call print() defined in Person.pm
employee_obj->print();  ## call print() defined in Employee.pm


Looking at the code, you will notice that we have a new method and a print method. Both the child class and its parent class have the same method defined. We have overridden the parent class’ methods with the ones from the child. When those methods are called on an Employee object, we will get the Employee class’ version of the method. This concept of using the methods of an existing object and modifying them is known as polymorphism.

Note:@ISA is stand for ‘is a’, that means the array contains parents’ package.Each package contains a special array called @ISA .

总结

继承是多态的基础

如何理解 bless {}, $class; ? 理解为 blessed into $class, 等同于被 $class 附体,return出来的reference可以完成 $class的能力。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: