Perl 5 中的三大OO特性
来源:互联网 发布:淘宝导购网站有哪些 编辑:程序博客网 时间:2024/04/30 02:46
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 Employeepackage Employee;use Person;use strict;our @ISA = qw(Person); # inherits from Person#constructorsub 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 idsub id { my ( $self, $id ) = @_; $self->{_id} = $idifdefined($id); return ( $self->{_id} );}#accessor method for titlesub 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.pluse Person;use Employee;my $employee_obj = Employee->new();my $person_obj = Person->new();person_obj->print(); ## call print() defined in Person.pmemployee_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
object,$class;∗∗?whenyouinvokeamethodon object, Perl know which package to search for the method. If the second argument is omitted, as in your example, the current package/class is used.
- Perl 5 中的三大OO特性
- JAVA中的三大特性
- OO技术三大特点
- Java技术中的三大特性
- OC中的面向对象三大特性之三
- OO的五大原则中的 LSP
- 黑马程序员 5 oc三大特性
- OC-5-三大特性:继承
- java 三大特性
- Java三大特性
- Java三大特性
- OC---三大特性
- Java三大特性
- java三大特性
- Java三大特性
- OC三大特性
- OC三大特性
- Java 三大特性
- android多任务同时下载的简单实现
- [UnityShader2]表面着色器实例(一)
- python-highlight
- 简洁清爽的LaTeX论文模板
- 【iOS开发】从 UIWebView 到 WKWebView
- Perl 5 中的三大OO特性
- Android开发初学者必看文档:Android开发规范
- HDU 1166 敌兵布阵 (树状数组 || 线段树单点更新)
- 将一个字符串按照单词逆转
- 适配 Android N 需要注意什么
- 引入js文件加版本号
- 【模板】用Java连接数据库
- [hdu2035] 人见人爱A^B
- 使用github和SourceTree进行源代码管理遇到POST git-receive-pack (chunked) 问题