第十三章 Perl的面向对象编程
一、模块简介package Cocoa;接下来,我们往包里添加方法使之成为一个类。第一个需添加的方法是new(),它是创建对象时必须被调用的,new()方法是对象的构造函数。
#
# Put "require" statements in for all required,imported packages
#
#
# Just add code here
#
1; # terminate the package with the required 1;
sub new {{}创建一个对不含键/值对的哈希表(即关联数组)的引用,返回值被赋给局域变量$this。函数bless()取出该引用,告诉对象它引用的是Cocoa,最后返回该引用。函数的返回值现在指向这个匿名哈希表。
my $this = {}; # Create an anonymous hash, and #self points to it.
bless $this; # Connect the hash to the package Cocoa.
return $this; # Return the reference to the hash.
}
1;
1 #!/usr/bin/perl第一行指出Perl解释器的位置,第二行中,将当前目录加到路径寻找列表@INC中供寻找包时使用。你也可以在不同的目录中创建你的模块并指出该绝对路径。例如,如果在/home/test/scripts/创建包,第二行就应该如下:
2 push (@INC,'pwd');
3 use Cocoa;
4 $cup = new Cocoa;
1、一定要在构造函数中初始化变量;加上声明的Cocoa构造函数如下:
2、一定要用my函数在方法中创建变量;
3、一定不要在方法中使用local,除非真的想把变量传递给其它子程序;
4、一定不要在类模块中使用全局变量。
sub new {也可以简单地调用包内或包外的其它函数来做更多的初始化工作,如:
my $this = {};
print "\n /* \n ** Created by Cocoa.pm \n ** Use at own risk";
print "\n ** Did this code even get pass the javac compiler? ";
print "\n **/ \n";
bless $this;
return $this;
}
sub new {创建类时,应该允许它可被继承,应该可以把类名作为第一个参数来调用new函数,那么new函数就象下面的语句:
my $this = {}
bless $this;
$this->doInitialization();
return $this;
}
sub new {此方法使用户可以下列三种方式之一来进行调用:
my $class = shift; # Get the request class name
my $this = {};
bless $this, $class # Use class name to bless() reference
$this->doInitialization(); return $this;
}
可以多次bless一个引用对象,然而,新的将被bless的类必然把对象已被bless的引用去掉,对C和Pascal程序员来说,这就象把一个指针赋给分配的一块内存,再把同一指针赋给另一块内存而不释放掉前一块内存。总之,一个Perl对象每一时刻只能属于一个类。Cocoa::new() Cocoa->new() new Cocoa
sub new {用数组保存的代码如下:my $type = shift;}
my %parm = @_;
my $this = {};
$this->{'Name'} = $parm{'Name'};
$this->{'x'} = $parm{'x'};
$this->{'y'} = $parm{'y'};
bless $this, $type;
sub new {构造对象时,可以如下传递参数:my $type = shift;}
my %parm = @_;
my $this = [];
$this->[0] = $parm{'Name'};
$this->[1] = $parm{'x'};
$this->[2] = $parm{'y'};
bless $this, $type;
1. sub nameLister {六、方法的输出
2. my $this = shift;
3. my ($keys ,$value );
4. while (($key, $value) = each (%$this)) {
5. print "\t$key is $value.\n";
6. }
7. }
package Cocoa;现在,我们写一个简单的Perl脚本来使用该类的方法,下面是创建一个Java applet源代码骨架的脚本代码:
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(setImports, declareMain, closeMain);
#
# This routine creates the references for imports in Java functions
#
sub setImports{
my $class = shift @_;
my @names = @_;
foreach (@names) {
print "import " . $_ . ";\n";
}
}
#
# This routine declares the main function in a Java script
#
sub declareMain{
my $class = shift @_;
my ( $name, $extends, $implements) = @_;
print "\n public class $name";
if ($extends) {
print " extends " . $extends;
}
if ($implements) {
print " implements " . $implements;
}
print " { \n";
}
#
# This routine declares the main function in a Java script
#
sub closeMain{
print "} \n";
}
#
# This subroutine creates the header for the file.
#
sub new {
my $this = {};
print "\n /* \n ** Created by Cocoa.pm \n ** Use at own risk \n */ \n";
bless $this;
return $this;
}
1;
#!/usr/bin/perl这段脚本创建了一个叫做Msg的Java applet,它扩展(extend)了java.applet.Applet小应用程序并使之可运行(runnable),其中最后三行也可以写成如下:
use Cocoa;
$cup = new Cocoa;
$cup->setImports( 'java.io.InputStream', 'java.net.*');
$cup->declareMain( "Msg" , "java.applet.Applet", "Runnable");
$cup->closeMain();
Cocoa::setImports($cup, 'java.io.InputStream', 'java.net.*');其运行结果如下:
Cocoa::declareMain($cup, "Msg" , "java.applet.Applet", "Runnable");
Cocoa::closeMain($cup);
/*注意:如果用->操作符调用方法(也叫间接调用),参数必须用括号括起来,如:$cup->setImports( 'java.io.InputStream', 'java.net.*');而双冒号调用如:Cocoa::setImports($cup, 'java.io.InputStream', 'java.net.*');也可去掉括号写成:Cocoa::setImports $cup, 'java.io.InputStream', 'java.net.*' ;
** Created by Cocoa.pm
** Use at own risk
*/
import java.io.InputStream;
import java.net.*;
public class Msg extends java.applet.Applet implements Runnable {
}
sub DESTROY {因为多种目的,Perl使用了简单的、基于引用的垃圾回收系统。任何对象的引用数目必须大于零,否则该对象的内存就被释放。当程序退出时,Perl的一个彻底的查找并销毁函数进行垃圾回收,进程中的一切被简单地删除。在UNIX类的系统中,这像是多余的,但在内嵌式系统或多线程环境中这确实很必要。
#
# Add code here.
#
}
package Bean;此类中,用$this变量设置一个匿名哈希表,将'Bean'类型设为'Colombian'。方法setBeanType()用于改变'Bean'类型,它使用$class引用获得对对象哈希表的访问。
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(setBeanType);
sub new {
my $type = shift;
my $this = {};
$this->{'Bean'} = 'Colombian';
bless $this, $type;
return $this;
}
#
# This subroutine sets the class name
sub setBeanType{
my ($class, $name) = @_;
$class->{'Bean'} = $name;
print "Set bean to $name \n";
}
1;
1 #第6行的require Bean;语句包含了Bean.pm文件和所有相关函数,方法setCoffeeType()用于设置局域变量$class->{'Coffee'}的值。在构造函数new()中,$this指向Bean.pm返回的匿名哈希表的指针,而不是在本地创建一个,下面两个语句分别为创建不同的哈希表从而与Bean.pm构造函数创建的哈希表无关的情况和继承的情况:
2 # The Coffee.pm file to illustrate inheritance.
3 #
4 package Coffee;
5 require Exporter;
6 require Bean;
7 @ISA = qw(Exporter, Bean);
8 @EXPORT = qw(setImports, declareMain, closeMain);
9 #
10 # set item
11 #
12 sub setCoffeeType{
13 my ($class,$name) = @_;
14 $class->{'Coffee'} = $name;
15 print "Set coffee type to $name \n";
16 }
17 #
18 # constructor
19 #
20 sub new {
21 my $type = shift;
22 my $this = Bean->new(); ##### <- LOOK HERE!!! ####
23 $this->{'Coffee'} = 'Instant'; # unless told otherwise
24 bless $this, $type;
25 return $this;
26 }
27 1;
1 #!/usr/bin/perl该代码的结果输出如下:
2 push (@INC,'pwd');
3 use Coffee;
4 $cup = new Coffee;
5 print "\n -------------------- Initial values ------------ \n";
6 print "Coffee: $cup->{'Coffee'} \n";
7 print "Bean: $cup->{'Bean'} \n";
8 print "\n -------------------- Change Bean Type ---------- \n";
9 $cup->setBeanType('Mixed');
10 print "Bean Type is now $cup->{'Bean'} \n";
11 print "\n ------------------ Change Coffee Type ---------- \n";
12 $cup->setCoffeeType('Instant');
13 print "Type of coffee: $cup->{'Coffee'} \n";
-------------------- Initial values ------------上述代码中,先输出对象创建时哈希表中索引为'Bean'和'Coffee'的值,然后调用各成员函数改变值后再输出。
Coffee: Instant
Bean: Colombian
-------------------- Change Bean Type ----------
Set bean to Mixed
Bean Type is now Mixed
------------------ Change Coffee Type ----------
Set coffee type to Instant
Type of coffee: Instant
sub makeCup {此函数可有三个参数,不同数目、值的参数产生不同的结果,例如:
my ($class, $cream, $sugar, $dope) = @_;
print "\n================================== \n";
print "Making a cup \n";
print "Add cream \n" if ($cream);
print "Add $sugar sugar cubes\n" if ($sugar);
print "Making some really addictive coffee ;-) \n" if ($dope);
print "================================== \n";
}
1 #!/usr/bin/perl其结果输出如下:
2 push (@INC,'pwd');
3 use Coffee;
4 $cup = new Coffee;
5 #
6 # With no parameters
7 #
8 print "\n Calling with no parameters: \n";
9 $cup->makeCup;
10 #
11 # With one parameter
12 #
13 print "\n Calling with one parameter: \n";
14 $cup->makeCup('1');
15 #
16 # With two parameters
17 #
18 print "\n Calling with two parameters: \n";
19 $cup->makeCup(1,'2');
20 #
21 # With all three parameters
22 #
23 print "\n Calling with three parameters: \n";
24 $cup->makeCup('1',3,'1');
Calling with no parameters:在此例中,函数makeCup()的参数既可为字符串也可为整数,处理结果相同,你也可以把这两种类型的数据处理区分开。在对参数的处理中,可以设置缺省的值,也可以根据实际输入参数值的个数给予不同处理。
==================================
Making a cup
==================================
Calling with one parameter:
==================================
Making a cup
Add cream
==================================
Calling with two parameters:
==================================
Making a cup
Add cream
Add 2 sugar cubes
==================================
Calling with three parameters:
==================================
Making a cup
Add cream
Add 3 sugar cubes
Making some really addictive coffee ;-)
==================================
sub printType {然后更新其@EXPORT数组来输出:
my $class = shift @_;
print "The type of Bean is $class->{'Bean'} \n";
}
$cup->Coffee::printType();输出分别如下:
$cup->printType();
$cup->Bean::printType();
The type of Bean is Mixed为什么都一样呢?因为在子类中没有定义函数printType(),所以实际均调用了基类中的方法。如果想使子类有其自己的printType()函数,必须在Coffee.pm类中加以定义:
The type of Bean is Mixed
The type of Bean is Mixed
#然后更新其@EXPORT数组:
# This routine prints the type of $class->{'Coffee'}
#
sub printType {
my $class = shift @_;
print "The type of Coffee is $class->{'Coffee'} \n";
}
The type of Coffee is Instant现在只有当给定了Bean::时才调用基类的方法,否则直接调用子类的方法。
The type of Coffee is Instant
The type of Bean is Mixed
The type of Bean is Mixed十二、Perl类和对象的一些注释
1、一定要通过方法来访问类变量。当编写包时,应该保证方法所需的条件已具备或通过参数传递给它。在包内部,应保证对全局变量的访问只用通过方法传递的引用来访问。对于方法要使用的静态或全局数据,应该在基类中用local()来定义,子类通过调用基类来获取。有时,子类可能需要改变这种数据,这时,基类可能就不知道怎样去寻找新的数据,因此,这时最好定义对该数据的引用,子类和基类都通过引用来改变该数据。
2、一定不要从模块外部直接访问类变量。