第<7>章 perl 中的对象:

来源:互联网 发布:9.9天天特价淘宝 编辑:程序博客网 时间:2024/06/11 12:41

Perl 中对象

一个对象(还可以称作一个实例,instance)就像一辆给定的轿车,拥有下面的这些内容:

某个特定类型的对象被称作属于一个类(class),我的车与你的车都属于叫做汽车的类

一个类的所有对象都具有相同的功能。

属性

一个对象就是 一组属性的集合,我们可以用数组或散列来表示这个集合。例如,如果你需要记录下雇员的详细情况的话

[root@master obj]# cat a1.pl

使用散列表来储存雇员的属性

%employee=(“name”=>”John Doe”,
“age” => 32,
“position”=> “Software Engineer”);
print “Name:$employee{name}”;
[root@master obj]# perl a1.pl
Name:John Doe[root@master obj]#

唯一标识:

很明显,一个%employee 是不够的,每个雇员都要求有一个唯一标识和他或她自己的属性集合。

你可以动态的分配这个数据结构,也可以返回一个指向局部数据结构的引用,如下所示:

[root@master obj]# cat a2.pl

使用匿名散列表

#
use Data::Dumper;
sub new_employee{
my(name,age,startingposition)=@;myr_employee = { ##使用匿名散列表
“name” =>name,              #创建一个唯一的对象  
     “age” =>
age,
“position” =>starting_position  
};  
  return
r_employee; #返回对象
};
yy=&new_employee(a1,b2,c3);  
my
xx= Dumper(yy);print111111111\n;printxx;
print “\n”;
[root@master obj]# perl a2.pl
111111111
$VAR1 = {
‘position’ => ‘c3’,
‘name’ => ‘a1’,
‘age’ => ‘b2’
};

[root@master obj]# cat a3.pl

或者返回指向局部变量的引用

use Data::Dumper;
sub new_employee{
my (name,age,$starting_position)=@_;

my %employee = (
“name” =>name,age=>age,
“position” => $starting_position
);
return \%employee; #返回指向局部变量的引用
}

使用它来创建两个雇员

emp1=newemployee(JohnDoe,32,SoftwareEngineer);myxx= Dumper(emp1);print111111111\n;printxx;
print “\n”;
[root@master obj]# perl a3.pl
111111111
$VAR1 = {
‘position’ => ‘Software Engineer’,
‘name’ => ‘John Doe’,
‘age’ => 32
};

在上面的两种情况下,new_employee() 都将返回指向一个唯一数据结构的引用

在上面的例子中,散列表就是对象,指向散列表的引用则称之为对象的引用

[root@master obj]# cat Employee.pm
package Employee;
sub new { my(name,age,startingposition)=@;myr_employee = { ##使用匿名散列表
“name” =>name,              #创建一个唯一的对象  
     “age” =>
age,
“position” =>starting_position  
};  
  return
r_employee; #返回对象
};
sub promote{
remployee=shift;x = shift;
y=x + 3;
return $y
};
1;

[root@master obj]# cat a4.pl
unshift(@INC,”/root/obj”);
require Employee;
use Data::Dumper;

$emp=Employee::new(“John Doe”,32,”Software Engineer”);

my xx=Dumper(emp);
print “111111111\n”;
print $xx;
print “\n”;

yy=Employee::promote("emp”,10);

print “222222\n”;
print $yy;
print “\n”;

cc=Employee::promote>("emp”,20);
print “333333\n”;
print $cc;
print “\n”;

[root@master obj]# perl a4.pl
111111111
$VAR1 = {
‘position’ => ‘Software Engineer’,
‘name’ => ‘John Doe’,
‘age’ => 32
};

222222
13
333333
23

cc=Employee::promote>("emp”,20); 这里没有bless 所以需要使用包::函数 方式

有bless 只需要
yy=emp->promote(‘20’);

在perl里对象就是bless过的引用

bless 在内部将对象标记为指向它们所在包的指针。

bless 的好处在于它提供给我们一种直接使用该对象的方式,如:

首先像以前一样创建两个对象:

$emp1=RegularEmployee::new(“John Doe”,32,”Software Engineer”,5000);

$emp2=HourlyEmployee::new(“Jane Smith”,35,”Auditor”,65,90);

现在我们使用箭头记号来直接调用实例的方法,或者用面向对象的话说,调用对象的方法:

直接调用

$emp1->promote();

当Perl看到emp1?promote(),emp1属于哪个类(也就是在其中执行bless的).

在这里,它是RegularEmployee.

Perl 于是就会如下所示调用这个函数:

RegularEmployee::promote($emp1) 换句话说,箭头左边的对象只是作为相应子例程的第一个参数。

Perl的实例方法没有什么神奇的地方,它只是第一个参数碰巧为对象引用的普通子例程

考虑下面的例子:

$details=; ##输入由制表符分割的雇员详细信息

(type,name,age,4position)=split(/\t/,details);

创建相应类的雇员对象

emp=type->new(name,age,$position);

现在就可以像往常一样来使用这个对象了

$emp->compute_ytd_income();

在这个例子中,$type 中可能包含这两种字符的一种:”HourlyEmployee” 或”RegularEmployee”.

注意这个变量不是对象,它只是类的名字。

0 0
原创粉丝点击