[书摘]Issues about RTTI (Run Time Type Identification)
来源:互联网 发布:华工网络教育平台v3 编辑:程序博客网 时间:2024/05/30 23:18
class Test2
{
static {
System.out.println("Loading Test2 ...");
}
}
public class Test {
static {
System.out.println("Loading Test ...");
}
public static void main(String[] args)
{
Object[] obj={new Test(),new Test2()};
System.out.println("*************** Using 'instanceof' to judge the type ******************");
for(int i=0;i<obj.length;i++){
if(obj[i] instanceof Test)System.out.println("obj["+i+"]"+" is a Test object"); //named type意思是instanceof后面的参数必须是一个类的名字,而不能是一个实例。
if(obj[i] instanceof Test2)System.out.println("obj["+i+"]"+" is a Test2 object");
}
System.out.println("************* Using method 'isInstance' to judge the type *************");
Class[] cls={
// Class literals:
Test.class,
Test2.class
};
for(int j=0;j<obj.length;j++){
for(int k=0;k<obj.length;k++)
if(cls[k].isInstance(obj[j]))System.out.println("obj["+j+"]"+" is a "+cls[k]+" object");
} //比较而言,Class类的isInstance()方法要灵活得多
}
}
================================================================================
There’s a rather narrow restriction on instanceof: You can compare it to a named type only, and not to a Class object. --------This means, if you use instanceof , you have to list all the class names you wanna compare with.
You can see that the isInstance( ) method has eliminated the need for the instanceof expressions. In addition, this means that you can add new objects simply by changing the obj array; the rest of the program does not need modification (as it did when using the instanceof expressions).
- [书摘]Issues about RTTI (Run Time Type Identification)
- RTTI(Run-Time Type Identification)
- RTTI(Run-time Type Identification)
- ABAP RTTI(Run Time Type Identification)介绍与反射机制
- Lession 26 RTTI (Run Time Type Identification)
- C++ Run-Time Type Identification(RTTI)运行时类型识别
- Java RTTI(Run-Time Type Identification) 运行时类型识别
- c++模拟MFC中运行时类型识别(RTTI)(Run-Time Type Identification)
- RTTI(Run-Time Type Identification,通过运行时类型识别)
- RTTI (Run-Time Type Identification,通过运行时类型识别)
- The C++ Standard Library: RTTI(Run-Time Type Identification,运行时类型识别)
- 从头认识java-12.1 为什么需要RTTI(Run-Time Type Identification)?
- RTTI的魅力(运行时类型识别 Run-time type Identification)
- C++中的Run Time Type Identification
- Advanced Run Time Type Identification in C++ Part II(Property Library An Implementation of RTTI in C++)
- RTTI(Run-Time Type Identification)、虚函数和虚基类的实现方式、开销分析及使用指导
- 行时类型识别(RTTI, Run-Time Type Identification)是Java中非常有用的机制 多态 类加载
- RTTI (Runtime Type Identification)
- 提升進程令牌殺死進程
- VISTA开启半透明效果
- zt ASP学习史上最强的数据分页方法
- windows下配置php+apache+mysql
- Ajax相关资源收集与整理...
- [书摘]Issues about RTTI (Run Time Type Identification)
- [企业管理]国家实验室再遭“人才贫血”困境 [ZT]——青润分析
- 基本控件
- P2P 之 UDP穿透NAT的原理与实现
- 英语学习每天一点
- 窗口过程,窗口过程函数
- windows xp自动更新失败的原因!
- ListView与DataSet的捆绑
- 从MDF文件恢复Sql Server2000数据库