perl 面向对象 use base
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的更多相关文章
- Perl面向对象(1):从代码复用开始
		官方手册:http://perldoc.perl.org/perlobj.html 本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象 ... 
- Perl面向对象(2):对象
		本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. 已有的代码结构 现在有父类Animal ... 
- Perl 面向对象编程的两种实现和比较:
		<pre name="code" class="html">https://www.ibm.com/developerworks/cn/linux/ ... 
- perl面向对象
		来源: http://www.cnblogs.com/itech/archive/2012/08/21/2649580.html Perl面向对象 首先让我们来看看有关 Perl 面向对象编程 ... 
- Perl面向对象(3):解构——对象销毁
		本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. perl中使用引用计数的方式管理内存, ... 
- perl 面向对象编程
		今天看到一个perl面向对象编程的例子,充分体现了如何对数据进行封装: 自己模仿写一个读取配置文件的例子, 配置文件的内容如下 samtools_binary = /usr/bin/samtools ... 
- perl 面向对象demo
		Vsftp:/root/perl/17# cat Critter.pm package Critter; sub new { my $self = {}; my $invocant = shift; ... 
- perl 面向对象 new方法
		[root@wx03 test]# cat Scan.pm package Scan; sub new{ my $class = shift; my $self={ 'a'=>11, 'b'=& ... 
- C# 面向对象的base的使用
		using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ... 
随机推荐
- VPN连接在遇到飞鱼星设备时可能出现的疑难问题
			在连接VPN设备时,设置都是正常的.在 
- [译]Stairway to Integration Services Level 9 - Control Flow Task Errors
			介绍 在本文中,我们会实验 MaximumErrorCount和ForceExecutioResult 故障容差属性,并且还要学习Control Flow task errors, event han ... 
- Onvif协议
			ONVIF致力于通过全球性的开放界面标准来推进网络视频在安防市场的应用,这一接口界面标准将确保不同厂商生产的网络视频监控产品具有互通性.2008年11月,论坛正式发布了ONVIF第一版规范ONVIF核 ... 
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )
			BFS... -------------------------------------------------------------------------------------------- ... 
- C++ Input & Output
			1.C++ I/O各类之间的继承关系图 参考网址: http://www.cplusplus.com/reference/iolibrary/ Note: 在程序中包含iostream文件将自动创建8 ... 
- 最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)
			Dijkstra算法 ———————————最后更新时间:2011.9.25———————————Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径. ... 
- c++ 实现将数字转换为中文数字输出
			实现如下函数: void printInChinese(int num); 这个函数输入一个小于100000000(一亿)的正整数,并在屏幕上打印这个数字的中文写法. 例如: 17 -> 一十七 ... 
- Gartner 认可 Microsoft 为应用程序平台即服务的领导者
			对于 Windows Azure 而言,2013 年是了不起的一年.客户使用量每月都创新高:4 月份 Windows Azure 基础结构服务一经正式发布即受到前所未有的青睐,成为重要的里程碑.Gar ... 
- Codeforces Round #200 (Div. 2) C. Rational Resistance
			C. Rational Resistance time limit per test 1 second memory limit per test 256 megabytes input standa ... 
- 复习知识点:GCD多线程
			GCD的基础 #pragma mark - 使用GCD 创建一个 串行 队列 // 第一种:系统提供的创建串行队列的方法 // 在真正的开发中如果需要创建串行队列,比较习惯用这种 // dispatc ... 
