perl面向对象编程案例

readme:
   本文摘要perl面向对象编程的相关概念,并提供一个完整案例,文中脚本都已调试通过
使用方法:
       在一个文件夹中分别建立oopperl.pl,cocoa.pm,coffee.pm,bean.pm四个文件,并写一个bat文件oopperl.bat

oopperl.bat内容:

@echo off
perl oopperl.pl %*

oopperl.pl文件如下:

#!/usr/local/bin/perl#perl解释器位置

###############面向对象编程###########
=pod
.类是一个Perl包,其中含提供对象方法的类。
.方法是一个Perl子程序,类名是其第一个参数。
.对象是对类中数据项的引用。
面向对象的一个重要特性是继承。Perl中的继承特性与其它面向对象语言不完全一样,
它只继承方法,必须用自己的机制来实现数据的继承。
类中成员的定位形式如:$class'$member与$class::$member相同
每个类都有自己的名字空间和符号名关联数组
构造函数是类的子程序,它返回与类名相关的一个引用。
将类名与引用相结合称为“祝福”一个对象,因为建立该结合的函数名为bless(),其语法为:
    bless YeReference [,classname]
    YeReference是对被“祝福”的对象的引用,classname是可选项,指定对象获取方法的包名,其缺省值为当前包名。
    创建一个构建函数的方法为返回已与该类结合的内部结构的引用,
=cut


=pod
对象和引用的真正区别:
Perl对象被bless以属于某类,引用则不然,
如果引用被bless,它将属于一个类,也便成了对象
对象知道自己属于哪个类,引用则不属于任何类。
=cut


#使用cocoa包创建对象例
##notes:
=pod
1、一定要在构造函数中初始化变量;
2、一定要用my函数在方法中创建变量;
3、一定不要在方法中使用local,除非真的想把变量传递给其它子程序;
4、一定不要在类模块中使用全局变量。 
=cut

push(@INC,'pwd');#将当前目录加到路径寻找列表中供寻找包时使用
use cocoa;        #包含cocoa.pm到解析的源文件拷贝中
####$cup = new cocoa;#创建对象

###创建对象方法:
###$cup = cocoa::new();
###$cup = cocoa->new();

###用哈希表存实例变量
$cup = cocoa::new( 'name' => 'top','x' => 10,'y' => 20 );
print "\nname=$cup->{'name'}\n";
print "\nx=$cup->{'x'}\n";
print "\ny=$cup->{'y'}\n";


###用数组存实例变量
##$cup = cocoa::new( 'cocoa','top',10, 20 );
## print "\nname=$cup->[0]\n";
## print "\nx=$cup->[1]\n";
## print "\ny=$cup->[2]\n";

#$cup->namelist('cocoa'); ##cocoa中虚函数的调用,间接调用,虚方法调用

cocoa::init();###静态方法调用
$cup->init();

=pod
perl中子程序的重载
八、重载
    两个不同的类有同名方法的时候。假设类Espresso和Qava都定义了方法grind,
   可以用::操作符指定使用Qava的方法:
    $mess = Qava::grind("whole","lotta","bags");
    Espresso::grind($mess, "whole","lotta","bags");
    可以根据程序的运行情况来选择使用哪个类的方法,这可以通过使用符号引用去调用来实现:
    $method = $local ? "Qava::" : "Espresso::";
    $cup->{$method}grind(@args);
=cut


##下面演示如何调用继承的方法
push (@INC,'pwd');
use coffee;
$drink = new coffee;
print "\n -------------------- Initial values ------------ \n";
print "coffee: $drink->{'coffee'} \n";
print "Bean: $drink->{'bean'} \n";
print "\n -------------------- Change Bean Type ---------- \n";
$drink->setBeanType('Mixed');
print "Bean Type is now $drink->{'bean'} \n";
print "\n ------------------ Change Coffee Type ---------- \n";
$drink->setCoffeeType('kaka coffee');

print "Type of coffee: $drink->{'coffee'} \n";

#####################
##以下演示多参数方法传递不同数目,值的参数时的调用过程
#####################

