<pre name="code" class="html">12.5.3 UNIVERSAL:最终的祖先类:

你可以把 UNIVERSAL 看作最终的祖先,所 有类都隐含地从它衍生而来。

INVOCANT->isa(CLASS)

如果 INVOCANT 的类是 CLASS 或者任何从 CLASS 继承来的,isa 方法返回真。 除了包名字以外,CLASS还
可以是一个内建的类型,比如 "HASH" 或者 "ARRAY"。 (准确地检查某种类型在封装和多态性机制中并不能很好
地工作。你应该依赖重 载分检给你正确的方法。) [root@wx03 test]# cat t9.pl
unshift(@INC,"/root/test");
use Horse;;
if (Horse->isa("Critter")) {
print "true.\n";
}
[root@wx03 test]# perl t9.pl
true. [root@wx03 test]# cat t9.pl
unshift(@INC,"/root/test");
use Horse;;
if (Horse->isa("Horse")) {
print "true.\n";
} [root@wx03 test]# perl t9.pl
true. INVOCANT->isa(CLASS) 对应的if (Horse->isa("Horse")) 如果INVOCANT 的类是CLASS或者任何从CLASS继承来的,isa方法返回真。除了包名字以外,CLASS还可以是一个内建的类型 比如HASH 或者ARRAY [root@wx03 test]# cat t10.pl
unshift(@INC,"/root/test");
use Horse;;
if (Horse->isa("Critter")) {
print "true.\n";
}
[root@wx03 test]# perl t10.pl
true. 判断Horse类是否继承("Critter")) 类 isa(CLASS)
如果调用 "isa" 的对象是隶属于 "CLASS" 或者它的子类, 那么 "isa" 返回
*真值*。 你也可以用传递两个参数的办法直接调用 "UNIVERSAL::isa":第一个参数是
一个对象(甚至是普通的引用),这个办法可以用来检查一个对象是不是属于
指定的类型。例如:
if(UNIVERSAL::isa($ref, 'ARRAY')) {
#...
} [root@wx03 test]# cat Horse.pm
package Horse;
our @ISA = "Critter";
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
color => "bay",
legs => 4,
owner => undef,
@_, # 覆盖以前的属性
};
return bless $self, $class;
};
sub sum1 {
$self=shift;
my $a=shift;
my $b=shift;
return $a + $b + 7;
}; our @arr=qw/1 2 3 4 5 6 7/;
our %h1=(1,2,3,4,5,6,7,8);
1; unshift(@INC,"/root/test");
use Horse;;
$ua=Horse->new();
if (UNIVERSAL::isa($ua,'HASH')) {
print "true.\n"; [root@wx03 test]# perl t10.pl
true. 判断 $ua是不是HASH 判断一个引用是不是被bless过的: [root@wx03 test]# perl t10.pl
It's an object
[root@wx03 test]#
[root@wx03 test]#
[root@wx03 test]# cat t10.pl
unshift(@INC,"/root/test");
use Horse;;
$ua=Horse->new(); print "It's an object\n" if UNIVERSAL::isa($ua, 'UNIVERSAL'); [root@wx03 test]# perl t10.pl
It's an object 注释掉bless
[root@wx03 test]# cat Horse.pm
package Horse;
our @ISA = "Critter";
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
color => "bay",
legs => 4,
owner => undef,
@_, # 覆盖以前的属性
};
#return bless $self, $class;
return $self;
};
sub sum1 {
$self=shift;
my $a=shift;
my $b=shift;
return $a + $b + 7;
}; our @arr=qw/1 2 3 4 5 6 7/;
our %h1=(1,2,3,4,5,6,7,8);
1;
[root@wx03 test]# perl t10.pl
[root@wx03 test]# 没有返回 [root@wx03 test]# cat t10.pl
unshift(@INC,"/root/test");
use Horse;;
$ua=Horse->new(); #print "It's an object\n" if UNIVERSAL::isa($ua, 'UNIVERSAL');
$code=Horse->can(sum1); use Data::Dumper;
$str=Dumper($code);
print "\$str is $str\n"; [root@wx03 test]# perl t10.pl
$str is $VAR1 = sub { "DUMMY" }; 如果 INVOCANT 中有 METHOD,那么 can 方法就返回一个可以调用的该子过程的 引用。如果没有定义这样的
子过程,can 返回 undef。 12.5.3 UNIVERSAL:最终的祖先类: 在 UNIVERSAL 类里面有下面的预定义的方法可以使用,因此所有类中都可以用它们。而且 不管它们是被当作类
方法还是对象方法调用的都能运行。 INVOCANT->isa(CLASS) 如果 INVOCANT 的类是 CLASS 或者任何从 CLASS 继承来的,isa 方法返回真。 [root@wx03 test]# cat t12.pl
unshift(@INC,"/root/test");
use Horse;;
if (Horse->isa("Critter")) {
print "true.\n";
}
[root@wx03 test]# perl t12.pl
true. [root@wx03 test]# cat t12.pl
unshift(@INC,"/root/test");
use Horse;;
if (Horse->isa("Horse")) {
print "true.\n";
}
[root@wx03 test]# perl t12.pl
true. [root@wx03 test]# cat t12.pl
unshift(@INC,"/root/test");
use Horse;;
if (Horse->isa("Horse1")) {
print "true.\n";
}
[root@wx03 test]# perl t12.pl
[root@wx03 test]# [root@wx03 test]# cat t13.pl
unshift(@INC,"/root/test");
use Horse;;
$ua=Horse->new();
if (UNIVERSAL::isa($ua,'HASH')) {
print "true.\n";
};
[root@wx03 test]# perl t13.pl
true. 检测$ua是否是hash [root@wx03 test]# cat t13.pl
unshift(@INC,"/root/test");
use Horse;;
$ua=Horse->new();
if (Horse->can("sum1")) {
print "Our invocant can copy.\n";
}
[root@wx03 test]# perl t13.pl
Our invocant can copy. 如果 INVOCANT 中有 METHOD,那么 can 方法就返回一个可以调用的该子过程的 引用。如果没有定义这样的
子过程,can 返回 undef。
												

