大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> CGI专区 -> Perl教学(13)Perl的面向对象编程之五

Perl教学(13)Perl的面向对象编程之五

时间: 2021-07-31 作者:daque

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 item11 #12 sub setcoffeetype{13 my ($class,$name) = @_;14 $class->{'coffee'} = $name;15 print "set coffee type to $name \n";16 }17 #18 # constructor19 #20 sub new {21 my $type = shift;22 my $this = bean->new(); ##### <- look here!!! ####23 $this->{'coffee'} = 'instant'; # unless told otherwise24 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/perl2 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: instantbean: colombian-------------------- change bean type ----------set bean to mixedbean type is now mixed ------------------ change coffee type ----------set coffee type to instanttype 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/perl2 push (@inc,'pwd');3 use coffee;4 $cup = new coffee;5 #6 # with no parameters7 #8 print "\n calling with no parameters: \n";9 $cup->makecup;10 #11 # with one parameter12 #13 print "\n calling with one parameter: \n";14 $cup->makecup('1');15 #16 # with two parameters17 #18 print "\n calling with two parameters: \n";19 $cup->makecup(1,'2');20 #21 # with all three parameters22 #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 cupadd cream==================================calling with two parameters:==================================making a cupadd creamadd 2 sugar cubes==================================calling with three parameters:==================================making a cupadd creamadd 3 sugar cubesmaking some really addictive coffee ;-)================================== 在此例中,因变量makecup()的参数既可为字符串也可为平头,处置截止沟通,你也不妨把这两种典型的数据处置辨别开。在对参数的处置中,不妨树立缺省的值,也不妨按照本质输出参数值的个数赋予各别处置。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.