RTTI可以帮助我们在运行时识别对象和类的信息。

一般传统的RTTI有三种实现方式:

1. 向上转型或向下转型(upcasting and downcasting),在java中,向下转型(父类转成子类)需要强制类型转换
2. Class对象(用了Class对象,不代表就是反射,如果只是用Class对象cast成指定的类,那就还是传统的RTTI)
3. instanceof或isInstance()

通过Class对象实现RTTI

interface HasBatteries{}
interface Waterproof{}
interface Shoots{} class Toy{
Toy() {}
Toy(int i) {}
}
class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots {
public FancyToy() {
super(1);
}
} public class ToyTest { public static void printInfo(Class c) {
System.out.println("Class name: " + c.getName() + "\n"
+ "is interface: " + c.isInterface() + "\n"
+ "Simple name: " + c.getSimpleName() + "\n"
+ "Cannonical name: " + c.getCanonicalName()
);
}
public static void main(String[] args) {
Class c = null;
try {
c = Class.forName("FancyToy"); //通过Class提供的forName方法得到FancyToy的Class对象
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("FancyToy类的基本信息:");
printInfo(c);
System.out.println("FancyToy实现的接口的信息:");
for (Class interf : c.getInterfaces()) {
printInfo(interf);
} Class up = c.getSuperclass();
System.out.println("FancyToy的父类的信息:");
printInfo(up);
Toy toy = null;
try {
toy = (Toy)up.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
printInfo(Toy.class); //通过类字面值常量获得Class对象
} } 输出:
FancyToy类的基本信息:
Class name: FancyToy
is interface: false
Simple name: FancyToy
Cannonical name: FancyToy
FancyToy实现的接口的信息:
Class name: HasBatteries
is interface: true
Simple name: HasBatteries
Cannonical name: HasBatteries
Class name: Waterproof
is interface: true
Simple name: Waterproof
Cannonical name: Waterproof
Class name: Shoots
is interface: true
Simple name: Shoots
Cannonical name: Shoots
FancyToy的父类的信息:
Class name: Toy
is interface: false
Simple name: Toy
Cannonical name: Toy
Class name: Toy
is interface: false
Simple name: Toy
Cannonical name: Toy

  

Class还有泛化的,例如: Class<? extends Base>表示所有继承自Base的类的Class对象。

instanceof /Class.isInstance

class Base{}
class Derived extends Base{} public class ToyTest {
public static void test(Object x) {
/*
* instanceof /Class.isInstance 表示左边的是右边的这个类吗?或者是右边这个类的派生类吗?
* == 或者 equal 比较的是确切的类型
*/
System.out.println("Testing x of type : " + x.getClass());
System.out.println("x instance of Base : " + (x instanceof Base));
System.out.println("x instanceof Derived : " + (x instanceof Derived));
System.out.println("Base.isInstance : " + Base.class.isInstance(x));
System.out.println("Derived.isInstance : " + Derived.class.isInstance(x));
System.out.println("x.getClass() == Base.class" + (x.getClass() == Base.class));
System.out.println("x.getClass() == Derived.class" + (x.getClass() == Derived.class));
System.out.println("x.getClass().equals(Base.class)" + (x.getClass().equals(Base.class)));
System.out.println("x.getClass().equals(Derived.class)" + (x.getClass().equals(Derived.class)));
} public static void main(String[] args) {
test(new Base());
test(new Derived());
}
}

  

参考: 《Java编程思想》14章

最新文章

  1. Wakatime 测试工作时间
  2. Flash AS实现时钟效果(全脚本实现)
  3. LR11-更改licence
  4. Ajax解决IE浏览器兼容问题
  5. Scrum Meeting---Seven(2015-11-2)
  6. Kinect开发学习笔记之(一)Kinect介绍和应用
  7. HDU 5046 Airport ( Dancing Links 反复覆盖 )
  8. Solr4.8.0源码分析(25)之SolrCloud的Split流程
  9. asp.mvc获取checkbox、radio、select的值
  10. jQuery 鼠标滚轮插件 jquery.mousewheel.js
  11. python利用selenium和safari浏览器驱动实现新浪微博自动点赞 Demo
  12. Linux下配置tomcat+apr+native应对高并发
  13. Long Long Message(后缀数组)
  14. 用TensorFlow教你手写字识别
  15. exec 命令简单用法 和 find 搭配用法示例
  16. keepalived工作原理和配置文件说明
  17. Erlang的gen_server的terminate()/2未执行
  18. php中memcached的使用
  19. selenium Java-1 配置
  20. [前端随笔][JavaScript] 制作一个富文本编辑器

热门文章

  1. word使用
  2. X-Forwarded-For 负载均衡 7 层 HTTP 模式获取来访客户端真实 IP 的方法(IIS/Apache/Nginx/Tomcat)
  3. c语言的字符串拷贝函数的精简
  4. Integer类之成员变量
  5. Python3 socketserver模块
  6. html5&lt;embed&gt;的完整属性
  7. 用python实现websocket请求遇到的问题及解决方法。
  8. input文本框只能输入数字
  9. 数据仓库基础(八)Informatica 小例子
  10. java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串