反射--进阶理解

1.首先我们先将自己写的一个javaProtect打成jar包


2.Reflect01中的类信息

package sun.com;

public class Student {
	public Student(String s){
		System.out.println("调用共有的构造方法");
	}
	Student(int a){
		System.out.println("调用默认的构造方法");
	}
	private Student(){
		System.out.println("调用私有构造方法");
	}
	private static int x;
	private String name;
	private String sex;
	public int money;
	protected int date;
	public static void method1(){
		System.out.println("method1");
	}
	private void method2(String string) {
		System.out.println("method2"+string);
	}
	protected void method3(String name,int money) {
		System.out.println("method3"+name+"----"+money);
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", sex=" + sex + ", money=" + money + ", date=" + date + ", x=" + x + "]";
	}
	
	public static void main(String[] args) {
		System.out.println("Main方法调用");
	}
	
}

Teacher类的

package sun.com;

public class Teacher {
	private String name;
	public void show(){
		System.out.println("name");
	}
}

现在正式进入反射阶段

1.先将大体分类说一下


2.现在运用反射的机制获取,构造方法(Main1)

package com.sun;

import java.io.FileReader;
import java.lang.reflect.Constructor;
import java.util.Properties;

public class Main1 {

	public static void main(String[] args) throws Exception {
		
		Properties properties=new Properties();
		properties.load(new FileReader("reflect.properties"));
		System.out.println(properties.getProperty("className"));
		
		// 获取所有的构造方法
		Class clazz = Class.forName("sun.com.Student");
		System.out.println("--------------获取public构造方法,参数的Class 对象--------------");
		Constructor constructor = clazz.getConstructor(String.class);		
		System.out.println(constructor);
		System.out.println("--------------获取所有的构造方法--------------");
		Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
		for (Constructor constructor2 : declaredConstructors) {
			System.out.println(constructor2);
		}
		System.out.println("--------------获取私有构造方法,并且调用私有构造方法--------------");
//		clazz.newInstance();这一种只能调用默认的无参的public 构造参数
		Constructor constructor2 = clazz.getDeclaredConstructor(null);
		constructor2.setAccessible(true);// 允许调用
		constructor2.newInstance(null);
		System.out.println(constructor2);
		System.out.println("--------------获取私有构造方法,并且调用私有构造方法--------------");
		Constructor declaredConstructor = clazz.getDeclaredConstructor(int.class);
		System.out.println(declaredConstructor);
		declaredConstructor.setAccessible(true);// 允许调用
		declaredConstructor.newInstance(1);
	}

}

3.运用发射获取变量

package com.sun;

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

public class Main2 {
	public static void main(String[] args) throws Exception {
		Class clazz= Class.forName("sun.com.Student");
		System.out.println("获取所有的共有变量");
		Field[] fields = clazz.getFields();
		for (Field field : fields) {
			System.out.println(field);
		}
		
		Field[] declaredFields = clazz.getDeclaredFields();
		System.out.println("获取所有变量");
		for (Field field : declaredFields) {
			System.out.println(field);
		}
		System.err.println("------------------------------------------");
		System.out.println("获取私有的变量 step1");
		Field fieldName = clazz.getDeclaredField("name");
		System.out.println("私有的变量"+fieldName);
		System.out.println("在获取构造方法,但这个是私有构造 step2");
		Constructor PrivatedConstructor = clazz.getDeclaredConstructor(int.class);
		//赋予可以调用这个私有方法的权限 access 获取
		System.out.println("给私有或者protected 权限,能调用 step3");
		PrivatedConstructor.setAccessible(true);
		//利用构造方法创建对象 其实我们在使用new 创建对象的时候 new Student默认走的是public Student()这个构造方法,
		//只不过这里是用构造方法直接构造出来一个 对象
		System.out.println("创建出来一个对象  step4");
		Object newInstance = PrivatedConstructor.newInstance(1);
		//获得变量也是私有的,要想更改,也需要赋予权限
		/**
		 * Object newInstance = PrivatedConstructor.newInstance(1);
		 * fieldName.set(newInstance, (Object)"nihao");
		 * 这两行按照我们正常的理解就是 Object new instance=new Student(),instance.fieldName="nihao";
		 */
		fieldName.setAccessible(true);
		System.out.println("给对象的属性赋值  step5");
		fieldName.set(newInstance, (Object)"nihao");
		System.out.println(newInstance);
		System.out.println(fieldName);
		System.err.println("------------------------------------------");
		System.out.println("获取static变量");
		Field staticField = clazz.getDeclaredField("x");
		System.out.println(staticField);
		staticField.setAccessible(true);
		staticField.set(newInstance, 1);
		//未写setget方法,toString 检测一下
		System.out.println(newInstance.toString());
		System.out.println("获取public变量");
		Field publicField = clazz.getDeclaredField("money");
		System.out.println(publicField);
		
	}
}

