如果你属于初级学习者,当你看类反射之后,说明你的编程能力又升级了!
什么是类反射:
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反射机制。
反射(Reflection)是Java程序开发语言的特征之一,它允许运行中的Java程序对自身进行检查, 也称自审,并能直接操作程序的内部属性。例如,使用它能获得Java类中各成员的名称并显示出来。
Java的这一能力在实际应用中应用得很多,在其它的程序语言中根本就不存在这一牲。例如,Pascal、C或者C++中就没有办法在程序中获得函数定义相关的信息。
JavaBean是类反射的实际应用之一,它能让一些工具可视化的操作软件组件。这些工具通过类反射动态的载入并取得Java组件(类)的属性。后面学习的各种框架,基本上都会有反射的使用。
首先举个例子:
api
<span style="font-size:18px;">/** * */ package cn.hncu.reflect.test1.api; /** * @author xinxin * */ public interface Intfaceebo { public void work(); }</span>UsbFactory
<span style="font-size:18px;">/** * */ package cn.hncu.reflect.test1.Factory; import java.io.File; import java.io.FileInputStream; import java.util.Properties; import cn.hncu.reflect.test1.api.Intfaceebo; import cn.hncu.reflect.test1.imp.Imp; /** * @author xinxin * */ public class USbFatory { public static Intfaceebo getUsb(){ // S<span style="color:#000099;">tring name1 ="cn.hncu.reflect.test1.imp.Imp"; // String name2 ="cn.hncu.reflect.test1.imp.Imp2";</span> try { // 通过修改 name 就可以调用不同的类,实现了 解耦 //name 不需要依靠类了,只需要路径名就可以获得该类的所有信息 <span style="color:#ff0000;">//此处手动改麻烦,还可以 过修改属性文件(这样写就比较大了,当然修改属性可以用界面,让用户自己进行修噶</span>) Properties p =new Properties(); File file =new File("properties.cog"); if(!file.exists()){ file.createNewFile(); } FileInputStream files =new FileInputStream(file);//配置文件需要 IO流 p.load(files);//读文件中的数据 <span style="color:#ff0000;">String name = p.getProperty("name").trim();//从配置文件中取出来 Class clazz=Class.forName(name);//字符串传过来的 进行类反射 Intfaceebo c=(Intfaceebo) clazz.newInstance();//获得类反射之后的对象</span> return c; } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { //1, //String name1 ="cn.hncu.reflect.test1.imp.Imp"; //(当Class clazz=Class.forName(name1);)work1 is Working // USbFatory.getUsb().work();//运行结果 work1 is Working // 2,String name2 ="cn.hncu.reflect.test1.imp.Imp2"; //Class clazz=Class.forName(name2); // USbFatory.getUsb().work();// work2 is Working // 采用配置文件写之后 USbFatory.getUsb().work(); //name =cn.hncu.reflect.test1.imp.Imp //work1 is Working // name =cn.hncu.reflect.test1.imp.Imp2 //work2 is Working } } </span>
<span style="font-size:18px;">/** * */ package cn.hncu.reflect.test1.imp; import cn.hncu.reflect.test1.api.Intfaceebo; /** * @author xinxin * */ public class Imp implements Intfaceebo { public void work() { System.out.println("work1 is Working"); } } </span>imp2
<span style="font-size:18px;">/** * */ package cn.hncu.reflect.test1.imp; import cn.hncu.reflect.test1.api.Intfaceebo; /** * @author xinxin * */ public class Imp2 implements Intfaceebo { public void work() { System.out.println("work2 is Working");//仅仅只是测试使用 } }</span>
反射使用的三个步骤 :
如:method
第一步:获得你想操作的类的java.lang.Class对象。在运行中的Java程序中,用java.lang.Class类来描述类和接口等。
第二步:调用诸如getDeclaredMethods的方法,取得该类中定义的所有方法的列表。
第三步:使用反射的API来操作这些信息。
自己写的Instance 测试类
1,首先需要一个对象
/** * */ package cn.hncu.reflect.test; /** * @author xinxin * */ public class A { },2,进行判断(也就是调用Class. isInstance(),运行是都匹配 )
/** * */ package cn.hncu.reflect.test; /** * @author xinxin * */ public class TestInstanceof { /** * @param args */ /* * 验证 instanceof 功能 通过这个函数isInstanceof * 通过类反射Class 中的isInstance 判断下 */ public static void main(String[] args) { <span style="color:#ff0000;">System.out.println( isInstanceof(new A()));//结果 true System.out.println( isInstanceof(new Integer(10)));// false</span> } private static boolean isInstanceof( Object obj) { boolean isT=false; try { <span style="color:#ff9900;">Class c =Class.forName("cn.hncu.reflect.test.A");//通过传过来的字符串,获得Class类型 isT=c.isInstance(obj);</span> } catch (ClassNotFoundException e) { e.printStackTrace(); } return isT; } }
1,通过对象的getClass方法进行获取。这种方式需要具体的类和该类的对象,以及调用getClass方法。
2, 任何数据类型(包括基本数据类型)都具备着一个静态的属性class,通过它可直接获取到该类型对应的Class对象。这种方式要使用具体的类,然后调用类中的静态属性class完成,无需调用方法,性能更好。
3,通过Class.forName()方法获取。这种方式仅需使用类名,就可以获取该类的Class对象,更有利于扩展。
实例演示:
/** * */ package cn.hncu.reflect.Three; import java.lang.reflect.Method; /** * @author xinxin * */ public class ReflectThree { /** * @param args */ //类反射必须先有类,导入Person //这仅仅是获得Class 对象 public static void main(String[] args) { // getObject1();//通过对象的getClass方法进行获取。 //、这种方式需要具体的类和该类的对象,以及调用getClass方法。 //2,任何数据类型(包括基本数据类型)都具备着一个静态的属性class, // 通过它可直接获取到该类型对应的Class对象。这种方式要使用具体的类, // 然后调用类中的静态属性class完成,无需调用方法,性能更好。 // getObject2(); //3,通过Class.forName()方法获取。这种方式仅需使用类名, // 就可以获取该类的Class对象,更有利于扩展。、 // getObject3(); //4 小小的先实验一下 获得类反射出的方法 getMethod(); } <span style="color:#ff0000;">//1,已知具体的类 </span> private static void getObject1() { Person p =new Person("jack", 11); Class c =<span style="color:#ff0000;">p.getClass()</span>;//获得Class 对象 System.out.println(c);//class cn.hncu.reflect.Three.Person } //任何对象包括基本数据类型都有.class 获得Class 对象 private static void getObject2() { Class c =<span style="color:#ff0000;">String.class</span>; System.out.println(c);//class java.lang.String } /* <span style="color:#ff0000;">* 通过Class.forName(str)的方式获取Class对象, * 该方式依赖的是字符串(类的名字),可以实现解耦</span> */ private static void getObject3() { String name ="cn.hncu.reflect.Three.Person"; try { <span style="color:#ff0000;">Class c =Class.forName(name)</span>; System.out.println(c);//class cn.hncu.reflect.Three.Person } catch (ClassNotFoundException e) { e.printStackTrace(); } } private static void getMethod() { String name ="cn.hncu.reflect.Three.Person"; try { Class c =Class.forName(name); // Object obj=c.newInstance();// 创建此 Class 对象所表示的类的一个新实例。 Method method[] =c.getMethods();//获得包括本身,父类的所有public方法 // Method method[] =c.getDeclaredMethods();//获得当前类的所有方法包括 private 等 for(int i=0;i<method.length;i++){ System.out.println(method[i]); } } catch (Exception e) { e.printStackTrace(); } } }
前面的基础基本介绍完了,现在开始解刨得到类对象之后:
获取类的方法:找出一个类中定义了些什么方法,这是一个非常有价值也非常基础的反射用法。
代码演示:首先需要一个未知的类(USerModel*(在做书店管理系统时用到的,直接拷贝过来)*)
/** * */ package cn.hncu.reflect.reflectDecompose; import java.io.Serializable; public class UserModel{ private String uuid; private String name; private static int type; private String pwd; public UserModel(String uuid, String name, int type, String pwd) { super(); this.uuid = uuid; this.name = name; this.type = type; this.pwd = pwd; } public UserModel(String uuid, int type){ this.uuid = uuid; this.type = type; } public UserModel(){ } private UserModel(String uuid){ this.uuid = uuid; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getType() { return type; } public void setType(int type) { this.type = type; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String toString() { return "{"+uuid+","+name+","+type+","+pwd+"}"; } private long privateMethod( double a){ return (long)a; } void defaultMethod(int i){ System.out.println("aa"); } }
获取类的方法:
private static void fetchMehtod(String name) throws Exception{ Class c=Class.forName(name); Method method[] =c.getMethods(); for(int i=0;i<method.length;i++){ //方法名 Method m =method[i]; System.out.println("name:"+m.getName()); System.out.println(m.getDeclaringClass()); //getDeclaringClass(),,如果此 Class 对象所表示的类或接口是另一个类的成员,则返回的 Class 对象表示该对象的声明类。 // 获得方法出现的异常 Class[] ex= m.getExceptionTypes(); for(int j=0;j<ex.length;j++){ System.out.println("exception:"+ex[j]); } //参数类型 Class par[]=m.getParameterTypes(); for(int j=0;j<par.length;j++){ System.out.println("Parameter:"+par[j]); } System.out.println("type:"+m.getReturnType()); } }
获取类的构造器:找出一个类中定义的构造方法,构造器没有返回类型
/** * 获取类中构造函数的信息 * getConstructors() : 获取当前类的public构造方法 * getDeclaredConstructors() :获取当前类声明的所有构造方法,包括private等其它非public方法 */ private static void fetchConstror(String name2) throws Exception { Class c=Class.forName(name); // Constructor[] con= c.getConstructors(); Constructor[] con= c.getDeclaredConstructors(); for(int i=0;i<con.length;i++){ //方法名 Constructor cons=con[i]; System.out.println("name:"+cons.getName()); System.out.println("DeclaringClass"+cons.getDeclaringClass()); //getDeclaringClass(),,如果此 Class 对象所表示的类或接口是另一个类的成员,则返回的 Class 对象表示该对象的声明类。 // 获得方法出现的异常 Class[] ex= cons.getExceptionTypes(); for(int j=0;j<ex.length;j++){ System.out.println("exception:"+ex[j]); } //参数类型 Class par[]=cons.getParameterTypes(); for(int j=0;j<par.length;j++){ System.out.println("Parameter:"+par[j]); } } }获取类的属性字段:找出一个类中定义了哪些属性字段。
private static void fetField(String name) throws Exception{ Class c=Class.forName(name); // Field field[]=c.getFields();//只能访问 public权限 Field field[]=c.getDeclaredFields();//获取当前类声明的所有构造方法,包括private等其它非public变量 for(int i=0;i<field.length;i++){ Field f=field[i]; System.out.println("name:"+f.getName());//变量名 System.out.println("Declaring:"+f.getDeclaringClass());//该类的明细路径 System.out.println("type:"+f.getType());// 变量的类型 int mod =f.getModifiers(); System.out.println("Modifiers::"+f);//变量的修饰符 System.out.println("Modifiers::"+Modifier.toString(mod));//输出类型 如 public private }