您的位置:首页 > 编程语言 > Java开发

Java编程思想 - 类型信息与反射机制

2015-07-07 20:40 716 查看
首先介绍一个本文后面会频繁提到的概念:RTTI(Runtime Type Information,或者,Run-Time Type Identification),运行时类型信息。简单来说,就是指程序能够在运行时发现和使用类型信息。

RTTI能做什么??它解放了程序在编期间执行的面向类型的操作,不管是程序的安全性还是可扩展性和可维护性,都得到了大大的加强。

我们一般使用二种方式来实现运行时识别对象和类的信息:“传统的”RTTI和“反射”机制。

、一个大家都熟悉的例子

传统的RTTI假定我们在编译时已经知道了所有的信息。

下面我们来看一看一个很熟悉的例子:

Java代码


<span style="font-size: small;">package cn.OOP.Typeinfo;

import java.util.Arrays;

import java.util.List;

abstract class Student{

void study(){ System.out.println(this+".study()");}

abstract public String toString();

}

class PrimaryStudent extends Student{

public String toString(){ return "PrimaryStudent";}

}

class HighSchoolStudent extends Student{

public String toString(){ return "HighSchoolStudent";}

}

class UniversityStudent extends Student{

public String toString(){ return "UniversityStudent";}

}

public class Students {

public static void main(String args[]){

List<Student> studentList = Arrays.asList(

new PrimaryStudent(),new HighSchoolStudent(),new UniversityStudent()

);

for(Student s : studentList){

s.study();

}

}

}

/* output:

PrimaryStudent.study()

HighSchoolStudent.study()

UniversityStudent.study()

*///

</span>

基类Studeng中包含study方法,通过传递this参数给System.out.println()方法,间接使用toString()来打印类标识符。这里,toString()被声明为abstract,以此强制子类覆写该方法,并可以防止无格式的Student的实例化。

输出结果反映,子类通过覆盖toString方法,study()方法在不同的情况下会有不同的输出(多态)。

而且,在将Student对象放入List<Student>的数组时,对象被向上转型为Student类,但同时也丢失了Student对象的具体类型信息:对于程序而言,如果我们不对数组内的对象进行向下转型,那么,他们“只是”Student对象。

在上述例子中,还有一个地方用到了RTTI。容器List将它持有的对象都看做Object对象来处理。当我们从数组中取出对象时,对象被转型回Student类型。这是最基本的RTTI形式,因为在Java中,所有的类型转换都是在运行才进行正确性检查的。

还有一点,例子中的RTTI类型转换并不彻底:Object对象被转型为Student,而不是UStudent、PStudent、HStudent。这是因为程序只知道数组中保存的是Student,在编译时Java通过容器和泛型来确保这一点,而在运行时就由转型来实现。

例子很简单,但说明的东西很多。

二、一个特殊的编程问题

在现实当中,当我们能够知道某个泛化引用的确切类型的时候,我们可以方便快捷的解决它,怎么办??

比如:我们扫描某个区域的生物(animal),并将之装入一个数组。当用户要突出的显出其中的某一类,如人类,的时候,系统是并不容易判断的。因为,对于程序而言,数组中的对象都是aniaml。使用RTTI,可以查询animal引用的确切类型,然后选择或者剔除特例。

三、Class对象

Class对象是RTTI在Java中工作机制的核心。

我们知道,Java程序是由一个一个的类组成的。而对于每一个类,都有一个class对象与之对应。也就是说,每编译一个新类都会产生一个class对象(事实上,是这个class对象时被保存在同名和.class文件当中的)。这个过程涉及到类的加载,不在本文的内容之内。

无论何时,只要我们想要在运行时使用类型信息,就必须获得恰当的class对象的引用。

常规来讲,class对象有三种获得方式,而且,它包含很多有用的方法。看下面的程序:

Java代码


package cn.OOP.Typeinfo;

interface Drinkable{}

interface Sellable{}

class Coke{

Coke(){} //运行这个程序后,注释掉这个默认的无参构造器再试一试

Coke(int i ){}

}

class CocaCola extends Coke

implements Drinkable,Sellable{

public CocaCola() { super(1);}

}

public class TestClass {

static void printinfo(Class c){

System.out.println("Class Name:"+c.getName()+" is interface? ["+

c.isInterface()+"]");

System.out.println("Simple Name:"+c.getSimpleName());

System.out.println("Canonical Name:"+c.getCanonicalName());

}

public static void main(String args[]){

Class c= null;

try{

c = Class.forName("cn.OOP.Typeinfo.CocaCola");

//or we can init c in this way

// CocaCola cc = new CocaCola();

// c = cc.getClass();

//we can also init c in this way

// c = CocaCola.class;

}catch(ClassNotFoundException e){

System.out.println("Can't find CocaCola!!");

System.exit(1);

}

printinfo(c);

for(Class face : c.getInterfaces()){

printinfo(face);

}

}

}/* output:

Class Name:cn.OOP.Typeinfo.CocaCola is interface? [false]

Simple Name:CocaCola

Canonical Name:cn.OOP.Typeinfo.CocaCola

Class Name:cn.OOP.Typeinfo.Drinkable is interface? [true]

Simple Name:Drinkable

Canonical Name:cn.OOP.Typeinfo.Drinkable

Class Name:cn.OOP.Typeinfo.Sellable is interface? [true]

Simple Name:Sellable

Canonical Name:cn.OOP.Typeinfo.Sellable

*///

CocaCola类继承自Cola类并实现了Drinkable、Sellable接口。在mian方法中,我们用forName()方法创建了一个

Class对象的引用。需要注意的是,forName()方法传入的参数必须是全限定名(就是包含包名)