#
# With no parameters
#
print "\n Calling with no parameters: \n";
$drink->makeCup;
#
# With one parameter
#
print "\n Calling with one parameter: \n";
$drink->makeCup('1');
#
# With two parameters
#
print "\n Calling with two parameters: \n";
$drink->makeCup(1,'2');
#
# With all three parameters
#
print "\n Calling with three parameters: \n";
$drink->makeCup('1',3,'1');


##################

#######此段脚本用于演示重载###########
$drink->coffee::printType();
$drink->printType();
$drink->bean::printType();
##$drink->SUPER::printType();##在不知道基类名时调用基类方法,但此例并未正确通过


##########################################
##########################################
=pod

十二、Perl类和对象的一些注释
    OOP的最大好处就是代码重用。OOP用数据封装来隐藏一些复杂的代码,Perl的包和模块通过my函数提供数据封装功能,但是Perl并不保证子类一定不会直接访问基类的变量,这确实减少了数据封装的好处,虽然这种动作是可以做到的,但却是个很坏的编程风格。
注意: 
1、一定要通过方法来访问类变量。
2、一定不要从模块外部直接访问类变量。
    当编写包时,应该保证方法所需的条件已具备或通过参数传递给它。在包内部,应保证对全局变量的访问只用通过方法传递的引用来访问。对于方法要使用的静态或全局数据,应该在基类中用local()来定义,子类通过调用基类来获取。有时,子类可能需要改变这种数据,这时,基类可能就不知道怎样去寻找新的数据,因此,这时最好定义对该数据的引用,子类和基类都通过引用来改变该数据。
    最后,你将看到如下方式来使用对象和类:
    use coffee::Bean;
    这句语句的含义是“在@INC数组所有目录的Coffee子目录来寻找Bean.pm”。如果把Bean.pm移到./Coffee目录,上面的例子将用这一use语句来工作。这样的好处是有条理地组织类的代码。再如,下面的语句:
    use Another::Sub::Menu;
    意味着如下子目录树:
    ./Another/Sub/Menu.pm

=cut
############################################
#############################################


=pod

perl5提供了许多有用的预定义模块,可用use导入和no取消
最有用的一些模块:
integer         使用整数运算
Diagnostics   输出较多的诊断信息(警告)
English         允许英文名用作系统变量的别名
Env         导入环境变量的Perl模块
POSIX         POSIX标准(IEEE 1003.1)的Perl接口   
Socket         装载C语言的套接字处理机制

最重要的东东:丰富的资源呀,我们要站在巨人的肩膀上前进咯^.^

CPAN(Comprehensive Perl Archive Network)的Perl文档有其完整的列表。
关于CPAN的更多信息见其网址:http://www.perl.com/perl/CPAN/README.html


=cut

cocoa.pm文件如下:
#
# used for class create example 
# sf.kaka 08122501
#modifications:
#all right reserved by sf.kaka

package cocoa;

###输出方法需要Exporter模块
require Exporter;    ##包含Exporter模块
@ISA = qw(Exporter); ##把Exporter类名加入@ISA数组中以供查找,Perl类的继承是通过@ISA数组实现
                      #,@INC是包含文件的寻找路径。
                     数组含有类(包)名,当一个方法在当前包中未找到时就到@ISA中的包去寻找                    中还含有当前类继承的基类名。

=pod
类中调用的所有方法必须属于同一个类或@ISA数组定义的基类。如果一个方法在@ISA数组中未找到,Perl就到AUTOLOAD()子程序中寻找,这个可选的子程序在当前包中用sub定义。若使用AUTOLOAD子程序,必须用use Autoload;语句调用autoload.pm包。AUTOLOAD子程序尝试从已安装的Perl库中装载调用的方法。如果AUTOLOAD也失败了,Perl再到UNIVERSAL类做最后一次尝试,如果仍失败,Perl就生成关于该无法解析函数的错误
=cut

@EXPORT = qw(init,namelist); ##想输出的类方法


sub init{
   print "\nclass initialization!!\n";
}

=pod
sub new {
my $class = shift;   #获取要求的类名,允许类被继承,以类名作为第一个参数
my $this = {}; # Create an anonymous hash, and #self points to it.
                 #{}创建一个对不含键/值对的哈希表的引用

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; # Connect the hash to the package Cocoa.
$this->init();#简单的调用包内的函数 line24
return $this; # Return the reference to the hash.,
                #从此函数返回后$this引用被销毁,但保存了对该hash表的引用
}
=cut


