博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java反射
阅读量:5217 次
发布时间:2019-06-14

本文共 6440 字,大约阅读时间需要 21 分钟。

1. 定义

JAVA反射(放射)机制:“程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言”。从这个观点看,Perl,Python,Ruby是动态语言,C++,Java,C#不是动态
语言。但是JAVA有着一个非常突出的动态相关机制:Reflection,用在Java身上指的是我们可以于运行时加载、探知、使用编译期间完全未知的classes。换句话说,Java程序
可以加载一个运行时才得知名称的class,获悉其完整构造(但不包括methods定义),并生成其对象实体、或对其fields设值、或唤起其methods。其中,有一个Class类型,
它是实现反射的起源,针对任何您想探勘的类,唯有先为它产生一个Class 对象,接下来才能经由后者唤起为数十多个的Reflection APIs。而关于jvm如何加载类的过程,见文
章: 。

2. 实例应用

案例接口
public interface China {	public void sayHello(String name, Integer age);}
案例类
public class Person implements China{	private int age;		private String name;	public int getAge() {		return age;	}	public final void setAge(Integer age) {		this.age = age;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	@Override	public String toString() {		return "Person [age=" + age + ", name=" + name + "]";	}	public Person(int age, String name) {		super();		this.age = age;		this.name = name;	}		public Person(int age){		super();		this.age = age;	}		public Person(String name){		super();		this.name = name;	}	public Person() {		super();	}	@Override	public void sayHello(String name, Integer age) {		System.out.println("hello "+ name + " : " + age);	}}
测试类
public class ReflectTest {	/**	 * 通过Class.forName和ClassLoader来加载类	 * @throws ClassNotFoundException	 */	@Test	public void test1() throws ClassNotFoundException{		Class
demo1 = null; Class
demo2 = null; Class
demo3 = null; Class
demo4 = null; demo1 = Class.forName("cn.test.reflect.Demo"); demo2 = new Demo().getClass(); demo3 = Demo.class; demo4 = Demo.class.getClassLoader().loadClass("cn.test.reflect.Demo"); System.out.println(demo1.getName()); System.out.println(demo2.getName()); System.out.println(demo3.getName()); System.out.println(demo4.getName()); } /** * 通过Class.forName("").newInstance()生成新对象,调用的是无参构造函数 * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException */ @Test public void test2() throws ClassNotFoundException, InstantiationException, IllegalAccessException{ Class
demo = Class.forName("cn.test.reflect.Person"); Person person = (Person) demo.newInstance(); person.setAge(12); person.setName("张三"); System.out.println(person); } /** * 打印出该类所有的信息:属性,方法,构造函数 * @throws ClassNotFoundException */ @Test public void test3() throws ClassNotFoundException{ Class
demo = Class.forName("cn.test.reflect.Person"); System.out.println("类属性"); Field[] fields = demo.getDeclaredFields(); for(Field field : fields){ int mo = field.getModifiers(); System.out.print(Modifier.toString(mo)+" "); System.out.print(field.getType().getName()+" "); System.out.print(field.getName()); System.out.println(); } System.out.println("所有方法"); Method[] methods = demo.getDeclaredMethods(); for(Method method : methods){ Class
[] parms = method.getParameterTypes(); int mo = method.getModifiers(); System.out.print(Modifier.toString(mo)+" "); System.out.print(method.getName()+"("); int i=1; for(Class
parm : parms){ System.out.print(parm.getName() + " arg"+i++); if(i<=parms.length){ System.out.print(","); } } System.out.print(")"); System.out.println(); } Constructor
[] constructors = demo.getConstructors(); System.out.print("构造方法:"); for(Constructor
constructor : constructors){ System.out.println(); Class
[] parms = constructor.getParameterTypes(); int mo = constructor.getModifiers(); System.out.print(Modifier.toString(mo)+" "); System.out.print(constructor.getName()+"("); int i=1; for(Class
parm : parms){ System.out.print(parm.getName() + " arg"+i++); if(i<=parms.length){ System.out.print(","); } } System.out.print("){}"); } } /** * 获取接口 * @throws ClassNotFoundException */ @Test public void test4() throws ClassNotFoundException{ Class
demo = Class.forName("cn.test.reflect.Person"); Class
[] intes = demo.getInterfaces(); for(Class
interf : intes){ System.out.println(interf.getName()); } } /** * 获取父类 * @throws ClassNotFoundException */ @Test public void test5() throws ClassNotFoundException{ Class
demo = Class.forName("cn.test.reflect.Person"); Class
superClass = demo.getSuperclass(); System.out.println(superClass.getName()); } /** * 反射调用类的中任意方法,需要知道方法名 * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException * @throws SecurityException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalArgumentException */ @Test public void test6() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{ Class
demo = Class.forName("cn.test.reflect.Person"); Person person = (Person) demo.newInstance(); Method sayHelloMethod = demo.getMethod("sayHello", String.class, Integer.class); sayHelloMethod.invoke(person, "张三", 20); Method setAgeMethod = demo.getMethod("setAge", Integer.class); setAgeMethod.invoke(person, 2); Method getAgeMethod = demo.getMethod("getAge"); System.out.println(getAgeMethod.invoke(person)); Method setNameMethod = demo.getMethod("setName", String.class); setNameMethod.invoke(person, "李四"); Method getNameMethod = demo.getMethod("getName"); System.out.println(getNameMethod.invoke(person)); Method toStringMethod = demo.getMethod("toString"); System.out.println(toStringMethod.invoke(person)); } /** * 反射设置属性值 * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException * @throws NoSuchMethodException * @throws SecurityException * @throws IllegalArgumentException * @throws InvocationTargetException * @throws NoSuchFieldException */ @Test public void test7() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException{ Class
demo = Class.forName("cn.test.reflect.Person"); Person person = (Person) demo.newInstance(); Field ageField = demo.getDeclaredField("age"); ageField.setAccessible(true); ageField.set(person, 10); Field nameField = demo.getDeclaredField("name"); nameField.setAccessible(true); nameField.set(person, "王武"); System.out.println(person); }}

转载于:https://www.cnblogs.com/marcotan/p/4256869.html

你可能感兴趣的文章
javascript keycode大全
查看>>
前台freemark获取后台的值
查看>>
log4j.properties的作用
查看>>
滴滴快车历史奖励政策:含工作日和周末的高峰奖励、翻倍奖励【历史政策】...
查看>>
文件操作类2
查看>>
游戏偶感
查看>>
Leetcode: Unique Binary Search Trees II
查看>>
phpcurl类
查看>>
C++ FFLIB 之FFDB: 使用 Mysql&Sqlite 实现CRUD
查看>>
Spring-hibernate整合
查看>>
cocos2dx 3.3环境搭建
查看>>
Spring DI
查看>>
c++ map
查看>>
exit和return的区别
查看>>
ThinkPHP5.1安装
查看>>
在Mac OS X中搭建STM32开发环境(2)
查看>>
vi 常用命令
查看>>
js += 含义(小知识)
查看>>
B2321 [BeiJing2011集训]星器 数学&&物理
查看>>
反馈的基础概述
查看>>