文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>perl5中文教程 14

perl5中文教程 14

时间:2007-03-09  来源:darrenshen

http://www.8esky.com/cms/Site/Article/wlbch15/perl408/51871_P3.html   此类中,用$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 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:
==================================
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

相关阅读 更多 +
排行榜 更多 +
方块枪战战场安卓版

方块枪战战场安卓版

飞行射击 下载
战斗火力射击安卓版

战斗火力射击安卓版

飞行射击 下载
空中防御战安卓版

空中防御战安卓版

飞行射击 下载