java反射详解

为啥突然写这个东西呢,是因为今天看到了通过反射更改tablayout的下标的宽度,才知道这是多么重要的知识点(多么痛的领悟)

什么是java的反射机制

java反射机制是在运行状态时,对于任意一个类,都能够获取这个类的所有变量和方法;对于任意一个对象,都能够调用它的任意一个方法和变量;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。

*示例类:

package cn.yuan.xiaoyu.testmodule.bean;

import cn.yuan.xiaoyu.wangmodule.view.MyInfoCheckSexDialog;

/**
 * Created by yukuoyuan on 2017/4/19.
 */
public class TestObjBean implements MyInfoCheckSexDialog.OnButtonCliickListener{
    private int age = 12;//年龄
    private String name = "zhuoma";//名字

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    private void setAge(int age) {
        this.age = age;
    }

    @Override
    public void onManClick() {

    }

    @Override
    public void onWomenClick() {

    }
}
*通过一个对象获取完整的包名和类名
 TestObjBean testObjBean = new TestObjBean();
 L.i("包名和类名", testObjBean.getClass().getName());
*输出值
04-19 17:21:31.635 7478-7478/cn.yuan.xiaoyu I/包名和类名: cn.yuan.xiaoyu.testmodule.bean.TestObjBean
* 获取类的名字或者类的包名获取类的包名和名字
        Class<?> class1 = null;
        Class<?> class2 = null;
        Class<?> class3 = null;
        try {
            class1 = Class.forName("cn.yuan.xiaoyu.testmodule.bean.TestObjBean");
            class2 = new TestObjBean().getClass();
            class3 = TestObjBean.class;
            L.i("包名和类名1", class1.getName());
            L.i("包名和类名2", class2.getName());
            L.i("包名和类名3", class3.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
*输出值
04-19 21:53:47.513 2694-2694/cn.yuan.xiaoyu I/包名和类名1: cn.yuan.xiaoyu.testmodule.bean.TestObjBean
04-19 21:53:47.513 2694-2694/cn.yuan.xiaoyu I/包名和类名2: cn.yuan.xiaoyu.testmodule.bean.TestObjBean
04-19 21:53:47.513 2694-2694/cn.yuan.xiaoyu I/包名和类名3: cn.yuan.xiaoyu.testmodule.bean.TestObjBean
* 获取一个类的父类和实现的接口
  Class<?> clazz = null;
        Class<?> parentClass = null;
        try {
            clazz = Class.forName("cn.yuan.xiaoyu.testmodule.bean.TestObjBean");
            // 取得父类
            parentClass = clazz.getSuperclass();
            L.i("父类是::", parentClass.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 获取所有的接口
        Class<?> intes[] = clazz.getInterfaces();
        for (int i = 0; i < intes.length; i++) {
            L.i("实现的接口:", (i + 1) + ":" + intes[i].getName());
        }
*输出值
04-19 22:09:02.264 13259-13259/cn.yuan.xiaoyu I/父类是::: java.lang.Object
04-19 22:09:02.264 13259-13259/cn.yuan.xiaoyu I/实现的接口:: 1:cn.yuan.xiaoyu.wangmodule.view.MyInfoCheckSexDialog$OnButtonCliickListener
* 获取一个类的对象,并且设置它的成员变量的值
Class<?> class1 = null;
        try {
            class1 = Class.forName("cn.yuan.xiaoyu.testmodule.bean.TestObjBean");
            TestObjBean testObjBean = (TestObjBean) class1.newInstance();
            testObjBean.setAge(24);
            testObjBean.setName("yuko");
            L.i("值是多少", testObjBean.toString());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
*输出值
04-19 22:22:11.146 22196-22196/cn.yuan.xiaoyu I/值是多少: TestObjBean{age=24, name='yuko'}
* 获取一个类的所有变量及其权限和类型
  Class<?> class1 = null;
        try {
            class1 = Class.forName("cn.yuan.xiaoyu.testmodule.bean.TestObjBean");
            Field[] field = class1.getDeclaredFields();
            for (int i = 0; i < field.length; i++) {
                // 权限修饰符
                int mo = field[i].getModifiers();
                String priv = Modifier.toString(mo);
                // 属性类型
                Class<?> type = field[i].getType();
                L.i("所有的变量", priv + " : " + type.getName() + " " + field[i].getName() + ";");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
*输出值
04-19 22:33:16.576 30137-30137/cn.yuan.xiaoyu I/所有的变量: private : int age;
04-19 22:33:16.576 30137-30137/cn.yuan.xiaoyu I/所有的变量: private : java.lang.String name;
* 获取一个类的实现接口或者父类的变量及其权限和类型
 Class<?> class1 = null;
        try {
            class1 = Class.forName("cn.yuan.xiaoyu.testmodule.bean.TestObjBean");
            Field[] field = class1.getFields();
            for (int i = 0; i < field.length; i++) {
                // 权限修饰符
                int mo = field[i].getModifiers();
                String priv = Modifier.toString(mo);
                // 属性类型
                Class<?> type = field[i].getType();
                L.i("所有的变量", priv + " : " + type.getName() + " " + field[i].getName() + ";");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
* 获取一个类的全部方法
  Class<?> class1 = null;
        try {
            class1 = Class.forName("cn.yuan.xiaoyu.testmodule.bean.TestObjBean");
            Method method[] = class1.getMethods();
            for (int i = 0; i < method.length; ++i) {
                Class<?> returnType = method[i].getReturnType();
                Class<?> para[] = method[i].getParameterTypes();
                int temp = method[i].getModifiers();
                System.out.print(Modifier.toString(temp) + " ");
                System.out.print(returnType.getName() + "  ");
                System.out.print(method[i].getName() + " ");
                System.out.print("(");
                for (int j = 0; j < para.length; ++j) {
                    System.out.print(para[j].getName() + " " + "arg" + j);
                    if (j < para.length - 1) {
                        System.out.print(",");
                    }
                }
                Class<?> exce[] = method[i].getExceptionTypes();
                if (exce.length > 0) {
                    System.out.print(") throws ");
                    for (int k = 0; k < exce.length; ++k) {
                        System.out.print(exce[k].getName() + " ");
                        if (k < exce.length - 1) {
                            System.out.print(",");
                        }
                    }
                } else {
                    System.out.print(")");
                }
                System.out.println();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
*输出值
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public boolean  equals (java.lang.Object arg0)
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public final java.lang.Class  getClass ()
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public int  hashCode ()
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public final native void  notify ()
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public final native void  notifyAll ()
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public void  onManClick ()
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public void  onWomenClick ()
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public void  setAge (int arg0)
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public void  setName (java.lang.String arg0)
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public java.lang.String  toString ()
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public final native void  wait () throws java.lang.InterruptedException 
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public final void  wait (long arg0) throws java.lang.InterruptedException 
04-19 22:46:57.912 9597-9597/cn.yuan.xiaoyu I/System.out: public final native void  wait (long arg0,int arg1) throws java.lang.InterruptedException 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值