java的反射机制(简单解释)
2013-10-23 09:50
399 查看
Person类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package d.z.h.replect; import java.util.List; public class Person { public String name = "ccccccc"; private int password=123; private static int age=66; public Person() { System.out.println("Person()"); } public Person(String name) { System.out.println(name); } public Person(String name, int password) { System.out.println("name="+name+":"+"password"+password); } private Person(List list) { System.out.println(" Person(List list)"); } //方法 public void getName(){ System.out.println("getName()"); } public void getName(String name,int password){ System.out.println("name="+name+":"+"password"+password); } private Class[] getName(String name,int [] password){ return new Class[]{String.class}; } public static void getName(int num){ System.out.println(num); } //main方法 public static void main(String[] args){ System.out.println("main!!"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package d.z.h.replect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.junit.Test; public class replectDemo { /** * 反射就是加载类,并解剖出类的各个组成部分(Class 类提供了解剖的一些方法,能解剖出来构造函数,方法,成员变量) * * */ // public Person() @Test public void test1() throws Exception { Class clazz = Class.forName("d.z.h.replect.Person"); Constructor c = clazz.getConstructor(null); Person p = (Person) c.newInstance(null); System.out.println(p.name); } // public Person(String name) @Test public void test2() throws Exception { Class clazz = Class.forName("d.z.h.replect.Person"); Constructor c = clazz.getConstructor(String.class); Person p = (Person) c.newInstance("xxxxxxxx"); System.out.println(p.name); } // private Person(String name, int password) @Test public void test3() throws Exception { Class clazz = Class.forName("d.z.h.replect.Person"); Constructor c = clazz.getConstructor(String.class, int.class); Person p = (Person) c.newInstance("dzh", 26); System.out.println(p.name); } // private Person(List list) @Test public void test4() throws Exception { Class clazz = Class.forName("d.z.h.replect.Person"); Constructor c = clazz.getDeclaredConstructor(List.class); c.setAccessible(true);// 对于私有的方法要用 “暴力反射”在外部才能被调用 Person p = (Person) c.newInstance(new ArrayList()); System.out.println(p.name); } /*** * 反射方法 测试 * * @throws ClassNotFoundException * */ // public void getName() @Test public void metoh1() throws Exception { Person p = new Person(); Class clazz = Class.forName("d.z.h.replect.Person"); Method metoh = clazz.getMethod("getName", null);// 1.哪个方法 2.哪个参数 metoh.invoke(p, null);// 执行方法 1.obj 对象(调用哪个对象的方法,传个对象)2.args 参数 } // public void getName(String name,int password) @Test public void metoh2() throws Exception { Person p = new Person(); Class clazz = Class.forName("d.z.h.replect.Person"); Method metoh = clazz.getMethod("getName", String.class, int.class);// 1.哪个方法 // 2.哪个参数 metoh.invoke(p, "ddd", 12);// 执行方法 1.obj 对象(调用哪个对象的方法,传个对象)2.args 参数 } // private Class[] getName(String name,int [] password) @Test public void metoh3() throws Exception { Person p = new Person(); Class clazz = Class.forName("d.z.h.replect.Person"); Method metoh = clazz.getDeclaredMethod("getName", String.class, int[].class);// 1.哪个方法 2.哪个参数 metoh.setAccessible(true); Class cs[] = (Class[]) metoh.invoke(p, "ddd", new int[] { 1, 2 });// 执行方法 // 1.obj // 对象(调用哪个对象的方法,传个对象)2.args // 参数 System.out.println(cs[0]); } // public static void getName(int num) 可以不要对象 @Test public void metoh4() throws Exception { Class clazz = Class.forName("d.z.h.replect.Person"); Method metoh = clazz.getMethod("getName", int.class); metoh.invoke(null, 21); } /** * 反射main方法 * */ // public static void main(String[] args) @Test public void metoh5() throws Exception { Class clazz = Class.forName("d.z.h.replect.Person"); Method metoh = clazz.getMethod("main", String[].class); // metoh.invoke(null,new Object[]{ new String[]{"aaa","bbb"}});//jdk1.4 // 和1.5有差异 ,1.4是数组要拆开 metoh.invoke(null, (Object) new String[] { "aaa", "bbb" });// 欺骗它说不是数组 } /** * 反射字段 * * */ //public String name = "ccccccc"; @Test public void field1() throws Exception { Person p = new Person(); Class clazz = Class.forName("d.z.h.replect.Person"); Field f = clazz.getField("name"); //获得字段的值 Object value = f.get(p); Class type = f.getType();//获得字段类型 if(type.equals(String.class)){ String svalue = (String) value; System.out.println(svalue); } //设置字段的值 f.set(p, "xxxxxxx"); System.out.println(p.name); } //private int password = 123; @Test public void field2() throws Exception { Person p = new Person(); Class clazz = Class.forName("d.z.h.replect.Person"); Field f = clazz.getDeclaredField("password"); f.setAccessible(true); System.out.println(f.get(p)); } // private static int age=66 @Test public void field3() throws Exception { Person p = new Person(); Class clazz = Class.forName("d.z.h.replect.Person"); Field f = clazz.getDeclaredField("age"); f.setAccessible(true); System.out.println(f.get(p)); } /** 三种加载类的方法 */ public void useMetoh() { // 三种加载类的方法 之一 常用 // Class clazz = Class.forName("d.z.h.replect.Person"); // 三种加载类的方法 之二 // Class clazz = new Person().getClass(); // 三种加载类的方法 之三 Class clazz = Person.class; } } |
相关文章推荐
- Java实现一个简单的RPC框架(七) 反射机制调用类函数
- Java 反射机制 的超简单实例分析
- Java 反射机制简单总结
- 浅谈Java中的反射机制以及Class、Constructor、Field、Method简单框架原理
- java 反射机制(一)简单实例
- 简单总结Java的反射机制的运用
- Java 反射机制 的超简单实例分析
- JAVA的泛型特性和反射机制在泛型中的简单运用
- java-反射机制-简单代码
- 关于Java反射机制中的Field简单尝试与示例以及说明
- Java的反射机制---动态调用对象的简单方法
- 简单的java 反射机制实践 map 与object对象的转换
- 利用Java的反射机制,写了一个简单的仿Gson的解析器
- Java基础系列:(3)反射机制的简单总结
- java-反射机制的原理与简单使用
- [java]反射机制给属性使用set方法构造对象
- 通过Java的反射机制创建运行时类的对象并调用其结构
- JAVA中的反射机制
- android 中的反射机制和java的对比
- JAVA : 反射机制