Java反射

Java反射

Student文件

package reflex;

public class Student {
	private String name;
	private int age;
	private String sex;
	private int height;
	private int weight;
	public Student(String name, int age, String sex, int height, int weight) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.height = height;
		this.weight = weight;
	}
	public Student() {
		super();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
		int currentAge = 0;
		if (currentAge == 10)
			System.out.println("实现");
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
}

访问成员变量文件

package reflex;

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

public class Hello {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Class<?> class1 = Class.forName("reflex.Student");
			//使用反射所必须的要创建的class对象
			
			System.out.println((Student)class1.newInstance());
			Method[] list = class1.getDeclaredMethods();
			for(Method method:list) {
				System.out.println("获取方法名"+method.getName());
				System.out.println("获取返回函数类型"+method.getReturnType());
				System.out.println("获取传入参数类型"+method.getParameterTypes());
			}
			//==================================================
			//对成员变量进行控制,这个方法比较好
			Object newInstance = class1.newInstance();
			Field f = class1.getDeclaredField("name");  //访问变量
			Field f1 = class1.getDeclaredField("age");  //访问变量
			f1.setAccessible(true);   //使f1所代表的私有变量可以设置
			f.setAccessible(true);    //使f所代表的私有变量可以设置
			f.set(newInstance, "zhangsan");
			f1.set(newInstance, 12);
			Student student = (Student) newInstance;
			//=================================================
			//invoke方法的使用,与上面的方法没有什么不同
			Method method = class1.getMethod("setAge", int.class);
//			method.setAccessible(true);
			method.invoke(newInstance, (Object)10);
			
			//===================================================
			System.out.println(student.getName());
			System.out.println(student.getAge());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值