Java 反射

field

public class FieldBean {

    private String name = "xq";
    private static int versionCode = 100;
    private final int height = 2;
    private static final String KEY = "hello";

    public int age = 18;
    public static String versionName = "v1.0.0";
    public final int width = 30;
    public static final String TAG = FieldBean.class.getSimpleName();

}

import java.lang.reflect.Field;

public class FieldTest {

    /**
     * getFields : 获取所有公有属性
     * final 不可修改值
     */
    public static void main(String[] args) {

        try {
            Class<?> bean = Class.forName("FieldBean");
            Object instance = bean.newInstance();

            Field[] fields = bean.getFields();
            for (int i = 0; i < fields.length; i++) {
                System.out.println("" + fields[i]);
            }

            System.out.println("========================================");

            //getField 会报错 : NoSuchFieldException ,私有属性
            Field field = bean.getDeclaredField("name");
            //setAccessible(false) 会报错 : IllegalAccessException
            field.setAccessible(true);
            String name = (String) field.get(instance);
            System.out.println("" + name);

            System.out.println("========================================");

            // 
            Field vCField = bean.getDeclaredField("versionCode");
            vCField.setAccessible(true);
            int versionCode1 = (int) vCField.get(instance);
            System.out.println("" + versionCode1);
            //重新设值
            vCField.set(instance, 666);
            int versionCode2 = (int) vCField.get(instance);
            System.out.println("" + versionCode2);

            System.out.println("========================================");

            //
            Field height = bean.getDeclaredField("height");
            height.setAccessible(true);
            int height1 = (int) height.get(instance);
            System.out.println("" + height1);
            //重新设值
            //报错 :
            //Exception in thread "main" java.lang.IllegalArgumentException:
            // Can not set final int field FieldBean.height to java.lang.String
//            height.set(instance, "new Hello");
//            int height2 = (int) height.get(instance);
//            System.out.println("" + height2);

            System.out.println("========================================");

            //
            Field keyField = bean.getDeclaredField("KEY");
            keyField.setAccessible(true);
            String key1 = (String) keyField.get(instance);
            System.out.println("" + key1);
            //重新设值
            //报错 :
            //java.lang.IllegalAccessException:
            // Can not set static final java.lang.String field FieldBean.KEY to java.lang.String
//            keyField.set(instance, "new Hello");
//            String key2 = (String) keyField.get(instance);
//            System.out.println("" + key2);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

    }
}

method

public class MethodBean {


    //无参数,无返回
    public static void method1() {
        System.out.println("============1无参数,无返回,public==============");
    }

    private static void method2() {
        System.out.println("============2无参数,无返回,private==============");
    }

    //带参数,无返回
    public static void method3(String name, int age) {
        System.out.println("============3带参数,无返回,public==============" +
                name + " : " + age);
    }

    private static void method4(String name, int age) {
        System.out.println("============4带参数,无返回,private==============" +
                name + " : " + age);
    }

    //带参数,带返回
    public static String method5(String name, int age) {
        System.out.println("============5带参数,带返回,public==============" +
                name + " : " + age);
        return name + " - " + age;
    }

    private static String method6(String name, int age) {
        System.out.println("============6带参数,带返回,private==============" +
                name + " : " + age);
        return name + " - " + age;
    }

}



import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MethodTest {


    public static void main(String[] args) {


        test1();
        test2();
        test3();
        test4();
    }

    /**
     * getMethods : 获取所有【公开】方法,包含可访问的父类方法
     * getDeclaredMethods : 获取本类中所有方法,包含私有
     */
    private static void test4() {
        System.out.println("=================test4=====================");
        try {
            Class<?> bean = Class.forName("MethodBean");

            Method[] methods = bean.getMethods();
            for (int i = 0; i < methods.length; i++) {
                System.out.println("" + methods[i].getName());
            }

            System.out.println("===============================");

            Method[] declaredMethods = bean.getDeclaredMethods();
            for (int i = 0; i < declaredMethods.length; i++) {
                System.out.println("" + declaredMethods[i].getName());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //method3
    //method5
    //method1
    //wait
    //wait
    //wait
    //equals
    //toString
    //hashCode
    //getClass
    //notify
    //notifyAll
    //===============================
    //method3
    //method6
    //method5
    //method4
    //method1
    //method2

    private static void test3() {
        System.out.println("=================test3=====================");
        try {
            Class<?> bean = Class.forName("MethodBean");

            {
                //获取公有方法 : getMethod
                Method method5 = bean.getMethod("method5", String.class, int.class);
                Object o5 = bean.newInstance();
                Object invoke5 = method5.invoke(o5, "xq", 18);
                System.out.println("method5===============" + invoke5);


                //获取私有方法 : getDeclaredMethod
                Method method6 = bean.getDeclaredMethod("method6", String.class, int.class);
                //必须
                method6.setAccessible(true);
                Object o6 = bean.newInstance();
                Object invoke6 = method6.invoke(o6, "xq6", 25);
                System.out.println("method6===============" + invoke6);
            }

        } catch (ClassNotFoundException
                | NoSuchMethodException
                | IllegalAccessException
                | InstantiationException
                | InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    //=================test3=====================
    //============5带参数,带返回,public==============xq : 18
    //method5===============xq - 18
    //============6带参数,带返回,private==============xq6 : 25
    //method6===============xq6 - 25

    private static void test2() {
        System.out.println("=================test2=====================");

        try {
            Class<?> bean = Class.forName("MethodBean");
            {
                Method method3 = bean.getMethod("method3", String.class, int.class);
                Object o3 = bean.newInstance();
                Object invoke3 = method3.invoke(o3, "xq", 18);
                System.out.println("method3===============" + invoke3);


                Method method4 = bean.getDeclaredMethod("method4", String.class, int.class);
                //必须
                method4.setAccessible(true);
                Object o4 = bean.newInstance();
                Object invoke4 = method4.invoke(o4, "xq4", 25);
                System.out.println("method4===============" + invoke4);
            }
        } catch (ClassNotFoundException
                | NoSuchMethodException
                | IllegalAccessException
                | InstantiationException
                | InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    //=================test2=====================
    //============3带参数,无返回,public==============xq : 18
    //method3===============null
    //============4带参数,无返回,private==============xq4 : 25
    //method4===============null


    private static void test1() {
        System.out.println("=================test1=====================");

        try {
            Class<?> bean = Class.forName("MethodBean");

            //1。获取方法
            Method method1 = bean.getMethod("method1");
            //2。实例化类
            Object o1 = bean.newInstance();
            //3。调用并返回
            Object invoke1 = method1.invoke(o1);
            System.out.println("method1===============" + invoke1);


            //1。获取方法
            Method method2 = bean.getDeclaredMethod("method2");
            //必须
            method2.setAccessible(true);
            //2。实例化类
            Object o2 = bean.newInstance();
            //3。调用并返回
            Object invoke2 = method2.invoke(o2);
            System.out.println("method2===============" + invoke2);
        } catch (ClassNotFoundException
                | NoSuchMethodException
                | IllegalAccessException
                | InstantiationException
                | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    //=================test1=====================
    //============1无参数,无返回,public==============
    //method1===============null
    //============2无参数,无返回,private==============
    //method2===============null


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值