Java反射机制的常用方法

1.获取成员变量
* Filed[] getFields(); //获取所有public修饰的成员变量
* Filed getField(String name); //获取指定name的public成员变量
* Field[] getDeclaredFields(); //获取所有成员变量,不考虑修饰符
* Filed getDeclaredField(String name); //获取指定成员变量,不考虑修饰符
* 获取值:get(Object obj);
* 设置值:set(Object obj, Object value);
* 忽略访问权限修饰符的安全检查:setAccessible(true);//暴力反射
2.获取构造方法
* Constructor<?>[] getConstructors();
* ConStructor getConstructor(类<?>…parameterTypes);
* Constructor<?>[] getDeclaredConstructors();
* ConStructor getDeclaredConstructor(类<?>…parameterTypes);
* 创建对象:Class.newInstance(); //调用无参构造
* //调用有参构造
* Constructor.newInstance(Object… initarges);
3.获取成员方法
* Method[] getMethods();
* Method getMethod(String name, 类<?>…parameterTypes);
* Method[] getDeclaredMethods(String name, 类<?>…parameterTypes);
* Method getDeclaredMethod();
* 执行方法:Object invoke(Object obj, Object… arges);
4.获取类名
* String getName();
5.获取类对象三种方式
* source源代码阶段:Class.forname(“全类名”);
* Class类对象阶段(加载进内存):类名.class
* Runtime运行时阶段:对象.getClass();

代码示例:

以操纵Person类为例

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

public class Reflect_Person {
	/**
	 * 1.获取成员变量
	 * Filed[] 	getFields();					//获取所有public修饰的成员变量
	 * Filed 	getField(String name);			//获取指定name的public成员变量
	 * Field[]	getDeclaredFields();			//获取所有成员变量,不考虑修饰符
	 * Filed	getDeclaredField(String name);	//获取指定成员变量,不考虑修饰符
	 * 获取值:get(Object obj);
	 * 设置值:set(Object obj, Object value);
	 * 忽略访问权限修饰符的安全检查:setAccessible(true);//暴力反射
	 * 2.获取构造方法
	 * Constructor<?>[] getConstructors();
	 * ConStructor<T> 	getConstructor(类<?>...parameterTypes);
	 * Constructor<?>[] getDeclaredConstructors();
	 * ConStructor<T> 	getDeclaredConstructor(类<?>...parameterTypes);
	 * 创建对象:Class.newInstance();			//调用无参构造
	 * 			Constructor<T>.newInstance(Object... initarges);//调用有参构造
	 * 3.获取成员方法
	 * Method[] getMethods();
	 * Method 	getMethod(String name, 类<?>...parameterTypes);
	 * Method[] getDeclaredMethods(String name, 类<?>...parameterTypes);
	 * Method 	getDeclaredMethod();
	 * 执行方法:Object invoke(Object obj, Object... arges);
	 * 4.获取类名
	 * String getName();
	 * 5.获取类对象三种方式:
	 * 	source源代码阶段:Class.forname("全类名");
	 * 	Class类对象阶段(加载进内存):类名.class
	 * 	Runtime运行时阶段:对象.getClass();
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws NoSuchMethodException 
	 * @throws InstantiationException 
	 * @throws InvocationTargetException 
	 */
	
	public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException {
		Class p = Person.class;//获取Person的Class对象
		
		Field[] fields = p.getFields();
		for(Field a : fields) System.out.println(a);
		Field field = p.getField("name");
		System.out.println(field);
		System.out.println("========================");
		fields = p.getDeclaredFields();
		for(Field a : fields) System.out.println(a);
		field = p.getDeclaredField("age");
		System.out.println(field);
		System.out.println("========================1");
		field = p.getDeclaredField("age");
		field.setAccessible(true);//忽略访问修饰符的安全检查,暴力反射
		Person one = new Person();
		Object value = field.get(one);
		System.out.println(value);
		field.set(one, 20);
		System.out.println(one.toString());
		System.out.println("=========================2");
		Method[] methods = p.getDeclaredMethods();
		for(Method a : methods) System.out.println(a);
		Method method = p.getDeclaredMethod("play", String.class);
		method.setAccessible(true);
		System.out.println(method);
		System.out.println("==========================3");
		method = p.getDeclaredMethod("eat", null);
		System.out.println(method);
		Object obj = p.newInstance();//获取一个Person对象来执行method方法,调用的是无参构造方法
		method.invoke(obj, null);
		method.invoke(one, null);
		System.out.println("==========================4");
		methods = p.getMethods();
		for(Method a : methods) System.out.println(a);
		System.out.println("==========================5");
		Constructor<String>[] cons = p.getDeclaredConstructors();
		for(Constructor a : cons) System.out.println(a);
		Constructor<String> con = p.getDeclaredConstructor(null);
		System.out.println(con);
		con = p.getDeclaredConstructor(String.class,int.class);
		con.setAccessible(true);
		obj = con.newInstance("李四",23);
		System.out.println(obj);
		System.out.println("==========================6");
		methods = p.getMethods();
		for(Method a : methods) {
			System.out.println(a);
			System.out.println(a.getName());
		}
		System.out.println("===========================7");
		String className = p.getName();
		System.out.println(className);
	}
}

Person类:

public class Person {
	public String name;
	private int age = 18;
	
	public Person() {
		System.out.println("无参构造");
	}
	Person(String name,int age){
		this.age = age;
		this.name = name;
		System.out.println("有参构造");
	}
	
	public void eat() {
		System.out.println(this.name+"正在吃饭。。。");
	}
	protected void play(String a){
		System.out.println(this.name+"在敲代码。。。");
	}
	
	@Override
	public String toString() {
		return "姓名:"+this.name+",年龄:"+this.age;
	}
}

运行结果

public java.lang.String web01_反射.Person.name
public java.lang.String web01_反射.Person.name

========================
public java.lang.String web01_反射.Person.name
private int web01_反射.Person.age
private int web01_反射.Person.age
========================1
无参构造
18
姓名:null,年龄:20
=========================2
public java.lang.String web01_反射.Person.toString()
protected void web01_反射.Person.play(java.lang.String)
public void web01_反射.Person.eat()
protected void web01_反射.Person.play(java.lang.String)
==========================3
public void web01_反射.Person.eat()
无参构造
null正在吃饭。。。
null正在吃饭。。。
==========================4
public java.lang.String web01_反射.Person.toString()
public void web01_反射.Person.eat()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
==========================5
public web01_反射.Person()
web01_反射.Person(java.lang.String,int)
public web01_反射.Person()
有参构造
姓名:李四,年龄:23
==========================6
public java.lang.String web01_反射.Person.toString()
toString
public void web01_反射.Person.eat()
eat
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait
public final void java.lang.Object.wait() throws java.lang.InterruptedException
wait
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait
public boolean java.lang.Object.equals(java.lang.Object)
equals
public native int java.lang.Object.hashCode()
hashCode
public final native java.lang.Class java.lang.Object.getClass()
getClass
public final native void java.lang.Object.notify()
notify
public final native void java.lang.Object.notifyAll()
notifyAll
===========================7
web01_反射.Person

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值