java反射机制的概述

反射:
在运行时,我们可以获取任意一个类的所有方法和属性
在运行时,让我们调用任意一个对象的所有方法和属性
反射的前提:
要获取类的对象(Class对象)
获取字节码对象的三种方式:

public class Student {
	private String name;
	int age;
	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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	public Student() {
		super();
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

}

public class ReflectDemo {
	public static void main(String[] args) throws ClassNotFoundException {
		//通过Object的getClass()方法获取,必须要有对象
		Student s=new Student();
		//Class class1 = s.getClass();
		//通过类名获取字节码对象
		//Class class2 = Student.class;
		//static Class<?> forName(String className)
		Class forName = Class.forName("com.Reflect.Student");

	}
}

通过反射获取构造方法并使用:
Constructor<?>[]getConstructor():以数组的形式,返回所有public修饰的构造
ConstructorgetConstructor(Class<?>…parameterTypes):根据指定的参数,来获取具体的对象
T newInstance()
Constructor:
T newInstance(Object…initargs)

public class ReflectDemo02 {
	public static void main(String[] args) throws ReflectiveOperationException {
		Class forName = Class.forName("com.Reflect.Student");
		//method01(forName);
		//method02(forName);
		//method03(forName);
//		Object newInstance = forName.newInstance();
//		System.out.println(newInstance);
		
	}

	private static void method03(Class forName)
			throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
		Constructor constructor = forName.getConstructor(String.class,int.class);//获取有参构造,参数1为String,参数2为int;
		System.out.println(constructor);
		Object newInstance = constructor.newInstance("张三",22);
		System.out.println(newInstance);
	}

	private static void method02(Class forName)
			throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
		Constructor constructor = forName.getConstructor();//获取无参构造	
		System.out.println(constructor);
		Object newInstance = constructor.newInstance();
		System.out.println(newInstance);
	}

	private static void method01(Class forName) {
		Constructor[] constructors = forName.getConstructors();
		for(int i=0;i<constructors.length;i++){
			System.out.println(constructors[i]);
		}
	}

}

通过反射获取成员变量并使用
Filed[]getFileds():获取公共的成员变量
Filed getFild(String name):根据名称获取公共的成员变量对象
Filed[]DeclaredFields():获取所有的成员变量
Filed DeclaredFields(String name)
Filed:
Object get(Object obj):获取成员变量
void set(Object obj,Object value):通过成员变量对象,修改指定对象的值

public class ReflectDemo03 {
	public static void main(String[] args) throws ReflectiveOperationException {
		//获取学生类的字节码对象
		Class forName = Class.forName("com.Reflect.Student");
		//获取学生类的对象
		Object newInstance = forName.newInstance();
		//method01(forName);
		Field field = forName.getField("age");
		//System.out.println(field);
			field.set(newInstance, 21);
		Object object = field.get(newInstance);
		System.out.println(object);
	}

	private static void method01(Class forName) {
		Field[] fields = forName.getFields();
		for (int i = 0; i < fields.length; i++) {
			System.out.println(fields[i]);
		}
		Field[] declaredFields = forName.getDeclaredFields();
		for (int i = 0; i < declaredFields.length; i++) {
			System.out.println(declaredFields[i]);
		}
	}

}

通过反射获取私有成员变量并使用
File getDeclareadFileds(String name)

public class ReflectDemo04 {
	public static void main(String[] args) throws ReflectiveOperationException {
		//获取学生类的字节码对象
		Class forName = Class.forName("com.Reflect.Student");
		//获取学生对象
		Object newInstance = forName.newInstance();
		//获取私有的字段对象
		Field declaredField = forName.getDeclaredField("name");
		//System.out.println(declaredField);
		declaredField.setAccessible(true);//设置反射时取消java的访问检查,暴力访问
		declaredField.set(newInstance, "李四");
		Object object = declaredField.get(newInstance);
		
		System.out.println(object);
	}

}

通过反射获取成员方法并使用
Method getMethod(String name, Class<?>…parameterTypes)
Method:
Object invoke(Object obj,Object…args)

public class ReflectDemo05 {
	public static void main(String[] args) throws ReflectiveOperationException{
		//获取学生类的字节码对象
		Class forName = Class.forName("com.Reflect.Student");
		//获取学生的对象类
		Object newInstance = forName.newInstance();
		//method01(forName, newInstance);
		//method02(forName, newInstance);
		//获取无参有返回值的方法
		Method method = forName.getMethod("getName");
		Object invoke = method.invoke(newInstance);
		System.out.println(invoke);
		
	}

	private static void method02(Class forName, Object newInstance)
			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		//获取有参无返回值的方法
		Method method = forName.getMethod("setName", String.class);
		method.invoke(newInstance, "李四");
		System.out.println(newInstance);
	}

	private static void method01(Class forName, Object newInstance)
			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		//获取无参无返回值的方法
		Method method = forName.getMethod("method");
		Object invoke = method.invoke(newInstance);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值