实例解析Perl继承用法

开发 架构
Perl语言中Perl继承的概念你是否了解,这里向大家简单介绍一下,类方法通过@ISA数组Perl继承,变量的Perl继承必须明确设定。

本文和大家重点讨论一下Perl继承的概念和用法,继承简单的说就是一个类继承另一个类后,可以使用被继承类的方法。希望本文的介绍能让你有所收获。

Perl继承

类方法通过@ISA数组Perl继承,变量的Perl继承必须明确设定。下例创建两个类Bean.pm和Coffee.pm,其中Coffee.pmPerl继承Bean.pm的一些功能。此例演示如何从基类(或称超类)Perl继承实例变量,其方法为调用基类的构造函数并把自己的实例变量加到新对象中。
Bean.pm代码如下:

  1. packageBean;  
  2. requireExporter;  
  3. @ISA=qw(Exporter);  
  4. @EXPORT=qw(setBeanType);  
  5.  
  6. subnew{  
  7. my$type=shift;  
  8. my$this={};  
  9. $this->{'Bean'}='Colombian';  
  10. bless$this,$type;  
  11. return$this;  
  12. }  
  13.  
  14. #  
  15. #Thissubroutinesetstheclassname  
  16. subsetBeanType{  
  17. my($class,$name)=@_;  
  18. $class->{'Bean'}=$name;  
  19. print"Setbeanto$name\n";  
  20. }  
  21. 1;  

此类中,用$this变量设置一个匿名哈希表,将'Bean'类型设为'Colombian'。方法setBeanType()用于改变'Bean'类型,它使用$class引用获得对对象哈希表的访问。
Coffee.pm代码如下:

  1. 1#  
  2. 2#TheCoffee.pmfiletoillustrateinheritance.  
  3. 3#  
  4. 4packageCoffee;  
  5. 5requireExporter;  
  6. 6requireBean;  
  7. 7@ISA=qw(Exporter,Bean);  
  8. 8@EXPORT=qw(setImports,declareMain,closeMain);  
  9. 9#  
  10. 10#setitem  
  11. 11#  
  12. 12subsetCoffeeType{  
  13. 13my($class,$name)=@_;  
  14. 14$class->{'Coffee'}=$name;  
  15. 15print"Setcoffeetypeto$name\n";  
  16. 16}  
  17. 17#  
  18. 18#constructor  
  19. 19#  
  20. 20subnew{  
  21. 21my$type=shift;  
  22. 22my$this=Bean->new();#####<-LOOKHERE!!!####  
  23. 23$this->{'Coffee'}='Instant';#unlesstoldotherwise  
  24. 24bless$this,$type;  
  25. 25return$this;  
  26. 26}  
  27. 271;  

 第6行的requireBean;语句包含了Bean.pm文件和所有相关函数,方法setCoffeeType()用于设置局域变量$class->{'Coffee'}的值。在构造函数new()中,$this指向Bean.pm返回的匿名哈希表的指针,而不是在本地创建一个,下面两个语句分别为创建不同的哈希表从而与Bean.pm构造函数创建的哈希表无关的情况和Perl继承的情况:
my$this={};#非Perl继承
my$this=$theSuperClass->new();#Perl继承

下面代码演示如何调用Perl继承的方法:

  1. 1#!/usr/bin/perl  
  2. 2push(@INC,'pwd');  
  3. 3useCoffee;  
  4. 4$cup=newCoffee;  
  5. 5print"\n--------------------Initialvalues------------\n";  
  6. 6print"Coffee:$cup->{'Coffee'}\n";  
  7. 7print"Bean:$cup->{'Bean'}\n";  
  8. 8print"\n--------------------ChangeBeanType----------\n";  
  9. 9$cup->setBeanType('Mixed');  
  10. 10print"BeanTypeisnow$cup->{'Bean'}\n";  
  11. 11print"\n------------------ChangeCoffeeType----------\n";  
  12. 12$cup->setCoffeeType('Instant');  
  13. 13print"Typeofcoffee:$cup->{'Coffee'}\n";  

该代码的结果输出如下:

  1. --------------------Initialvalues------------  
  2. Coffee:Instant  
  3. Bean:Colombian  
  4. --------------------ChangeBeanType----------  
  5. SetbeantoMixed  
  6. BeanTypeisnowMixed  
  7. ------------------ChangeCoffeeType----------  
  8. SetcoffeetypetoInstant  
  9. Typeofcoffee:Instant 


上述代码中,先输出对象创建时哈希表中索引为'Bean'和'Coffee'的值,然后调用各成员函数改变值后再输出。

【编辑推荐】

  1. 构造函数中Perl方法用法解析
  2. Perl文件句柄概念详解
  3. 解析四大Perl操作符用法
  4. 浅析Perl面向对象编程用法
  5. Perl标量转换函数用法指南

 

责任编辑:佚名 来源: csdn.net
相关推荐

2010-07-16 08:47:53

Perl if语句

2010-07-15 15:54:10

Perl守护进程

2010-07-23 13:46:27

Perl语言

2010-07-13 13:49:43

Perl foreac

2010-07-14 15:32:21

Perl转义字符

2010-07-16 13:10:36

Perl哈希表

2010-07-21 13:59:59

Perl引用

2010-07-15 10:22:23

Perl控制结构

2010-07-19 14:13:41

Perl函数

2010-07-16 16:56:01

Perl构造函数

2010-07-20 15:02:20

Perl数组

2010-07-26 15:17:51

Perl解析XML文件

2010-07-14 12:39:30

Prel字符串

2010-07-13 09:23:00

Perl变量

2010-07-15 10:47:22

Perl命令行

2010-07-14 15:51:30

Perl 字符匹配

2010-07-26 14:06:43

Perl substr

2010-07-19 14:37:01

Perl进程启动函数

2010-07-16 13:45:41

Perl引用

2010-07-19 14:20:57

Perl函数
点赞
收藏

51CTO技术栈公众号