perl-面向对象

我看这个教程,也是觉得有点意思。面向对象一页就将完了?但是仔细去一看,却又无比清晰,我甚至觉得我python面向对象学的有点问题。因为关于实例化一个对象的过程,理解的没有这里深刻。而他仅仅只用了几句话就讲清楚了事物的本质。

所以有必要重新认识一下OOP:

OOP的目的是什么

除了相比于perl中的模块或者python中的函数而言,一个重要的特性是支持:继承,封装,多态,当然perl中的模块也具有封装的特点。可以看出OOP对代码的复用率更高。

Object对象:

我觉得这个解释特别好:

An object is a single entity that combines both data and code. Object is described in the following terms:

  • Actions or methods describe what it can do. It is the code part of the object.
  • Attributes or properties describe what information the object conveys. It is the data part of the object.

Class类

A class is a blueprint or template of similar objects.

封装继承多态的解释:

Encapsulation

Through object, you can hide its complexity which is known as abstraction or encapsulation in object oriented programming. It means the client of the object does not need to care about the internal logic of the object but still can use the object through its interfaces (methods).

Inheritance

Inheritance means one class inherits both attributes and methods from another class. Inheritance enables you to reuse and extend existing classes without copy-n-paste the code or re-invent the wheel.

  • A class that other classes inherit from is called base class or superclass.
  • A class that inherits from other class called subclass or derived class.

Polymorphism

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


以上属于通用oop的理解。下面是perl中的oop。

perl中的oop

这里对象的解释一语道破类与对象的本质区别。类只是一个框架模板,而对象则是一个reference,一个内存地址,就是为了知道它是属于那个类的。

Perl OOP rules

可以看出从结构上与module差别不是很大。

There are three important rules in Perl object oriented programming:

定义perl的class

在perl中class指的就是一个package,和模块一样。

  1. 选定好一个class的名字:Product
  2. 新建文件:Product.pm
1
package Product;

构建perl的object

这里与python差别可是有点大。perl中使用一个子程序、函数来构建object。一般来说我们会使用new作为函数的名称,比较容易辨别。当然了其它任何名字都是可以的。

1
2
3
4
5
6
7
8
sub new{
my($class_name,$args) = @_;
my $self = bless {
serial => $args -> {serial},
name => $args -> {name},
price => $args -> {price},
},$class;
}
  1. 和模块不同,每当我们调用new()方法的时候,perl就会自动的将类的名字Product作为第一个参数传入到了特殊变量@_中。

  2. 当我们创建一个object时,实际上是创建了一个reference,告诉它属于那个类。而内建的函数bless就是用来创建一个类的reference,同时返回一个类的实例。

  3. bless() 函数的语法:

    1
    object = bless reference, classname;

    We’ve passed a hash reference to the bless()function. You can pass any kind of reference to the bless function e.g., array reference, however, it is much easier to work with a hash reference.

调用这个Product类

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/perl
use strict;
use warnings;

use Product;

my $iphone = Product->new({
serial =>"100",
name => "iPhone 5",
price => 650.00});

We called the method new()of the Productclass and get an object $iphone. We passed a hash reference to the new() method containing serial, nameand price.

完整的事例

定义一个Product类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package Product;

use strict;
use warnings;

# init product with serial, name and price
sub new{
my ($class,$args) = @_;
my $self = bless { serial => $args->{serial},
name => $args->{name},
price => $args->{price}
}, $class;
}
# get name of the product
sub get_name{
my $self = shift;
return $self->{name};
}

# set new name for the product
sub set_name{
my ($self,$new_name) = @_;
$self->{name} = $new_name;
}

# get price of the product
sub get_price{
my $self = shift;
return $self->{price};
}

# set price for the product
sub set_price{
my ($self,$new_price) = @_;
$self->{price} = $new_price;
}
# get serial
sub get_serial{
my $self = shift;
return $self->{serial};
}
# set serial
sub set_serial{
my ($self,$new_price) = @_;
$self->{price} = $new_price;
}
# return formatted string of the product
sub to_string{
my $self = shift;
return "Serial: $self->{serial}\nName: $self->{name}\nPrice: $self->{price}USD\n";
}

1;

使用Product类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl
use strict;
use warnings;

use Product;

my $iphone = Product->new({
serial =>"100",
name => "iPhone",
price => 650.00});
my $nexus = Product->new({ serial =>"101",
name => "Nexus",
price => 299.00});

print $iphone->to_string();
print $nexus->to_string();

这里还是有许多不是很明白的地方,以后边用边加深理解。

另外我安装了一个atom,因为我的vim总感觉缺点啥,可能是没有自动补全吧。没有安装sublime是因为在windows上老是弹出需要购买的信息。看着不是很舒服