{$cfg_webname}
主页 > 外文翻译 > 计算机翻译 >

类型识别(含外文出处)

来源:wenku7.com  资料编号:WK712068 资料等级:★★★★★ %E8%B5%84%E6%96%99%E7%BC%96%E5%8F%B7%EF%BC%9AWK712068
以下是资料介绍,如需要完整的请充值下载。
1.无需注册登录,支付后按照提示操作即可获取该资料.
2.资料以网页介绍的为准,下载后不会有水印.资料仅供学习参考之用. 帮助
资料介绍

类型识别(含外文出处)(中文3400字,英文2100字)
作者姓名:Bruce Eckel
文章出处:《Thinking In Java》
摘要:本文主要介绍了Java是如何允许我们在运行期识别对象和类的信息。主要有两种形式:一种是传统RTTI,它假定我们在编译和运行期已经知道了所有类型;另一种是“反射机制(reflection)”,利用它可在运行期获得类的信息。
关键字:Java,RTTI,反射,类型识别
RTTI 语法
Java是通过Class对象实现RTTI机制的,即便我们只是要做诸如类型转换这类的事情。Class类也提供了许多其他方式,以方便我们使用RTTI。
首先必须获得指向适当Class对象的引用。就象前例演示的那样,一种办法是用字符串以及Class.forName()方法。这种做法很方便,因为在获取Class引用时不需要生成该Class类型的对象。然而,如果已经有了一个你感兴趣的类型的对象,那么就可以调用根类Object提供的getClass()方法获取Class引用。它的作用是返回一个特定的Class引用,用来表示对象的实际类型。Class提供了一些有趣且较为有用的方法,从下例为你展现这些方法:

Detecting Types
by Bruce Eckel
Thinking In Java, 3rd ed. Revision 4.0
Abstract
This chapter looks at the ways that Java allows you to discover information about objects and classes at run time. This takes two forms: “Traditional” RTTI, which assumes that you have all the types available at compile time and run time, and the “reflection” mechanism, which allows you to discover class information solely at run time.
RTTI syntax
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.
First, you must get a reference to the appropriate Class object. One way to do this, as shown in the previous example, is to use a string and the Class.forName( ) method. This is convenient because you don’t need an object of that type in order to get the Class reference. However, if you do already have an object of the type you’re interested in, you can fetch the Class reference by calling a method that’s part of the Object root class: getClass( ). This returns the Class reference representing the actual type of the object. Class has many interesting methods demonstrated in the following example:
 

推荐资料