
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
| public class ClassDemo1 {
public static void main(String[] args) { Foo foo1 = new Foo();
Class c1=Foo.class; Class c2 = foo1.getClass(); Class c3=Class.forName("com.package.Foo") }
Foo foo = (Foo) c1.newInstance();foo.print();
class Foo { void print() { } } }
|




1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 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
| 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.imooc.reflect;
import java.lang.reflect.Method;
public class BeanUtil {
public static Object getValueByPropertyName(Object obj, String propertyName) { String getMethodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Class c = obj.getClass(); try { Method m= c.getMethod(getMethodName); Object value = m.invoke(obj); return value; } catch (Exception e) { e.printStackTrace(); return null; } } }
|
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(")"); } } }
|
Author:
John Doe
Permalink:
http://yoursite.com/2018/12/10/java基础/java反射/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?