参考视频 反射——Java高级开发必须懂的 https://www.imooc.com/learn/199
Class类
方法的反射
如何获取某个方法 方法的名称和方法的参数列表才能唯一决定某个方法
方法反射的操作 method.invoke(对象,参数列表)
为什么要用方法的反射 why?指定方法名称调用方法 举个实际应用的案例 —->通过标准JavaBean的属性名获取其属性值 BeanUtil类
通过Class,Method来认识泛型的本质
反射-Class类的使用 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 package com.imooc.reflect;public class ClassDemo1 { public static void main (String[] args) { Foo foo1 = new Foo(); Class c1 = Foo.class; Class c2 = foo1.getClass(); System.out.println(c1 == c2); Class c3 = null ; try { c3 = Class.forName("com.imooc.reflect.Foo" ); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(c2==c3); try { Foo foo = (Foo)c1.newInstance(); foo.print(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }class Foo { void print () { System.out.println("foo" ); } }
动态加载类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.imooc.reflect;public class ClassDemo2 { public static void main (String[] args) { Class c1 = int .class; Class c2 = String.class; Class c3 = double .class; Class c4 = Double.class; Class c5 = void .class; System.out.println(c1.getName()); System.out.println(c2.getName()); System.out.println(c2.getSimpleName()); System.out.println(c5.getName()); } }
获取 方法信息 和 成员变量构造函数信息 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 package com.imooc.reflect;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class ClassUtil { public static void printClassMethodMessage (Object obj) { Class c = obj.getClass(); System.out.println("类的名称是:" +c.getName()); Method[] ms = c.getMethods(); for (int i = 0 ; i < ms.length;i++){ Class returnType = ms[i].getReturnType(); System.out.print(returnType.getName()+" " ); System.out.print(ms[i].getName()+"(" ); Class[] paramTypes = ms[i].getParameterTypes(); for (Class class1 : paramTypes) { System.out.print(class1.getName()+"," ); } System.out.println(")" ); } } public static void printFieldMessage (Object obj) { Class c = obj.getClass(); Field[] fs = c.getDeclaredFields(); for (Field field : fs) { Class fieldType = field.getType(); String typeName = fieldType.getName(); String fieldName = field.getName(); System.out.println(typeName+" " +fieldName); } } public static void printConMessage (Object obj) { Class c = obj.getClass(); Constructor[] cs = c.getDeclaredConstructors(); for (Constructor constructor : cs) { System.out.print(constructor.getName()+"(" ); Class[] paramTypes = constructor.getParameterTypes(); for (Class class1 : paramTypes) { System.out.print(class1.getName()+"," ); } System.out.println(")" ); } } }
1 2 3 4 5 6 7 8 9 10 11 package com.imooc.reflect;public class ClassDemo3 { public static void main (String[] args) { String s = "hello" ; ClassUtil.printClassMethodMessage(s); Integer n1 = 1 ; ClassUtil.printClassMethodMessage(n1); } }
1 2 3 4 5 6 7 8 9 10 package com.imooc.reflect;public class ClassDemo5 { public static void main (String[] args) { ClassUtil.printConMessage("hello" ); ClassUtil.printConMessage(new Integer(1 )); } }
方法反射的基本操作 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 package com.imooc.reflect;import java.lang.reflect.Method;public class MethodDemo1 { public static void main (String[] args) { A a1 = new A(); Class c = a1.getClass(); try { Method m = c.getMethod("print" , int .class,int .class); Object o = m.invoke(a1, 10 ,20 ); System.out.println("==================" ); Method m1 = c.getMethod("print" ,String.class,String.class); o = m1.invoke(a1, "hello" ,"WORLD" ); System.out.println("===================" ); Method m2 = c.getMethod("print" ); m2.invoke(a1); } catch (Exception e) { e.printStackTrace(); } } }class A { public void print () { System.out.println("helloworld" ); } public void print (int a,int b) { System.out.println(a+b); } public void print (String a,String b) { System.out.println(a.toUpperCase()+"," +b.toLowerCase()); } }
通过反射了解集合泛型的本质
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 package com.imooc.reflect;import java.lang.reflect.Method;import java.util.ArrayList;public class MethodDemo4 { public static void main (String[] args) { ArrayList list = new ArrayList(); ArrayList<String> list1 = new ArrayList<String>(); list1.add("hello" ); Class c1 = list.getClass(); Class c2 = list1.getClass(); System.out.println(c1 == c2); try { Method m = c2.getMethod("add" , Object.class); m.invoke(list1, 20 ); System.out.println(list1.size()); System.out.println(list1); } catch (Exception e) { e.printStackTrace(); } } }