反射获取成员变量 方法 构造方法

一个类的组成部分
1:变量
2:方法
3:构造方法
反射步骤
1:字节码文件
2:字节码对象

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.Field_Btn)
    Button FieldBtn;
    @BindView(R.id.Method_Btn)
    Button MethodBtn;
    @BindView(R.id.Concur_Btn)
    Button ConcurBtn;
    private Class mCls;
    private Student mStu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //首先得到字节码对象
        mStu = new Student();
        mCls = mStu.getClass();
    }

    @OnClick({R.id.Field_Btn, R.id.Method_Btn, R.id.Concur_Btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Field_Btn:
                getField();
                setField();
                break;
            case R.id.Method_Btn:
                getMethod();
                break;
            case R.id.Concur_Btn:
                getConstructors();
                break;
        }
    }

    private void getConstructors() {
        Constructor[] con = mCls.getDeclaredConstructors();
        for (Constructor c : con) {
            Log.e("构造方法", c.getName());
        }
        try {
            Constructor constructor = mCls.getDeclaredConstructor(String.class, String.class, int.class);
            Object o = constructor.newInstance("哈哈哈", "男",19);
            Log.e("获取构造", o.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getMethod() {
        //得到所有成员方法
        Method[] methods = mCls.getDeclaredMethods();
        for (Method m : methods) {
            Log.e("查看方法", m.getName());
        }
        try {
            Method info = mCls.getDeclaredMethod("getInfo", String.class, String.class, int.class);
            info.setAccessible(true);
            Object instance = mCls.newInstance();
            info.invoke(instance, "哈哈哈哈哈", "女", 18);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setField() {
        try {
            Field field = mCls.getDeclaredField("name");
            field.setAccessible(true);
            Student instance = (Student) mCls.newInstance();
            field.set(instance, "哈哈哈");
            Log.e("给属性赋值", instance.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //反射获取属性
    private void getField() {
        //得到该类的所有成员变量
        Field[] fields = mCls.getDeclaredFields();
        //所有的public修饰的变量
        for (Field f : fields) {
            Log.e("变量", "getField: " + f.getName());
        }
        try {
            Field field = mCls.getDeclaredField("lick");
            field.setAccessible(true);
            Object o = field.get(mStu);
            if (o instanceof String) {
                Log.e("通过反射获取属性的值", o.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Bean:

public class Student {
    private String name;
    private String sex;
    private int age;
    private String lick = "今天天气很好";

    public Student(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public Student() {
    }

    private Student(String name) {
        this.name = name;
    }

    public void getInfo(String name) {
        this.name = name;
    }

    public void getInfo(String name, String sex, int age) {
        Log.e("name和sex", name + ",sex=" + sex + ",age=" + age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

你可能感兴趣的:(反射获取成员变量 方法 构造方法)