反射3

package reflect.corecode;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestOne {

    public static void main(String[] args) {
        
        /**
         * 
         * 反射
         * 动态操纵Java代码,被大量应用于Javabean中。
         * 
         * getFields  当前类及其超类的公有域
         * getDeclaredFields 当前类的所有域
         * 
         * getMethods 当前类及其超类的公有方法
         * public Method[] getDeclaredMethods() throws SecurityException { 当前类的所有方法
         * getMethod(String arg0, Class... arg1) 获取当前类及超类的方法
         * 
         * 
         * Method类中invoke方法
         * invoke(Object arg0, Object... arg1)
         */
        
        ///////////////////////////////book
        System.out.println(Double[].class.getName());
        
        Man man = new Man();
        man.setAge(11); 
        Class class1 = man.getClass();
        Field[] field1 = class1.getFields();
        Field[] field2 = class1.getDeclaredFields();
        
        Method[] method1 = class1.getMethods();
        Method[] method2 = class1.getDeclaredMethods();
        try {
            Method method3 = class1.getMethod("getAge", null);  //get方法
            Method method4 = class1.getMethod("setAge", int.class); //set方法
            Object value = method3.invoke(man, null);
            System.out.println("----->>"+method3);
        } catch (Exception e) {
            e.printStackTrace();
        } 
        
    }
    
    
    

}
原文地址:https://www.cnblogs.com/lxh520/p/9082386.html