java反射
万物皆对象
关键字:
class 类
Method 方法
Filed 属性
Constructor 构造方法
1 package com.qfedu.test1; 2 3 import java.lang.reflect.Field; 4 /** 5 * 获取public声明的所有的属性信息 并且访问 6 * @author WHD 7 * 8 */ 9 public class Student { 10 public String name; 11 public int age; 12 public String address; 13 14 public Student() { 15 System.out.println("Student类的无参构造"); 16 } 17 18 public static void main(String[] args) { 19 try { 20 // 根据包名+ 类名 (全限定名)获取类信息文件 21 Class<?> stuClass = Class.forName("com.qfedu.test1.Student"); 22 // 调用Class类的newInstance方法 构造实例 23 Object newInstance1 = stuClass.newInstance(); 24 Object newInstance2 = stuClass.newInstance(); 25 Object newInstance3 = stuClass.newInstance(); 26 27 // 通过Class对象获取到当前类所有的属性 返回值为字段数组 28 Field [] fs = stuClass.getFields(); 29 for(Field f : fs) { 30 System.out.println(f.getName()); 31 } 32 33 // 根据字段名称获取到单个字段对象 34 Field nameField = stuClass.getField("name"); 35 36 // 调用字段set方法 设置属性值 37 nameField.set(newInstance2, "赵四"); 38 39 // 调用字段get方法 获取属性值 40 System.out.println(nameField.get(newInstance2)); 41 42 Field ageField = stuClass.getField("age"); 43 44 ageField.set(newInstance1, 20); 45 System.out.println(ageField.get(newInstance1)); 46 47 Field addressField = stuClass.getField("address"); 48 addressField.set(newInstance3, "象牙山"); 49 System.out.println(addressField.get(newInstance3)); 50 51 52 } catch (ClassNotFoundException e) { 53 e.printStackTrace(); 54 } catch (InstantiationException e) { 55 e.printStackTrace(); 56 } catch (IllegalAccessException e) { 57 e.printStackTrace(); 58 } catch (NoSuchFieldException e) { 59 e.printStackTrace(); 60 } catch (SecurityException e) { 61 e.printStackTrace(); 62 } 63 } 64 65 }
1 package com.qfedu.test2; 2 3 import java.lang.reflect.Field; 4 5 /** 6 * 获取到类中所有的属性 7 * @author WHD 8 * 9 */ 10 public class Person { 11 private String name; 12 int age; 13 protected String address; 14 public char sex; 15 16 public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException { 17 Class<?> personClass = Class.forName("com.qfedu.test1.Person"); 18 Field[] fs = personClass.getDeclaredFields(); 19 for (Field field : fs) { 20 System.out.println(field.getName() + "\t" +field.getType()); 21 } 22 23 Object newInstance = personClass.newInstance(); 24 Field nameField = personClass.getDeclaredField("name"); 25 nameField.set(newInstance, "广坤"); 26 System.out.println(nameField.get(newInstance)); 27 28 Field sexField = personClass.getDeclaredField("sex"); 29 sexField.set(newInstance, '男'); 30 System.out.println(sexField.get(newInstance)); 31 32 } 33 }
1 package com.qfedu.test2; 2 3 import java.lang.reflect.Field; 4 /** 5 * 获取属性 6 * getFields() 获取所有的public修饰的属性 7 * getField(String name)根据名字获取单个属性对象 8 * getDeclaredFields() 获取所有已定义的属性对象 包括 private 默认 protected public 9 * getDeclaredField(String name) 根据名字获取单个属性对象 可以是任意访问修饰符 10 * @author WHD 11 * 12 */ 13 public class TestPerson { 14 public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException { 15 // Person p = new Person(); 16 // p.name = "" 17 18 Class<?> personClass = Class.forName("com.qfedu.test1.Person"); 19 20 Field nameField = personClass.getDeclaredField("name"); 21 22 nameField.setAccessible(true); // 设置访问权限为true 表示忽略JVM非法访问异常 23 24 25 26 System.out.println(nameField.getName() + nameField.getType()); 27 Object newInstance = personClass.newInstance(); 28 nameField.set(newInstance, "赵四"); 29 System.out.println(nameField.get(newInstance)); 30 } 31 }
3.使用反射获取方法
3.1获取public修饰的,和继承自父类的方法
getMethods() 获取本类所有public修饰的方法和继承父类的方法
getMethod() 获取单个的方法
1 package com.qfedu.test3; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 6 public class TestStudent { 7 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 8 Class<?> studentClass = Class.forName("com.qfedu.test3.Student"); 9 Method[] methods = studentClass.getMethods(); 10 System.out.println(methods.length); 11 for (Method method : methods) { 12 System.out.println(method.getName()); 13 } 14 Object newInstance = studentClass.newInstance(); 15 Method m1Method = studentClass.getMethod("m1"); 16 17 m1Method.invoke(newInstance); 18 19 20 Method method = studentClass.getMethod("m1", String.class); 21 method.invoke(newInstance, "赵四"); 22 23 24 Method method2 = studentClass.getMethod("m1", int.class); 25 method2.invoke(newInstance, 330); 26 27 28 Method method3 = studentClass.getMethod("m3", String.class,int.class); 29 method3.invoke(newInstance, "赵四",22); 30 } 31 }
3.2获取本类中定义的方法
getDeclaredmethod(String name, Class<?>....parameterTypes)获取单个本类中声明的方法 如果访问权限不足 可以使用setAccessable(true)修改权限
getDeclaredMethobs()获取所有本类中已经定义的方法
1 package com.qfedu.test4; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.lang.reflect.Method; 5 6 public class TestStudent { 7 public static void main(String[] args) { 8 try { 9 Class<?> stuClass = Class.forName("com.qfedu.test4.Student"); 10 Method[] ms = stuClass.getDeclaredMethods(); 11 for (Method method : ms) { 12 System.out.println(method.getName()); 13 } 14 Object newInstance = stuClass.newInstance(); 15 Method m1 = stuClass.getDeclaredMethod("m1"); 16 m1.setAccessible(true); // 私有方法必须设置访问权限为true 17 m1.invoke(newInstance); 18 19 20 Method m2 = stuClass.getDeclaredMethod("m1", String.class); 21 m2.invoke(newInstance, "赵四"); 22 23 24 Method m3 = stuClass.getMethod("m3", String.class,int.class); 25 m3.invoke(newInstance, "广坤",20); 26 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 } catch (NoSuchMethodException e) { 30 e.printStackTrace(); 31 } catch (SecurityException e) { 32 e.printStackTrace(); 33 } catch (IllegalAccessException e) { 34 e.printStackTrace(); 35 } catch (IllegalArgumentException e) { 36 e.printStackTrace(); 37 } catch (InvocationTargetException e) { 38 e.printStackTrace(); 39 } catch (InstantiationException e) { 40 e.printStackTrace(); 41 } 42 } 43 }
4.使用反射获取构造器
4.1获取所有public修饰的构造器
getConstructors()获取public修饰的构造方法
getConstructor(class...type)获取单个的构造器对象
1 package com.qfedu.test5; 2 3 /** 4 * 获取本类中定义的以public修饰的构造方法 5 * @author WHD 6 * 7 */ 8 public class Student { 9 public String name; 10 public int age; 11 public String address; 12 13 public Student() { 14 System.out.println("Student类的无参构造"); 15 } 16 17 public Student(String name, int age, String address) { 18 this.name = name; 19 this.age = age; 20 this.address = address; 21 System.out.println("三个参数的构造方法"); 22 } 23 24 public Student(String name) { 25 this.name = name; 26 System.out.println("一个参数的构造方法"); 27 } 28 29 public Student(String name, int age) { 30 this.name = name; 31 this.age = age; 32 System.out.println("两个参数的构造方法"); 33 } 34 }
1 package com.qfedu.test5; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.InvocationTargetException; 5 6 public class TestStudent { 7 public static void main(String[] args) { 8 try { 9 Class<?> stuClass = Class.forName("com.qfedu.test5.Student"); 10 Constructor<?>[] cs = stuClass.getConstructors(); 11 for (Constructor<?> c : cs) { 12 System.out.println("名字" + c.getName() + "参数个数" + c.getParameterCount()); 13 } 14 15 Constructor<?> constructor1 = stuClass.getConstructor(); 16 Object newInstance1 = constructor1.newInstance(); 17 18 19 Constructor<?> constructor2 = stuClass.getConstructor(String.class,int.class,String.class); 20 Object newInstance2 = constructor2.newInstance("赵四",20,"象牙山"); 21 22 23 Constructor<?> constructor3 = stuClass.getConstructor(String.class); 24 Object newInstance3 = constructor3.newInstance("广坤"); 25 26 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 } catch (NoSuchMethodException e) { 30 e.printStackTrace(); 31 } catch (SecurityException e) { 32 e.printStackTrace(); 33 } catch (InstantiationException e) { 34 e.printStackTrace(); 35 } catch (IllegalAccessException e) { 36 e.printStackTrace(); 37 } catch (IllegalArgumentException e) { 38 e.printStackTrace(); 39 } catch (InvocationTargetException e) { 40 e.printStackTrace(); 41 } 42 } 43 }
4.2获取本类中定义的构造器
getDeclaredConstructors() 获取本类中定义的所有的构造方法
getDeclaredConstructor() 获取本类中单个的构造方法
1 package com.qfedu.test6; 2 3 /** 4 * 获取本类中定义的以public修饰的构造方法 5 * @author WHD 6 * 7 */ 8 public class Student { 9 public String name; 10 public int age; 11 public String address; 12 13 private Student() { 14 System.out.println("Student类的无参构造"); 15 } 16 17 Student(String name, int age, String address) { 18 this.name = name; 19 this.age = age; 20 this.address = address; 21 System.out.println("三个参数的构造方法"); 22 } 23 24 protected Student(String name) { 25 this.name = name; 26 System.out.println("一个参数的构造方法"); 27 } 28 29 public Student(String name, int age) { 30 this.name = name; 31 this.age = age; 32 System.out.println("两个参数的构造方法"); 33 } 34 }
1 package com.qfedu.test6; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.InvocationTargetException; 5 6 public class TestStudent { 7 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 8 Class<?> stuClass = Class.forName("com.qfedu.test6.Student"); 9 Constructor<?>[] cs = stuClass.getDeclaredConstructors(); 10 System.out.println(cs.length); 11 12 13 Constructor<?> cons1 = stuClass.getDeclaredConstructor(); 14 cons1.setAccessible(true); 15 Object newInstance1 = cons1.newInstance(); 16 } 17 }

浙公网安备 33010602011771号