框架:半成品软件。可以在框架的基础上进行软件开发,简化编码 反射:将类的各个组成部分封装为其他对象,这就是反射机制 好处: 1. 可以在程序运行过程中,操作这些对象。 2. 可以解耦,提高程序的可扩展性。
通过下面这张图可理解到反射机制的本质实际是得到class对象后,反向获取Student对象的各种信息
ReflectDemo1.java
package reflect; import domain.Person; public class ReflectDemo1 { public static void main(String args[]) throws Exception{ //1.Class.forname("全类名") Class cls1 = Class.forName("domain.Person"); System.out.println(cls1); //2.类名.class Class cls2 = Person.class; System.out.println(cls2); //3.对象.getClass() Person p = new Person(); Class cls3 = p.getClass(); System.out.println(cls3); } }输出结果:包名.类名 class domain.Person class domain.Person class domain.Person
例子:
package reflect; import domain.Person; import java.lang.reflect.Field; /** * Created by Administrator on 2020/7/3. */ public class ReflectDemo2 { public static void main(String args[]) throws Exception { //0.获取Person的Class对象 Class personClass = Person.class; /** * 1.获取成员变量们 * Field[] getFields() * Field getField(String name) * * Field[] getDeclaredFields() * Field getDeclaredField(String name) */ //1.Field[] getFields() 获取所有public修饰的成员变量 Field[] fields = personClass.getFields(); for (Field field : fields) { System.out.println(field); } System.out.println("========"); //2.Field getField(String name) Field a = personClass.getField("a"); //获取成员变量a的值 Person p = new Person(); Object value = a.get(p); System.out.println(value); //设置a的值 a.set(p, "张三"); System.out.println(p); System.out.println("========="); //Field[] getDeclaredFields(),获取所有的成员变量,不考虑修饰符 Field[] declaredFields = personClass.getDeclaredFields(); for (Field declaredField : declaredFields) { System.out.println(declaredField); } //Field getDeclaredField(String name) Field d = personClass.getDeclaredField("d"); //忽略访问权限修饰符的安全检查 d.setAccessible(true); //暴露反射 Object value2 = d.get(p); System.out.println(value2); } }运行结果:
public java.lang.String domain.Person.a ======== null Person{name='null', age=0, a='张三', b='null', c='null', d='null'} ========= private java.lang.String domain.Person.name private int domain.Person.age public java.lang.String domain.Person.a protected java.lang.String domain.Person.b java.lang.String domain.Person.c private java.lang.String domain.Person.d null例子:
package reflect; import domain.Person; import java.lang.reflect.Constructor; import java.lang.reflect.Field; /** * Created by Administrator on 2020/7/3. */ public class ReflectDemo3 { public static void main(String args[]) throws Exception { //0.获取Person的Class对象 Class personClass = Person.class; /* * 1.获取构造方法们 * Constructor<?>[] getConstructors() * Constructor<T> getConstructor(类<?>... parameterTypes) * * Constructor<T> getDeclaredConstructors(类<?>... parameterTypes) * Constructor<?>[] getDeclaredConstructors() * */ //Constructor<T> getConstructor(类<?>... parameterTypes) Constructor constructor = personClass.getConstructor(String.class, int.class); System.out.println(constructor); //创建对象 Object person = constructor.newInstance("张三",23); System.out.println(person); System.out.println("======"); Constructor constructor1 = personClass.getConstructor(); System.out.println(constructor1); //创建对象 Object person1 = constructor1.newInstance(); System.out.println(person1); Object o = personClass.newInstance(); System.out.println(o); } }运行结果:
public domain.Person(java.lang.String,int) Person{name='张三', age=23, a='null', b='null', c='null', d='null'} ====== public domain.Person() Person{name='null', age=0, a='null', b='null', c='null', d='null'} Person{name='null', age=0, a='null', b='null', c='null', d='null'}例子:
package reflect; import domain.Person; import java.lang.reflect.Constructor; import java.lang.reflect.Method; /** * Created by Administrator on 2020/7/3. */ public class ReflectDemo4 { public static void main(String args[]) throws Exception { //0.获取Person的Class对象 Class personClass = Person.class; /* 2.获取成员方法们: Method[] getMethods() Method getMethod(String name,类<?>... parameterTypes) Method[] getDeclaredMethods() Method getDeclaredMethod(String name, 类<?>... parameterTypes) * */ //获取指定名称的方法 Method eat_method = personClass.getMethod("eat"); Person p = new Person(); //执行方法 eat_method.invoke(p); Method eat_method2 = personClass.getMethod("eat",String.class); eat_method2.invoke(p,"饭"); System.out.println("============="); //获取所有public修饰的方法 Method[] methods = personClass.getMethods(); for(Method method : methods){ // method.setAccessible(true); System.out.println(method); String name = method.getName(); System.out.println(name); } //获取类名 String className = personClass.getName(); System.out.println(className); } }运行结果:
eat.... eat....饭 ============= public java.lang.String domain.Person.toString() toString public java.lang.String domain.Person.getName() getName public void domain.Person.setName(java.lang.String) setName public int domain.Person.getAge() getAge public void domain.Person.eat(java.lang.String) eat public void domain.Person.eat() eat public void domain.Person.setAge(int) setAge public final void java.lang.Object.wait() throws java.lang.InterruptedException wait public final void java.lang.Object.wait(long,int) 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 domain.Person