Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

java反射详解

$
0
0

为啥突然写这个东西呢,是因为今天看到了通过反射更改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 

作者:EaskShark 发表于2017/4/20 11:02:48 原文链接
阅读:178 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>