Java中RTTI和反射的区别 [英] Difference between RTTI and reflection in Java

查看:47
本文介绍了Java中RTTI和反射的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如何在运行时加载类信息?

My question is when how does the class info gets loaded during runtime?

当有人调用 instanceof 时,这被认为是 RTTI 还是反射?还是看实际情况?

When someone calls instanceof is that considered RTTI or reflection? Or it depends on the actual situation?

推荐答案

术语RTTI" 是一个特定于 C++ 的术语,指的是核心语言的功能,它允许程序在运行时确定各种对象的动态类型.它通常指的是 dynamic_casttypeid 运算符,以及由 typeid 生成的关联 std::type_info 对象.

The term "RTTI" is a C++-specific term referring to the functionality of the core language that allows the program to determine the dynamic types of various objects at runtime. It usually refers to the dynamic_cast or typeid operators, along with the associated std::type_info object produced by typeid.

另一方面,术语反射是跨编程语言使用的通用术语,指的是程序在运行时检查和修改其对象、类型等的能力.

The term reflection, on the other hand, is a generic term used across programming languages to refer to the ability of a program to inspect and modify its objects, types, etc. at runtime.

我听说过的用于 instanceof 的术语是 类型自省instanceof有时被称为对象自省,因为允许程序查看运行的类型来确定什么过程要采取的行动.我认为这是一个比反射更弱的术语,因为它不允许对对象的字段或方法进行详细的内省,但我认为调用 instanceof<的使用在技术上是不正确的/code> 操作符反射.

The term I've heard applied to instanceof is type introspection and instanceof is sometimes referred to as object introspection, as the program is allowed to look at the running types to determine what course of action to take. I think this is a weaker term than reflection, as it doesn't allow for elaborate introspection on the fields or methods of an object, but I don't think it would be technically incorrect to call the use of the instanceof operator reflection.

至于您的另一个问题 - 如何在运行时加载类信息?- 这真的取决于 JVM 实现.ClassLoader 类型最终负责将类加载到系统中,但 JVM 可以根据需要进行解释.我曾经用 JavaScript 构建了一个原型 JVM,在内部所有的反射调用只是查询我准备好的底层 JS 数据结构来表示类、字段和方法.我认为 HotSpot JVM 会做一些完全不同的事情,但它几乎是由实现定义的.

As to your other question - how does class information get loaded at runtime? - that's really up to the JVM implementation. The ClassLoader type is ultimately responsible for loading classes into the system, but the JVM can interpret this however it wants to. I once built a prototype JVM in JavaScript, and internally all reflection calls just queried the underlying JS data structures I had in place to represent classes, fields, and methods. I would imagine that the HotSpot JVM does something totally different, but it's pretty much implementation-defined.

希望这有帮助!

这篇关于Java中RTTI和反射的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