五,反射

什么是反射?

是Java提供的一套API,可以对Java对象进行自我检查,可以检查对象的类型, 类型包含呢些属性, 包含哪些方法, 包含哪些构

造器等。

反射API还提供了,动态执行功能:包括动态加载类, 动态创建对象, 动态访问属性, 动态调用方法 等。

使用反射的目的一般是为了解除耦合! 简称:解耦

package reflect;

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

import Anno.RequestMapping;
import Anno.Test;

public class someReflectUse {
	public static void main(String[] args) throws Exception {
		test(5);
		// 1,动态的加载类
		Class one = Class.forName("java.lang.String");
		Class two = someReflectUse.class.getClassLoader().loadClass("java.lang.Integer");
		// 1.1
		// two.getClassLoader().getResource("");
		// two.getClassLoader().getResourceAsStream("");
		// two.getClass().getResource("");
		// two.getClassLoader().getResourceAsStream("");
		// 2,动态的创建对象
		Object obj = one.newInstance();
		// 3,动态的访问属性,设置属性
		Class happyNewYear = Class.forName("reflect.HappyNewYear");

		Object objHNY = happyNewYear.newInstance();

		Field pub = happyNewYear.getDeclaredField("pub");
		Object val = pub.get(objHNY);
		System.out.println(val);
		pub.set(objHNY, 5);
		val = pub.get(objHNY);
		System.out.println(val);
		
		Field pri = happyNewYear.getDeclaredField("pri");
		int bool=pri.getModifiers();
		pri.setAccessible(true);
		Object valPri = pri.get(objHNY);
		System.out.println(valPri);
		
		Object anno=pri.getAnnotations();
		Test t=pri.getAnnotation(Test.class);
		if(t==null) {}
		// 4,动态的实现方法
		Method plusSum = happyNewYear.getMethod("sumPlus", null);
		plusSum.invoke(objHNY, null);
		
		Method sumFrom=happyNewYear.getMethod("sumFrom", new Class[] {int.class,int.class});
		Object objReturn =sumFrom.invoke(objHNY, new Object[] {1,2});
		System.out.println(objReturn);
		//5,动态的实现构造方法
		Constructor c1=happyNewYear.getConstructor(int.class);
		Object asClassNewInstance=c1.newInstance(2);
	}

	public static void test(Object obj) {
		// 1,是什么类型,包含哪些属性,包含哪些方法,包含哪些构造器
		Class objClass = obj.getClass();
		System.out.println(objClass);//
		Field[] fields = objClass.getDeclaredFields();
		for (Field field : fields) {
			System.out.println(field.getName() + "/" + field.getType());//
		}
		Method[] methods = objClass.getMethods();
		for (Method method : methods) {
			System.out.println(method.getName()
			+ method.getReturnType() + method.getParameters());//
		}
		Constructor[] constructors = objClass.getConstructors();
		for (Constructor constructor : constructors) {
			System.out.println(constructor.getName());//
		}
	}
}
package reflect;

import Anno.RequestMapping;
import Anno.Test;

@RequestMapping("/user")
public class HappyNewYear {
	private int pri;
	public int pub;
	public int plus;
	public static final int STA = 10;
	public String str;

	public HappyNewYear() {
		pri = 10;
		pub = 20;
		str = "OK";
	}
	public HappyNewYear(int a) {
		pri = 10;
		pub = 20;
		str = "OK";
		plus=a;
	}
	@RequestMapping("/user.do")
	@Test
	public void sumPlus() {
		plus = pri + pub;
		System.out.println(plus);
	}

	public int sum() {
		return pri + pub;
	}
	public int sumFrom(int a,int b) {
		return a+b;
	}

}

 

 

 

你可能感兴趣的:(JAVA)