反射(二)---newInstance、get\set、invoke

Person

package com.harvic.blog_reflect_3;

import android.util.Log;

import java.util.ArrayList;

public class Person {

	private int age;
	private String name;

	public Person() {
	}

	private Person(int age, String name) {
		this.age = age;
		this.name = name;
	}

	private Person(Integer age, String name) {
		this.age = age;
		this.name = name;
	}

	public int getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

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

	public Boolean testInvoke(Integer age, String name) {
		Log.d("qijian", "得到参数age:" + age + "   name:" + name);
		return true;
	}

}

MyActivity

package com.harvic.blog_reflect_3;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import android.support.v4.app.Fragment;
import android.widget.PopupWindow;

public class MyActivity extends Activity {
	private String TAG = "qijian";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					newInstanceDemo();


					getFieldsDemo();

					fieldgetset();


					methodInvokeDemo();

				} catch (Exception e) {
					Log.e(TAG, e.getMessage());
				}
			}
		});
	}

	/**
	 * 2、通过反射获取实例
	 */
	public void newInstanceDemo() throws Exception {
		Class<?> clazz = Person.class;
		Constructor<?> constructor = clazz.getDeclaredConstructor(
				Integer.class, String.class);
		constructor.setAccessible(true);

		// 构造实例一
		Person person1 = (Person) constructor.newInstance(new Integer(30),
				new String("harvic"));
		Log.d(TAG, "构造的参数为:" + person1.getName() + "  " + person1.getAge());

		// 构造实例二
		Person person2 = (Person) constructor.newInstance(50, "qijian");
		Log.d(TAG, "构造的参数为:" + person2.getName() + "  " + person2.getAge());

		try {
			// 构造实例三
			Person person3 = (Person) constructor.newInstance();
			person3.setAge(30);
			person3.setName("qijian");
			Log.d(TAG, "构造的参数为:" + person3.getName() + "  " + person3.getAge());
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}
	}

	/**
	 * 4、获取属性名
	 */
	public void getFieldsDemo() throws Exception {
		Class<?> clazz = Person.class;
		// 1、枚举
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			Log.d(TAG, "枚举到的field:" + field.getName());
		}
		// 2、指定名称
		Field field = clazz.getDeclaredField("age");
		field.setAccessible(true);
		Log.d(TAG, "得到age对应的field:" + field.getName());

	}

	/**
	 * 5、获取或设置指定类对象中某变量的值时,可以使用Field中的get,set方法
	 */
	public void fieldgetset() throws Exception {
		Class<?> clazz = Person.class;
		Constructor<?> constructor = clazz.getConstructor();
		Person person = (Person) constructor.newInstance();

		// 示例1:get() set()函数
		Field fName = clazz.getDeclaredField("name");
		fName.setAccessible(true);
		fName.set(person, "qijian");
		String val = (String) fName.get(person);
		// 利用set设置
		Log.d(TAG, "fieldName:" + val + "   personName:" + person.getName());

		// 示例2:getInt()与setInt()
		Field fAge = clazz.getDeclaredField("age");
		fAge.setAccessible(true);
		fAge.setInt(person, 20);
		Log.d(TAG,
				"fieldAge:" + fAge.getInt(person) + "   personAge:"
						+ person.getAge());

		// 示例3:给声明为原始类型的变量使用get()、set()
		Field fAge2 = clazz.getDeclaredField("age");
		fAge2.setAccessible(true);
		fAge2.set(person, 30);
		Integer ageval = (Integer) fAge2.get(person);
		Log.d(TAG,
				"fieldAge:" + ageval.toString() + "   personAge:"
						+ person.getAge());

	}

	/**
	 * 7、Invoke函数无疑是Method类中最重要的方法,它的功能是用于执行Method对象所对应的函数
	 */
	public void methodInvokeDemo() throws Exception {
		Class<?> clazz = Person.class;
		Person person = new Person();
		Method method = clazz.getDeclaredMethod("testInvoke", Integer.class,
				String.class);

		method.setAccessible(true);
		Boolean result = (Boolean) method.invoke(person, 25, "I m harvic");
		Log.d(TAG, "执行结果:" + result);
	}

}

反射(二)---newInstance、get\set、invoke_第1张图片

你可能感兴趣的:(反射(二)---newInstance、get\set、invoke)