package Cocoa;
#
# Put "require" statements in for all required,imported packages
#
#
# Just add code here
#
1; # terminate the package with the required 1;
接下來,我們往包里添加方法使之成為一個類。第一個需添加的方法是new(),它是創建對象時必須被調用的,new()方法是對象的構造函數.
四、構造函數sub new {
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;
{}創建一個對不含鍵/值對的哈希表(即關聯數組)的引用,返回值被賦給局域變量$this。函數bless()取出該引用,告訴對象它引用的是Cocoa,最后返回該引用。函數的返回值現在指向這個匿名哈希表。
從new()函數返回后,$this引用被銷毀,但調用函數保存了對該哈希表的引用,因此該哈希表的引用數不會為零,從而使Perl在內存中保存該哈希表。創建對象可如下調用:
$cup = new Cocoa;
旅嬗錁湮?褂酶冒?唇ǘ韻蟮睦?櫻?
1 #!/usr/bin/perl
2 push (@INC,"pwd");
3 use Cocoa;
4 $cup = new Cocoa;
第一行指出Perl解釋器的位置,第二行中,將當前目錄加到路徑尋找列表@INC中供尋找包時使用。你也可以在不同的目錄中創建你的模塊并指出該絕對路徑。例如,如果在/home/test/scripts/創建包,第二行就應該如下:
push (@INC , "/home/test/scripts");
在第三行中,包含上包Cocoa.pm以獲取腳本中所需功能。use語句告訴Perl在@INC路徑尋找文件Cocoa.pm并包含到解析的源文件拷貝中。use語句是使用類必須的。第四行調用new函數創建對象,這是Perl的妙處,也是其易混淆之處,也是其強大之處。創建對象的方法有多種,可以這樣寫:
$cup = cocoa->new();
如果你是C程序員,可以用雙冒號強制使用Cocoa包中的new()函數,如:
$cup = Cocoa::new();
可以在構造函數中加入更多的代碼,如在Cocoa.pm中,可以在每個對象創建時輸出一個簡單聲明,還可以用構造函數初始化變量或設置數組或指針。
注意:
1、一定要在構造函數中初始化變量;
2、一定要用my函數在方法中創建變量;
3、一定不要在方法中使用local,除非真的想把變量傳遞給其它子程序;
4、一定不要在類模塊中使用全局變量。
加上聲明的Cocoa構造函數如下:
sub new {
my $this = {};
print "
/*
** Created by Cocoa.pm
** Use at own risk";
print "
** Did this code even get pass the javac compiler? ";
print "
**/
";
bless $this;
return $this;
}
也可以簡單地調用包內或包外的其它函數來做更多的初始化工作,如:
sub new {
my $this = {}
bless $this;
$this->doInitialization();
return $this;
}
創建類時,應該允許它可被繼承,應該可以把類名作為第一個參數來調用new函數,那么new函數就象下面的語句:
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;
}
此方法使用戶可以下列三種方式之一來進行調用:
Cocoa::new()
Cocoa->new()
new Cocoa
可以多次bless一個引用對象,然而,新的將被bless的類必然把對象已被bless的引用去掉,對C和Pascal程序員來說,這就象把一個指針賦給分配的一塊內存,再把同一指針賦給另一塊內存而不釋放掉前一塊內存??傊?,一個Perl對象每一時刻只能屬于一個類。
對象和引用的真正區別是什么呢?Perl對象被bless以屬于某類,引用則不然,如果引用被bless,它將屬于一個類,也便成了對象。對象知道自己屬于哪個類,引用則不屬于任何類。
實例變量
作為構造函數的new()函數的參數叫做實例變量。實例變量在創建對象的每個實例時用于初始化,例如可以用new()函數為對象的每個實例起個名字。
可以用匿名哈希表或匿名數組來保存實例變量。
用哈希表的代碼如下:
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;
}
構造對象時,可以如下傳遞參數:
$mug = Cocoa::new( "Name" => "top","x" => 10,"y" => 20 );
操作符=>與逗號操作服功能相同,但=>可讀性好。訪問方法如下:
print "Name=$mug->{"Name"}
";
print "x=$mug->{"x"}
";
print "y=$mug->{"y"}
";
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. }
六、方法的輸出
如果你現在想引用Cocoa.pm包,將會得到編譯錯誤說未找到方法,這是因為Cocoa.pm的方法還沒有輸出。輸出方法需要Exporter模塊,在包的開始部分加上下列兩行:
require Exporter;
@ISA = qw (Exporter);
這兩行包含上Exporter.pm模塊,并把Exporter類名加入@ISA數組以供查找。接下來把你自己的類方法列在@EXPORT數組中就可以了。例如想輸出方法closeMain和declareMain,語句如下:
@EXPORT = qw (declareMain , closeMain);
Perl類的繼承是通過@ISA數組實現的。@ISA數組不需要在任何包中定義,然而,一旦它被定義,Perl就把它看作目錄名的特殊數組。它與@INC數組類似,@INC是包含文件的尋找路徑。@ISA數組含有類(包)名,當一個方法在當前包中未找到時就到@ISA中的包去尋找。@ISA中還含有當前類繼承的基類名。
類中調用的所有方法必須屬于同一個類或@ISA數組定義的基類。如果一個方法在@ISA數組中未找到,Perl就到AUTOLOAD()子程序中尋找,這個可選的子程序在當前包中用sub定義。若使用AUTOLOAD子程序,必須用use Autoload;語句調用autoload.pm包。AUTOLOAD子程序嘗試從已安裝的Perl庫中裝載調用的方法。如果AUTOLOAD也失敗了,Perl再到UNIVERSAL類做最后一次嘗試,如果仍失敗,Perl就生成關于該無法解析函數的錯誤。
七、方法的調用
調用一個對象的方法有兩種方法,一是通過該對象的引用(虛方法),一是直接使用類名(靜態方法)。當然該方法必須已被輸出?,F在給Cocoa類增加一些方法,代碼如下:
package Cocoa;
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;
現在,我們寫一個簡單的Perl腳本來使用該類的方法,下面是創建一個Java applet源代碼骨架的腳本代碼:
#!/usr/bin/perl
use Cocoa;
$cup = new Cocoa;
$cup->setImports( "java.io.InputStream", "java.net.*");
$cup->declareMain( "Msg" , "java.applet.Applet", "Runnable");
$cup->closeMain();
這段腳本創建了一個叫做Msg的Java applet,它擴展(extend)了java.applet.Applet小應用程序并使之可運行(runnable),其中最后三行也可以寫成如下:
Cocoa::setImports($cup, "java.io.InputStream", "java.net.*");
Cocoa::declareMain($cup, "Msg" , "java.applet.Applet", "Runnable");
Cocoa::closeMain($cup);
其運行結果如下:
/*
** Created by Cocoa.pm
** Use at own risk
*/
import java.io.InputStream;
import java.net.*;
public class Msg extends java.applet.Applet implements Runnable {
}
注意:如果用->操作符調用方法(也叫間接調用),參數必須用括號括起來,如:$cup->setImports( "java.io.InputStream", "java.net.*");而雙冒號調用如:Cocoa::setImports($cup, "java.io.InputStream", "java.net.*");也可去掉括號寫成:Cocoa::setImports $cup, "java.io.InputStream", "java.net.*" ;
八、重載sub DESTROY {
#
# Add code here.
#
}
因為多種目的,Perl使用了簡單的、基于引用的垃圾回收系統。任何對象的引用數目必須大于零,否則該對象的內存就被釋放。當程序退出時,Perl的一個徹底的查找并銷毀函數進行垃圾回收,進程中的一切被簡單地刪除。在UNIX類的系統中,這像是多余的,但在內嵌式系統或多線程環境中這確實很必要。
十、繼承
類方法通過@ISA數組繼承,變量的繼承必須明確設定。下例創建兩個類Bean.pm和Coffee.pm,其中Coffee.pm繼承Bean.pm的一些功能。此例演示如何從基類(或稱超類)繼承實例變量,其方法為調用基類的構造函數并把自己的實例變量加到新對象中。
Bean.pm代碼如下:
package Bean;
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;
此類中,用$this變量設置一個匿名哈希表,將"Bean"類型設為"Colombian"。方法setBeanType()用于改變"Bean"類型,它使用$class引用獲得對對象哈希表的訪問。
Coffee.pm代碼如下:
1 #
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;
第6行的require Bean;語句包含了Bean.pm文件和所有相關函數,方法setCoffeeType()用于設置局域變量$class->{"Coffee"}的值。在構造函數new()中,$this指向Bean.pm返回的匿名哈希表的指針,而不是在本地創建一個,下面兩個語句分別為創建不同的哈希表從而與Bean.pm構造函數創建的哈希表無關的情況和繼承的情況:
my $this = {}; #非繼承
my $this = $theSuperClass->new(); #繼承
下面代碼演示如何調用繼承的方法:
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 ------------
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
上述代碼中,先輸出對象創建時哈希表中索引為"Bean"和"Coffee"的值,然后調用各成員函數改變值后再輸出。
方法可以有多個參數,現在向Coffee.pm模塊增加函數makeCup(),代碼如下:
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 cubesn" 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:
==================================
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 ;-)
==================================
在此例中,函數makeCup()的參數既可為字符串也可為整數,處理結果相同,你也可以把這兩種類型的數據處理區分開。在對參數的處理中,可以設置缺省的值,也可以根據實際輸入參數值的個數給予不同處理。
十一、子類方法的重載
繼承的好處在于可以獲得基類輸出的方法的功能,而有時需要對基類的方法重載以獲得更具體或不同的功能。下面在Bean.pm類中加入方法printType(),代碼如下:
sub printType {
my $class = shift @_;
print "The type of Bean is $class->{"Bean"} n";
}
然后更新其@EXPORT數組來輸出:
@EXPORT = qw ( setBeanType , printType );
現在來調用函數printType(),有三種調用方法:
$cup->Coffee::printType();
$cup->printType();
$cup->Bean::printType();
輸出分別如下:
The type of Bean is Mixed
The type of Bean is Mixed
The type of Bean is Mixed
為什么都一樣呢?因為在子類中沒有定義函數printType(),所以實際均調用了基類中的方法。如果想使子類有其自己的printType()函數,必須在Coffee.pm類中加以定義:
#
# This routine prints the type of $class->{"Coffee"}
#
sub printType {
my $class = shift @_;
print "The type of Coffee is $class->{"Coffee"} n";
}
然后更新其@EXPORT數組:
@EXPORT = qw(setImports, declareMain, closeMain, printType);
現在輸出結果變成了:
The type of Coffee is Instant
The type of Coffee is Instant
The type of Bean is Mixed
現在只有當給定了Bean::時才調用基類的方法,否則直接調用子類的方法。
那么如果不知道基類名該如何調用基類方法呢?方法是使用偽類保留字SUPER::。在類方法內使用語法如:$this->SUPER::function(...argument list...); ,它將從@ISA列表中尋找。剛才的語句用SUPER::替換Bean::可以寫為$cup->SUPER::printType(); ,其結果輸出相同,為:
The type of Bean is Mixed
十二、Perl類和對象的一些注釋1、一定要通過方法來訪問類變量。
2、一定不要從模塊外部直接訪問類變量。
當編寫包時,應該保證方法所需的條件已具備或通過參數傳遞給它。在包內部,應保證對全局變量的訪問只用通過方法傳遞的引用來訪問。對于方法要使用的靜態或全局數據,應該在基類中用local()來定義,子類通過調用基類來獲取。有時,子類可能需要改變這種數據,這時,基類可能就不知道怎樣去尋找新的數據,因此,這時最好定義對該數據的引用,子類和基類都通過引用來改變該數據。
最后,你將看到如下方式來使用對象和類:
use coffee::Bean;
這句語句的含義是“在@INC數組所有目錄的Coffee子目錄來尋找Bean.pm”。如果把Bean.pm移到./Coffee目錄,上面的例子將用這一use語句來工作。這樣的好處是有條理地組織類的代碼。再如,下面的語句:
use Another::Sub::Menu;
意味著如下子目錄樹:
./Another/Sub/Menu.pm