1、XXX.pm 文件里面的第一行要是:package  XXX;
2、要有构造函数 sub new,实现如下:
sub new {
my $class = shift; # Get the request class name
my $self = {};
my ($name)=@_;
my $self = {
"name" =>$name
};
bless $self, $class; # Use class name to bless() reference
return $self;
} 3、接着用sub YYY{} 定义自己的函数
例如:
sub setBeanType{
my ($class, $name) = @_;//传进来的第一个参数是类似c++的this指针,第二个才是真正的参数
$class->{'Bean'} = $name;
print "Set bean to $name \n";
} [root@wx03 test]# cat p1.pm
package p1;
use Data::Dumper;
sub new {
my $self = {};
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my ($name)=@_;
my $self = {
"name" =>$name
};
bless $self, $class; # Use class name to bless() reference
print "111111111111111111\n";
$str=Dumper($self);
print "\$str is $str\n";
return $self; }; sub setBeanType{
my ($self, $name) = @_;##//传进来的第一个参数是类似c++的self指针,第二个才是真正的参数
$self->{'Bean'} = $name;
print "Set bean to $name \n";
$str=Dumper($self);
print "\$str is $str\n";
}; 1; [root@wx03 test]# cat p1.pm
package p1;
use base qw(p2);
use Data::Dumper;
sub new {
my $self = {};
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my ($name)=@_;
my $self = {
"name" =>$name
};
bless $self, $class; # Use class name to bless() reference
print "111111111111111111\n";
$str=Dumper($self);
print "\$str is $str\n";
return $self; }; sub setBeanType{
my ($self, $name) = @_;##//传进来的第一个参数是类似c++的self指针,第二个才是真正的参数
$self->{'Bean'} = $name;
print "Set bean to $name \n";
$str=Dumper($self);
print "\$str is $str\n";
}; 1; [root@wx03 test]# cat p1.pl
unshift(@INC,"/root/test");
require p1;
$ua=p1->new('lily');
print "2222222222222222\n";
$str=$ua->setBeanType(scan); [root@wx03 test]# perl p1.pl
111111111111111111
$str is $VAR1 = bless( {
'name' => 'lily'
}, 'p1' ); 2222222222222222
Set bean to scan
$str is $VAR1 = bless( {
'Bean' => 'scan',
'name' => 'lily'
}, 'p1' ); 不断的填充这个对象: 使用基类:
use base 是面向对象编程时,用来描述“基类”的:

perl 面向对象 use base的更多相关文章

  1. Perl面向对象(1):从代码复用开始

    官方手册:http://perldoc.perl.org/perlobj.html 本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象 ...

  2. Perl面向对象(2):对象

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. 已有的代码结构 现在有父类Animal ...

  3. Perl 面向对象编程的两种实现和比较:

    <pre name="code" class="html">https://www.ibm.com/developerworks/cn/linux/ ...

  4. perl面向对象

    来源: http://www.cnblogs.com/itech/archive/2012/08/21/2649580.html Perl面向对象     首先让我们来看看有关 Perl 面向对象编程 ...

  5. Perl面向对象(3):解构——对象销毁

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. perl中使用引用计数的方式管理内存, ...

  6. perl 面向对象编程

    今天看到一个perl面向对象编程的例子,充分体现了如何对数据进行封装: 自己模仿写一个读取配置文件的例子, 配置文件的内容如下 samtools_binary = /usr/bin/samtools ...

  7. perl 面向对象demo

    Vsftp:/root/perl/17# cat Critter.pm package Critter; sub new { my $self = {}; my $invocant = shift; ...

  8. perl 面向对象 new方法

    [root@wx03 test]# cat Scan.pm package Scan; sub new{ my $class = shift; my $self={ 'a'=>11, 'b'=& ...

  9. C# 面向对象的base的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

随机推荐

  1. asp.neti 加密三种方式

    public string Get_MD5_Method1(string strSource) { System.Security.Cryptography.MD5 md5 = new System. ...

  2. js 获取单项复选的值

    html: 单选框-----> 25岁以下 25~35岁 35~50岁 50岁以上 获值 var question1 = $('input:radio[name="radio" ...

  3. OCP-1Z0-053-V13.02-712新题

       Why does the number of blocks for the table remain the sale after the shrink operation? A.Because ...

  4. MongoDB学习笔记1(简介)

    一.简介                 1.丰富的数据类型         MongoDB是一种非关系型数据库,是面向文档的数据库.         MongoDB没有模式,文档的键不会事先定义,也 ...

  5. PHP学习笔记10-图片加水印

    先找好一张图片,更名为face.jpeg,创建watermark.php: <?php /** * Created by PhpStorm. * User: Administrator * Da ...

  6. python网络编程——将IPv4地址转换成不同的格式

    1.将IPv4地址转换为32位二进制格式,用做底层网络函数. import socket from binascii import hexlify def convert_IPv4_address() ...

  7. iOS显示PDF

    使用UIWebView来显示 //locale file NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUT ...

  8. poj 1838

    http://poj.org/problem?id=1838 并查集,,,计算总共个数的模版..... #include <iostream> #define maxn 16006 #in ...

  9. poj 2346 Lucky tickets(区间dp)

    题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...

  10. iOS/Xcode异常:reason = “The model used to open the store is incompatible with the one used to create the store”

    reason=The model used to open the store is incompatible with the one used to create the store 出现上述异常 ...