JDK5.0新特性-反射
反射:
什么是反射?
剖析Java类中的各个组成部分映射成一个个java对象
为什么用反射?
多用于框架和组件,写出复用性高的通用程序
1.Class类.
Class代表的就是我们的字节码文件.
三种方式.
1.类名.class
2.对象.getClass();
3.Class.forName();
在开发中创建使用第三种 Class.forName();为什么?
这种方式耦合度低,没有依赖性.
------------------------------------------
2.Constructor(构造器对象)
构造器的作用就是用来创建对象的。
Constructor代表的是构造器对象,我们得到它后就可以实例化对象.
1.怎样得到Constructor对象.
getConstructor()-----得到指定的构造器
getConstructors();---得到所有的构造器
2.关于Constructor的注意事项
1.如果构造方法不是public。那么要获取其构造器对象使用 getDeclaredConstuctor()方法.
2.对于私有化权限,在使用前必须设置权限检查.
public void setAccessible(boolean flag)
值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查
3.在开发中,我们在得到Class时,一般会直接实例化类的对象.
2.Field
3.Method
1.
2.对于static方法怎样调用?
静态方法调用时不需要使用对象,直接写null
staticMethod.invoke(null,参数);
方法的参数如果是数组怎样调用?
在调用invoke时,第二个参数强制转换成Object,或在外面在包装一层数组.
1
package cn.itcast.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import org.junit.Test; public class Demo1 { @Test public void demo1() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class clazz = Class.forName("cn.itcast.reflect.Teacher"); clazz.newInstance(); Teacher t = new Teacher(); } // 获取Class对象 @Test public void demo2() throws ClassNotFoundException { // 1.通过.class // Class clazz=Teacher.class; // 2.通过对象获取 // Teacher t = new Teacher(); // Class clazz = t.getClass(); //getClass方法是在Object类中定义的 // 3.Class.forName() Class clazz = Class.forName("cn.itcast.reflect.Teacher"); } // 获取构造器. @Test public void demo3() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { // 1.得到Teacher类的Class对象. Class clazz = Teacher.class; // 2.得到Teacher类的构造器. Constructor c1 = clazz.getConstructor(); // 无参数构造 Constructor c2 = clazz.getConstructor(String.class); // String类型参数构造 Constructor c3 = clazz.getConstructor(String.class, int.class); // String,int类型参数构造 // 3.通过Constructor对象创建Teacher类的实例. // Teacher obj1 = (Teacher) c1.newInstance(); // 相当于new Teacher(); // // obj1.show(); // Teacher obj2 = (Teacher) c2.newInstance("张三");// new Teacher("张三"); // // obj2.show(); Teacher obj3 = (Teacher) c3.newInstance("李四", 20);// new // Teacher("李四",20); obj3.show(); } // 关于Constructor的注意事项 @Test public void demo4() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { // 1.得到Teacher类的Class对象. Class clazz = Teacher.class; // 2.获取非public的构造器对象. Constructor c = clazz.getDeclaredConstructor(String.class, int.class); // 取消安全检查 c.setAccessible(true); // 3.创建Teacher对象 Teacher obj = (Teacher) c.newInstance("李四", 20); obj.show(); } @Test public void demo5() throws InstantiationException, IllegalAccessException { Class clazz = Teacher.class; Teacher t = (Teacher) clazz.newInstance();// 要求必须存在无参构造 } // 关于Field类.---它代表的是属性. @Test public void demo6() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstantiationException { // Field对象的获取也是通过Class Class clazz = Teacher.class; Field fname = clazz.getDeclaredField("name"); //相当于得到了String name属性. fname.setAccessible(true); //得到属性值. Teacher t=(Teacher) clazz.newInstance(); //Object msg=fname.get(t); //相当于 t.name; //对属性值进行赋值操作. fname.set(t, "abcdef"); //t.name="abcdef"; System.out.println(t.getName()); } }
2
package cn.itcast.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.junit.Test; //Method操作 public class Demo2 { // 得到Teacher类中的show方法的Metho对象. @Test public void fun1() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { // 1.得到Class Class c = Teacher.class; // 2.得到Method // Method m1 = c.getMethod("show"); // 获取show(); Method m2 = c.getMethod("show", String.class); // 3.让方法执行. Teacher t = (Teacher) c.newInstance(); // Teacher t=new Teacher(); // m1.invoke(t); //相当于 t.show(); m2.invoke(t, "good"); } @Test public void fun2() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { // 1.得到Class Class c = Teacher.class; // 2.得到Method Method m=c.getMethod("show", String.class, int.class); //3.让方法执行. Object returnValue=m.invoke(c.newInstance(), "tom",20); //String msg=t.show("tom",20); System.out.println(returnValue); } }
3
package cn.itcast.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.junit.Test; public class Demo3 { public static void main(String[] args) { // 要通过反射去调用main方法.并传递参数. System.out.println(args[0]); System.out.println(args[1]); } @Test public void fun() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { // 1.得到当前类的Class Class clazz = Demo3.class; // 2.得到这个类中的main方法的Method Method main = clazz.getMethod("main", String[].class); // 3.调用main方法. invoke。 //main.invoke(null, new String[]{"hello", "world"}); // 参数个数错误 main.invoke(null,new Object[]{new String[]{"hello", "world"}}); main.invoke(null, (Object)(new String[]{"hello", "world"})); } }
4
package cn.itcast.reflect; public class Teacher { public int age = 10; private String name = "tom"; public Teacher() { System.out.println("Teacher无参构造方法"); } public Teacher(String name) { this.name = name; System.out.println("Teacher构造方法----String name"); } private Teacher(String name, int age) { this.name = name; this.age = age; System.out.println("Teacher构造方法----String name,int age"); } public String getName() { return name; } public void show() { System.out.println("name:" + name + " age:" + age); } public void show(String msg) { System.out.println(msg); } public String show(String msg,int i){ return msg+" "+i; } }