=pod
构造函数new的参数称为实例变量,实例变量在创建对象的每个实例时用于初始化
可以用匿名哈希表或匿名数组来保存实例变量,也可用数组保存
=cut


#hash table

sub new {
#my $class = shift;   #获取要求的类名,允许类被继承,以类名作为第一个参数

my %para = @_;#实例hash table

my $this = {}; # Create an anonymous hash, and #self points to it.
                 #{}创建一个对不含键/值对的哈希表的引用
$this ->{'name'} = $para{'name'};
$this ->{'x'}    = $para{'x'};
$this ->{'y'}    = $para{'y'};

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;#,$class; # Connect the hash to the package Cocoa.
init();#简单的调用包内的函数 line24
return $this; # Return the reference to the hash.,
                #从此函数返回后$this引用被销毁,但保存了对该hash表的引用
}

#######array to save
=pod
sub new {
my $class = shift;   #获取要求的类名,允许类被继承,以类名作为第一个参数

my @para = @_;#实例hash table

my $this =[]; # Create an anonymous array
                 #{}创建一个对不含键/值对的哈希表的引用
$this ->[0] = $para[0];
$this ->[1]    = $para[1];
$this ->[2]    = $para[2];

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,$class; # Connect the hash to the package Cocoa.
init();#简单的调用包内的函数 line24
return $this; # Return the reference to the hash.,
                #从此函数返回后$this引用被销毁,但保存了对该hash表的引用
}
=cut

=pod
Perl规定方法的第一个参数为对象或其被引用的包。Perl有两种方法:静态方法和虚方法。
静态方法第一个参数为类名,虚方法第一个参数为对象的引用。
方法处理第一个参数的方式决定了它是静态的还是虚的。
静态方法一般忽略掉第一个参数,因为它们已经知道自己在哪个类了,
构造函数即静态方法(本例中演示了两种均OK)。
虚方法通常首先把第一个参数shift到变量self或this中,然后将该值作普通的引用使用

=cut


##虚方法演示
sub namelist{

my $this = shift;
my ($keys ,$value );
while (($key, $value) = each (%$this)) {
print "\t$key is $value.\n";
}

}

1; #perl包的必需条件,否则不会被perl处理


bean.pm文件内容如下:

#
# used for class inheritance
# sf.kaka 08122502
#modifications:
#all right reserved by sf.kaka


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


package bean;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(setBeanType , printType);


####构造函数
sub new {
my $type = shift;
my $this = {};

$this->{'bean'} = 'colombian';
bless $this, $type;
return $this;
}


### This subroutine sets the class name
### setBeanType()用于改变'bean'类型,它使用$class引用获得对对象哈希表的访问
sub setBeanType{
my ($class, $name) = @_;
$class->{'bean'} = $name;
print "Set bean to $name \n";
}

##########此函数用于演示重载
sub printType {
my $class = shift @_;
print "The type of Bean is $class->{'bean'} \n";
}


1; #perl包的必需条件,否则不会被perl处理


coffee.pm文件内容如下:

#
# used for class inheritance
# sf.kaka 08122503
#modifications:
#all right reserved by sf.kaka

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

#
# The Coffee.pm file to illustrate inheritance.
#
package coffee;
require Exporter;
require bean;
@ISA = qw(Exporter, bean);
@EXPORT = qw(setCoffeeType, makeCup,printType);

#
# constructor
#
sub new {
my $type = shift;

my $this = bean->new(); ##this指向bean.pm返回的匿名哈希表的指针,而非在本地创建
                                ##非继承即创建自己的哈希表方法为my $this = {};
$this->{'coffee'} = 'Instant'; # unless told otherwise
bless $this, $type;
return $this;
}

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";
}


#
# set item
#
sub setCoffeeType{
my ($class,$name) = @_;
$class->{'coffee'} = $name;
print "Set coffee type to $name \n";
}

##########此函数用于演示重载
sub printType {
my $class = shift @_;
print "The type of coffee is $class->{'coffee'} \n";
}


1; #perl包的必需条件,否则不会被perl处理

###############################################################################################
###############################################################################################
########           merry christmas & happy new year                          ##################
###############################################################################################
###############################################################################################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值