4.利用反射获取类中所有的方法

package com.sun;

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

public class Main3Method {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		/**
		 * 修饰符有public、protected、private,当然还有缺省的(default)修饰符,缺省的就是方法名前无任何修饰符
		 */
		Class clazz = Class.forName("sun.com.Student");
		Constructor morenConstructor = clazz.getDeclaredConstructor(int.class);
		//这里调用的是缺省修饰符的 构造方法,同样要赋予执行权限
		morenConstructor.setAccessible(true);
		Object newInstance = morenConstructor.newInstance(1);
		
		Method method1 = clazz.getMethod("method1", new Class[]{});
		System.out.println(method1);
		//public static void sun.com.Student.method1()
		/**
		 * 可以看出如果是static 静态方法,那么调用这个方法的时候,前面的对象可以加,亦可以不加,也可以直接加 clazz Class的对象,Student的类名
		 */
		method1.invoke(null, null);
		method1.invoke(newInstance, null);
		method1.invoke(clazz, null);
		System.out.println("------------------------------调用私有方法--------------------------");
		Method privatedMethod = clazz.getDeclaredMethod("method2",String.class);
		privatedMethod.setAccessible(true);
		privatedMethod.invoke(newInstance, "method2");
		System.out.println("------------------------------调用protect方法--------------------------");
		/**
		 * 这两种寻找方法的方法都行,Class 有构造函数
		 */
		Method publicMethod3 = clazz.getDeclaredMethod("method3",String.class,int.class);
		Method protectedMethod3 = clazz.getDeclaredMethod("method3",new Class[]{String.class,int.class});
		
		publicMethod3.setAccessible(true);
		protectedMethod3.setAccessible(true);
		/**
		 * 测试
		 */
		publicMethod3.invoke(newInstance, "method3",5);
		protectedMethod3.invoke(newInstance, "method3",6);
		/**
		 * 接收的是object数组,传过去的要是object数组
		 */
		protectedMethod3.invoke(newInstance, new Object[]{"method3",7});
		System.out.println("----------------------------调用主方法-其实也是static,他这个方法属于类,前面不加对象也可以,加了也可以,加类clazz也可以---------------------------");
		Method MainMethod = clazz.getDeclaredMethod("main", new Class[]{String[].class});
		Method MainMethod1 = clazz.getDeclaredMethod("main", String[].class);
		MainMethod.invoke(null, (Object)new String[]{});
		MainMethod1.invoke(null, new Object[]{new String[]{}});
		MainMethod1.invoke(newInstance, new Object[]{new String[]{}});
		MainMethod1.invoke(clazz, new Object[]{new String[]{}});
		System.out.println(MainMethod);
		System.out.println(MainMethod1);
//		Object[] object=new Object[]{"name",5};
//		System.out.println(object[0].getClass());
	}

}

5.我们可以使用properties配置文件获取我们想要的进行反射的类

reflect.properties文件内容如下

className="sun.com.Student";

使用方法

		Properties properties=new Properties();
		properties.load(new FileReader("reflect.properties"));
		System.out.println(properties.getProperty("className"));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值