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(); ,其结果输出相同,为:
如果您对本文有任何疑问或者建议,请到讨论区发表您的意见:
>>
论坛入口 <<
上一篇:
实战 FastCGI 下一篇:
Perl 新手必读
【文章评论】
【收藏本文】
【推荐好友】
【打印本文】
【我要投稿】 【论坛讨论】