在printinfo()方法中,分别使用getSimpleName()和getCanonicalName()来打印出不含包名的类名和全限定的类名。isInterface()很明显,是得到这个class对象是否表示一个接口。虽然,我们这里只是演示了class对象的3种方法,但实际上,通过class对象,我们发现我们能够了解到类型的几乎所有的信息(之所以是“几乎”是因为我们有时候并不需要客户了解我们提供的类的某些信息,而选择性的屏蔽。大家可以试一试用class对象查询某些类的private属性!)

例子有出现了三种不同的获得class对象的方法:

Class.forName():最简单的,也是最快捷的方式,因为我们并不需要为了获得class对象而持有该类的对象实例。

obj.getClass():当我们已经拥有了一个感兴趣的类型的对象时,这个方法很好用。

Obj.class : 类字面常量,这种方式很安全,因为它在编译时就会得到检查(因此不需要放到try语句块中),而且高效。

我们可以根据我们的程序的条件和需要,选择上面三种方式中的任何一种来实现RTTI。

四、泛化的Class引用

通过上面的例子我们可以很容易的知道,class引用表示的是它所指向的对象的确切类型,并且,通过class对象我们

能获取特定类的几乎所有信息。这很容易理解。

但是,Java的设计者并不止步于此。通过泛型,我们可以让class引用所指向的类型更加具体。

package cn.OOP.Typeinfo;

Java代码


public class GenericClassReference {

public static void main(String args[]){

Class intClass = int.class;

Class<Integer> genericIntClass = int.class;

genericIntClass = Integer.class; //same thing

intClass = double.class;

// genericIntClass = double.class; //Illegal,<span style="font-size: xx-small;"><span class="hps" style="color: #333333; font-family: arial, sans-serif; white-space: normal; background-color: #f5f5f5;">Can not</span><span style="color: #333333; font-family: arial, sans-serif; white-space: normal; background-color: #f5f5f5;"> </span><span class="hps" style="color: #333333; font-family: arial, sans-serif; white-space: normal; background-color: #f5f5f5;">compiled</span></span>

}

}

看这个例子,普通的Class引用intClass能被随意赋值指向任意类型,但是,使用了泛型之后,编译器会强制对class引用的重新赋值进行检查。

但这种泛型的使用与普通的泛型又是不同的,比如下面这条语句:

Class<Number> c = int.class;

乍一看,没什么不对,Integer继承自Number类,不就是父类引用指向子类对象么。但实际上,这句代码会在编译时就出错。因为Integer的class对象引用不是Number的Class引用的子类。这看起来很诡异,但却是事实。

事实上,正确的做法是使用通配符?。看下面:

package cn.OOP.Typeinfo;

Java代码


public class WildClassReference {

public static void main(String args[]){

Class<?> intClass = int.class; //? means everything

intClass = double.class;

Class<? extends Number> longClass = long.class;

longClass = float.class; //Compile Success

}

}

通配符?表示“任何类”,所以intClass能够重新指向double.class。同时? extends Number 表示任何Number类的子类。

五、反射:RTTI实现和动态编程

就上面的例子我们看到,RTTI可以告诉你所有的你想知道的类型信息,前提是这个类型是在编译时候已知的。

这好像没有什么不对吧??

将眼界放开点:假设程序获取了一个我们程序空间以外的对象的引用。即编译时并不存在的类。例如:从本地硬盘,从网络。要知道,Java的一大优势就是适于现在的WEB环境!!!

看下面:

package cn.OOP.Typeinfo;

Java代码


import java.util.Scanner;

public class Reflection {

public static void main(String args[]){

Class c = null;

Scanner sc = new Scanner(System.in);

System.out.println("Please put the name of the Class you want load:");

String ClassName = sc.next();

try {

c = Class.forName(ClassName);

System.out.println("successed load the Class:"+ClassName);

} catch (ClassNotFoundException e) {

System.out.println("Can not find the Class ACommonClass");

System.exit(1);

}

}

}

当我们运行这个程序的时候,系统会阻塞在这一步:String ClassName = sc.next();

这时的输出是:

Please put the name of the Class you want load:

然后,由我们给定一个类名。假定,我们需要输入一个我们自定义的类:ACommonClass。注意,这是关键。这个时候,我们并没有开始写这个类,更没有编译这个类,也就没有对应的.class文件。

这个时候,写第二个程序(类)

package cn.OOP.Typeinfo;

Java代码


public class ACommonClass {

//I am just a generic Class

}

编译这个类,得到此类的.class 文件。然后,在程序一(注意,这个程序一直没有停止,一直在运行)输入类名:

cn.OOP.Typeinfo.ACommonClass,阻塞停止,打印输出。

最终结果:

Please put the name of the Class you want load:

Java代码


cn.OOP.Typeinfo.ACommonClass

successed load the Class:cn.OOP.Typeinfo.ACommonClass

在这个例子中,我们看到了一个和以前传统的编程截然不同的东西:在程序运行时,我们还能云淡风轻的写着程序必须的类,而且,这个类还能用于这个已经开始的程序!!!!神奇吧。

当然,这只是一个最简单的RTTI反射应用,通常的Java动态编程会更复杂,也更神奇!!

Class类与java.lang.reflect类库一起对反射机制提供了支持。当通过反射与一个未知类型的对象打交道时,JVM只是简单的检查这个对象,看他属于哪个特定的类(就像传统的RTTI一样)。当我们用反射机制做某些事情时,我们还是必须知道特定的类(也就是必须得到.class文件),要么在本地,要么从网络获取。所不同的是,由于设计体系的特殊,我们逃避了在编译期间的检查,知道运行时猜打开和检查.class文件。

未完待续!!!!

等会更新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: