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 ...
随机推荐
- windows版的node.js简单示例
1.下载node.exe放到任意目录,假设E:\nodejs\ 2.在E:\nodejs\下新建helloworld.js,输入以下内容,保存关闭 var http = require('http') ...
- 一个简单二叉树的C++实现(一)
很久没有接触二叉树了,写这个当作练手,接下来会比较详细地实现二叉树的各个功能及应用. /* * BinaryTree.cpp * Author: Qiang Xiao * Time: 2015-07- ...
- 在一个数组中是否存在两个数A、B的和为M
#include <iostream>#include <algorithm>//#include <vector>using namespace std; int ...
- Lake Counting(poj 2386)
题目描述: Description Due to recent rains, water has pooled in various places in Farmer John's field, wh ...
- FormView用法
功能描述: 学生可以对相应学校机构进行投诉建议. form表单 class SuggestForm(forms.Form): TYPE_CHOICES = ( (0, u'学校'), (1, u'学院 ...
- 设计模式多线程方面之Thread-Per-Message 模式
Thread-Per-Message模式是一个很简单但很常应用的模式,尤其是在GUI程式中,我们举个例子,当您设计一个文件编辑器时,您可能像这样注册一个开启档案的事件处理: menuOpenFile ...
- Android抖动动画
//CycleTimes动画重复的次数 public Animation shakeAnimation(int CycleTimes) { Animation translateAnimation = ...
- Java学习之IO之File类一
File的操作 package com.gh.file; import java.io.File; import java.io.IOException; /** * File操作 * @author ...
- C语言中操作符的优先级大全
C语言中操作符的优先级大全, 当然c++, Objective-C,大部分语言都试用. 下面是来自The C Programming Language 2th的总结. OperatorsAssocia ...
- android 时间滚动控件 底部弹出
下载地址:http://download.csdn.net/detail/ldd119/7440895 转载请说明出处 先上个效果图 watermark/2/text/aHR0cDovL2Jsb2cu ...