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
| package com.sympa.lesson01;
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
public class ReflectionTest {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<Person> clazz = Person.class;
Person person = clazz.getDeclaredConstructor().newInstance(); System.out.println(person);
Field[] fields = clazz.getDeclaredFields(); for(Field f : fields){ System.out.println(f); }
Method[] methods = clazz.getDeclaredMethods(); for(Method m : methods){ System.out.println(m); }
Method show = clazz.getMethod("show");
} }
|