12.5.3 UNIVERSAL:最终的祖先类:
<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:最终的祖先类:的更多相关文章
- perl 祖先类UNIVERSAL
在perl 面向对象编程里,同其它语言一样存在祖先类.所有类默认继承UNIVERSAL的属性和方法. UNIVERSAL类有几个常用方法can,isa. can可以检查一个对象是否有相应的方法,这个 ...
- 访问祖先类的虚方法(直接访问祖先类的VMT,但是这种方法在新版本中未必可靠)
访问祖先类的虚方法 问题提出 在子类覆盖的虚方法中,可以用inherited调用父类的实现,但有时候我们并不需要父类的实现,而是想跃过父类直接调用祖先类的方法. 举个例子,假设有三个类,实现如下: t ...
- Delphi中获取某类的祖先类及其所在单元名称(使用GetTypeData(PClass.ClassInfo)函数,并且该类是从TPersistent类的派生类才可以这么使用)
前几天在CSDN社区看到一篇<如何得到自身单元名称>的帖子,其中一位名为sdzeng网友给出了答案.受此启发,自己写了一个函数,用来获取指定类的所有祖先类的名称及其所在的单元名称. //参 ...
- firedac数据集控件的公共祖先类——TFDAdaptedDataSet
firedac数据集控件的公共祖先类——TFDAdaptedDataSet TFDQuery = class(TFDCustomQuery)TFDCustomQuery = class(TFDRdbm ...
- 妙味css3课程---1-2、css3中新增的伪类和伪元素有哪些
妙味css3课程---1-2.css3中新增的伪类和伪元素有哪些 一.总结 一句话总结: 1.div:target{}是什么意思? 比如a标签的锚点链接到div,div:target{}就可以找到这个 ...
- 浅谈java中的祖先类Object
首先一道题: public class User{ private String name; private int age; public String getName() { return nam ...
- 2015/12/29 eclipse 设置要点 空间 项目 类 eclipse汉化
开始使用eclipse,双击eclipse.exe文件,启动eclipse.程序会显示一个工作空间的对话框,工作空间用来存放你的项目文件,你可以使用程序默认的,点击确定即可,你也可以重新选择一个文件夹 ...
- android开发之路12(android四大组件&Fragment&AsyncTask类)
一.Activity组件1.简介:Activity组件是Android四大组件之一,通常一个Activity相当于一个用户界面,我们可以通过加载布局文件将Android提供的各种控件及自定义控件显示到 ...
- python多继承中子类访问祖先类的同名成员
子类调用父类的同名成员 方式1: class A: def f_a(self): print("----A----") class B: def f_a(self): print( ...
随机推荐
- octopress command memo
1 rake new_post rake new_post[title] # Begin a new post in source/_posts 2 rake preview ht ...
- B-树和B+树的应用:数据搜索和数据库索引
B-树和B+树的应用:数据搜索和数据库索引 B-树 1 .B-树定义 B-树是一种平衡的多路查找树,它在文件系统中很有用. 定义:一棵m 阶的B-树,或者为空树,或为满足下列特性的m 叉树:⑴树中每 ...
- CSDN排名第一和第二的人
http://blog.csdn.net/phphot http://blog.csdn.net/yuanmeng001
- js面向对象继承
前言 最近看到js面向对象这章节了,主要学习了原型和面向对象继承关系,为了梳理自己的知识逻辑,特此记录. js的面向对象 先说说我目前了解的js创建对象方法 1.写一个函数,然后通过new创建对象 2 ...
- 【Web】CGI与Servlet技术对比
CGI:Common Gateway Interface,通用网关接口. 1.CGI处理步骤 首先,客户端(即Web浏览器)根据某资源的URL向Web服务器提出请求:Web服务器的守护进程(HTTP ...
- C# 方法的调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- mvc中的几个数据传递
1.ViewData对象 ViewBagData是一种字典集合数据同时属于视图基类和控制器基类的属性. 实例: //控制器 public class HomeController:Controller ...
- jQuery中的index方法介绍
从jq api手册摘过来的内容,index这个方法在写 tab silder 之类的组件还是比较有用的说. js没有传统的函数重载的概念,但是根据传入参数的不同,js的函数可以完成不同的功能,也可说是 ...
- VirtualBox中的Ubuntu没有权限访问共享文件夹/media/sf_bak
之前已经搞定可以自动共享文件夹了,但是现在发现无法去访问,非root用户下,使用“ls /media/sf_bak”提示没有权限,当然如果切换到root,是可以的. [解决过程]1.把普通用户名加入到 ...
- 如何去掉IE控件的垂直滚动条(使用QAxWidget加载IE控件)
如果使用MFC的CHtmlView或Qt的QAxWidget加载IE控件,载入html文件后都会自动带一个垂直滚动条,我们不想要这个滚动条,改怎么办呢?搜索了一下“隐藏IE控件滚动条”,发现在 htt ...