think in java笔记:RTTI-Runtime type information
2016-08-15 19:35
369 查看
RTTI-Runtime type information
To understand how RTTI works in Java, you must first know how type information isrepresented at run time. This is accomplished through a special kind of object called the
Class object, which contains information about the class. In fact, the Class object is used to
create all of the “regular” objects of your class. Java performs its RTTI using the Class
object, even if you’re doing something like a cast. The class Class also has a number of other
ways you can use RTTI.
译:
为了懂得RTTI是怎么工作的,你必须type信息是如何在运行时候表示的。这个是通过一个特殊的对象Class实现的,这个对象保存着这个class的信息。实际上,Class对象是用来创建所有正常类型的对象。Java通过Class对象来执行RTTI,即使你看起来是在做cast操作。Class这个class还有一些可以使用RTTI的方式。
(class VS Class : class是静态的引用,Class是用在实例化之后getClass之后得到的,两个都可以获取对象的信息.)
It’s important to realize that there’s nothin g magic about reflection. When you’re using
reflection to interact with an object of an unknown type, the JVM will simply look at the
object and see that it belongs to a particular class (just like ordinary RTTI). Before anything
can be done with it, the Class object must be loaded. Thus, the .class file for that particular
type must still be available to the JVM, either on the local machine or across the network. So
the true difference between RTTI and reflection is that with RTTI, the compiler opens and
examines the .class file at compile time. Put another way, you can call all the methods of an
object in the “normal” way. With reflection, the .class file is unavailable at compile time; it is
opened and examined by the runtime environment.
译:
我们需要知道反射是没有很神奇的东西。当你使用反射来于一个不知道类型的object操作时,JVM将会寻找它所对应的class。在任何东西可以做之前,Class对象必须已经加载。即.class文件必须存在,不论是在本地或者远程机器上。所以RTTI和反射不同的地方就是:RTTI是只能反射出来编译时的所有类型,但是反射可以知道实际运行过程中所有的类型。
相关文章推荐
- Thinking in Java笔记:Runtime Type Information
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information
- C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释
- java学习笔记---类型信息(type information)、反射机制与动态代理
- RTTI(Runtime Type Information )
- Runtime Type Information(融贯变通 RTTI)
- 三十三、RTTI(runtime type information)、dynamic_cast运算符、typeid运算符、type_info
- C++中的RTTI(runtime type information) 机制
- Runtime Type Information 运行时类型信息RTTI
- c++对象模型和RTTI(runtime type information)
- C++ typeid 与RTTI(Runtime Type Information)
- C++ - RTTI(RunTime Type Information)运行时类型信息 详解
- Got an exception - java.lang.RuntimeException: Unable to get class information for @throws tag 'XXXException'.
- think in java - 第四章 学习笔记
- Think in java 学习笔记 ---- 阶段 2,3
- Java笔记2 面向对象<5>内部类、静态内部类、内部类定义原则、匿名内部类、异常概述、RuntimeException
- RTTI-Runtime type identification
- think in java第十五章关于网络编程的读书笔记
- 4.[Think in Java笔记]容器深入研究
- (JAVA SE 学习笔记)Java.SE.第003讲.原生数据类型使用陷阱.Pitfall.of.Primitive.Data.Type