第十三章 Perl的面向?qū)ο缶幊?/p>
一、模塊簡介package Cocoa;接下來,我們往包里添加方法使之成為一個類。第一個需添加的方法是new(),它是創(chuàng)建對象時必須被調(diào)用的,new()方法是對象的構(gòu)造函數(shù)。
#
# Put "require" statements in for all required,imported packages
#
#
# Just add code here
#
1; # terminate the package with the required 1;
sub new {{}創(chuàng)建一個對不含鍵/值對的哈希表(即關(guān)聯(lián)數(shù)組)的引用,返回值被賦給局域變量$this。函數(shù)bless()取出該引用,告訴對象它引用的是Cocoa,最后返回該引用。函數(shù)的返回值現(xiàn)在指向這個匿名哈希表。
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中供尋找包時使用。你也可以在不同的目錄中創(chuàng)建你的模塊并指出該絕對路徑。例如,如果在/home/test/scripts/創(chuàng)建包,第二行就應該如下:
2 push (@INC,'pwd');
3 use Cocoa;
4 $cup = new Cocoa;
1、一定要在構(gòu)造函數(shù)中初始化變量;加上聲明的Cocoa構(gòu)造函數(shù)如下:
2、一定要用my函數(shù)在方法中創(chuàng)建變量;
3、一定不要在方法中使用local,除非真的想把變量傳遞給其它子程序;
4、一定不要在類模塊中使用全局變量。
sub new {也可以簡單地調(diào)用包內(nèi)或包外的其它函數(shù)來做更多的初始化工作,如:
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 {創(chuàng)建類時,應該允許它可被繼承,應該可以把類名作為第一個參數(shù)來調(diào)用new函數(shù),那么new函數(shù)就象下面的語句:
my $this = {}
bless $this;
$this->doInitialization();
return $this;
}
sub new {此方法使用戶可以下列三種方式之一來進行調(diào)用:
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程序員來說,這就象把一個指針賦給分配的一塊內(nèi)存,再把同一指針賦給另一塊內(nèi)存而不釋放掉前一塊內(nèi)存?傊粋Perl對象每一時刻只能屬于一個類。Cocoa::new() Cocoa->new() new Cocoa
sub new {用數(shù)組保存的代碼如下:my $type = shift;}
my %parm = @_;
my $this = {};
$this->{'Name'} = $parm{'Name'};
$this->{'x'} = $parm{'x'};
$this->{'y'} = $parm{'y'};
bless $this, $type;
sub new {構(gòu)造對象時,可以如下傳遞參數(shù):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;現(xiàn)在,我們寫一個簡單的Perl腳本來使用該類的方法,下面是創(chuàng)建一個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這段腳本創(chuàng)建了一個叫做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.*');其運行結(jié)果如下:
Cocoa::declareMain($cup, "Msg" , "java.applet.Applet", "Runnable");
Cocoa::closeMain($cup);
/*注意:如果用->操作符調(diào)用方法(也叫間接調(diào)用),參數(shù)必須用括號括起來,如:$cup->setImports( 'java.io.InputStream', 'java.net.*');而雙冒號調(diào)用如: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使用了簡單的、基于引用的垃圾回收系統(tǒng)。任何對象的引用數(shù)目必須大于零,否則該對象的內(nèi)存就被釋放。當程序退出時,Perl的一個徹底的查找并銷毀函數(shù)進行垃圾回收,進程中的一切被簡單地刪除。在UNIX類的系統(tǒng)中,這像是多余的,但在內(nèi)嵌式系統(tǒng)或多線程環(huán)境中這確實很必要。
#
# 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文件和所有相關(guān)函數(shù),方法setCoffeeType()用于設置局域變量$class->{'Coffee'}的值。在構(gòu)造函數(shù)new()中,$this指向Bean.pm返回的匿名哈希表的指針,而不是在本地創(chuàng)建一個,下面兩個語句分別為創(chuàng)建不同的哈希表從而與Bean.pm構(gòu)造函數(shù)創(chuàng)建的哈希表無關(guān)的情況和繼承的情況:
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該代碼的結(jié)果輸出如下:
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 ------------上述代碼中,先輸出對象創(chuàng)建時哈希表中索引為'Bean'和'Coffee'的值,然后調(diào)用各成員函數(shù)改變值后再輸出。
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 {此函數(shù)可有三個參數(shù),不同數(shù)目、值的參數(shù)產(chǎn)生不同的結(jié)果,例如:
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其結(jié)果輸出如下:
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:在此例中,函數(shù)makeCup()的參數(shù)既可為字符串也可為整數(shù),處理結(jié)果相同,你也可以把這兩種類型的數(shù)據(jù)處理區(qū)分開。在對參數(shù)的處理中,可以設置缺省的值,也可以根據(jù)實際輸入?yún)?shù)值的個數(shù)給予不同處理。
==================================
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數(shù)組來輸出:
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為什么都一樣呢?因為在子類中沒有定義函數(shù)printType(),所以實際均調(diào)用了基類中的方法。如果想使子類有其自己的printType()函數(shù),必須在Coffee.pm類中加以定義:
The type of Bean is Mixed
The type of Bean is Mixed
#然后更新其@EXPORT數(shù)組:
# 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現(xiàn)在只有當給定了Bean::時才調(diào)用基類的方法,否則直接調(diào)用子類的方法。
The type of Coffee is Instant
The type of Bean is Mixed
The type of Bean is Mixed十二、Perl類和對象的一些注釋
1、一定要通過方法來訪問類變量。當編寫包時,應該保證方法所需的條件已具備或通過參數(shù)傳遞給它。在包內(nèi)部,應保證對全局變量的訪問只用通過方法傳遞的引用來訪問。對于方法要使用的靜態(tài)或全局數(shù)據(jù),應該在基類中用local()來定義,子類通過調(diào)用基類來獲取。有時,子類可能需要改變這種數(shù)據(jù),這時,基類可能就不知道怎樣去尋找新的數(shù)據(jù),因此,這時最好定義對該數(shù)據(jù)的引用,子類和基類都通過引用來改變該數(shù)據(jù)。
2、一定不要從模塊外部直接訪問類變量。