12.5.3 UNIVERSAL:最终的祖先类:的更多相关文章

  1. perl 祖先类UNIVERSAL

    在perl 面向对象编程里,同其它语言一样存在祖先类.所有类默认继承UNIVERSAL的属性和方法. UNIVERSAL​类有几个常用方法can,isa. can可以检查一个对象是否有相应的方法,这个 ...

  2. 访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)

    访问祖先类的虚方法 问题提出 在子类覆盖的虚方法中,可以用inherited调用父类的实现,但有时候我们并不需要父类的实现,而是想跃过父类直接调用祖先类的方法. 举个例子,假设有三个类,实现如下: t ...

  3. Delphi中获取某类的祖先类及其所在单元名称(使用GetTypeData(PClass.ClassInfo)函数,并且该类是从TPersistent类的派生类才可以这么使用)

    前几天在CSDN社区看到一篇<如何得到自身单元名称>的帖子,其中一位名为sdzeng网友给出了答案.受此启发,自己写了一个函数,用来获取指定类的所有祖先类的名称及其所在的单元名称. //参 ...

  4. firedac数据集控件的公共祖先类——TFDAdaptedDataSet

    firedac数据集控件的公共祖先类——TFDAdaptedDataSet TFDQuery = class(TFDCustomQuery)TFDCustomQuery = class(TFDRdbm ...

  5. 妙味css3课程---1-2、css3中新增的伪类和伪元素有哪些

    妙味css3课程---1-2.css3中新增的伪类和伪元素有哪些 一.总结 一句话总结: 1.div:target{}是什么意思? 比如a标签的锚点链接到div,div:target{}就可以找到这个 ...

  6. 浅谈java中的祖先类Object

    首先一道题: public class User{ private String name; private int age; public String getName() { return nam ...

  7. 2015/12/29 eclipse 设置要点 空间 项目 类 eclipse汉化

    开始使用eclipse,双击eclipse.exe文件,启动eclipse.程序会显示一个工作空间的对话框,工作空间用来存放你的项目文件,你可以使用程序默认的,点击确定即可,你也可以重新选择一个文件夹 ...

  8. android开发之路12(android四大组件&Fragment&AsyncTask类)

    一.Activity组件1.简介:Activity组件是Android四大组件之一,通常一个Activity相当于一个用户界面,我们可以通过加载布局文件将Android提供的各种控件及自定义控件显示到 ...

  9. python多继承中子类访问祖先类的同名成员

    子类调用父类的同名成员 方式1: class A: def f_a(self): print("----A----") class B: def f_a(self): print( ...

随机推荐

  1. 高质量程序设计指南C/C++语言——C++/C常量

  2. java:字符串的“+”运算

    今天在一篇博客里,意外的看到了一段关于java中对字符串的“+”运算的处理(博客原文:http://blog.csdn.net/yirentianran/article/details/2871417 ...

  3. [lua]笔试-按字典序列出指指定的序列的位置

    计算方法: n的阶乘记为f(n), s为输入序列, sub(i)为s的i到n的子序列.A(i)为第i位对应的字母在子序列sub(i)中的字典顺序 N(s) = sum_{1,n} T(i)*(A(i) ...

  4. Spring Boot 属性配置和使用(转)

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  5. Mac OS X10.9安装的Python2.7升级Python3.3步骤详解

    Mac OS X10.9默认带了Python2.7,不过现在Python3.3.3出来了,如果想使用最新版本,赶紧升级下吧.基本步骤如下 第1步:官网下载Python3.3 这里面有windows和m ...

  6. 「数据结构」:模拟指针(simulated pointer)

    模拟指针,也就是清华严老师<数据结构-C语言描述>中的静态链表,静态链表的引用是使用一段连续的存储区还模拟指针的功能,可以有效的利用一段连续内存进行一定范围内可变的子链表的空间分配,此数据 ...

  7. c++11 stl atomic_flag 样例

    Author:DriverMonkey Mail:bookworepeng@Hotmail.com Phone:13410905075 QQ:196568501 測试环境:Win7 64 bit 编译 ...

  8. Js基础操作

    var a="zhangsan"; document.write(a+":I love JavaScrip"); a="lisi"; doc ...

  9. 嵌入式:nfs挂载开发板的几个陷阱

    1. host没有设置好,这个比较容易排查到. 开启portmap帮助网络应用程序找到正确的通讯端口: 开启nfs-kernel-server服务: 开启设置要export出去的服务目录. sudo ...

  10. 新安装的linux(linux mint 或则ubuntu)系统中安装postgresql-xc安装的包

    一:./configure的时候1,gcc的处理:sudo apt-get install clang && rvm install 1.9.3 --with-gcc=clang2,缺 ...