反射:框架设计的灵魂
框架:半成品软件。可以在框架的基础上进行软件开发,简化编码*反射:将类的各个组成部分封装为其他对象,这就是反射机制
好处
1.可以在程序运行过程中,操作这些对象。
2.可以解耦,提高程序的可扩展性。
获取Class对象的方式:`
public static void main(String[] args) throws Exception {
//1.Class.forName("全类名")
Class cls1 = Class.forName("com.ying.pojo.Person");
System.out.println(cls1);
//2.类名.class
Class cls2= Person.class;
System.out.println(cls2);
//3.对象.getClass();
Person person=new Person();
Class cls3 = person.getClass();
System.out.println(cls3);
//====比较三个对象
System.out.println(cls1 == cls2);//true
System.out.println(cls2 == cls3);//true
}
`
结论∶
同一个字节码文件(*.class)在一次程序运行过程中,只会被加载一次,不论通过哪一种方式获取的class对象都是同一个。
Class对象功能:
* 获取功能:
**1. 获取成员变量们**
* Field[] getFields() :获取所有public修饰的成员变量
* Field getField(String name) 获取指定名称的 public修饰的成员变量
* Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符
* Field getDeclaredField(String name)
* * 操作:
1. 设置值
* void set(Object obj, Object value)
2. 获取值
* get(Object obj)
3. 忽略访问权限修饰符的安全检查
* setAccessible(true):暴力反射
public static void main(String[] args) throws Exception {
//获取Person的Class对象
Class personClass = Person.class;
/**
* 1. 获取成员变量们
* * Field[] getFields() :获取所有public修饰的成员变量
* * Field getField(String name) 获取指定名称的 public修饰的成员变量
*
* * Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符
* * Field getDeclaredField(String name)
*/
//Field[] getFields() :获取所有public修饰的成员变量
Field[] fields = personClass.getFields();
for (Field field:fields){
System.out.println(field);
}
System.out.println("==================");
//Field getField(String name) 获取指定名称的 public修饰的成员变量
Field a = personClass.getField("a");
//创建一个实例对象 类似于 Person person= new Person();
Object person = personClass.newInstance();
//获得成员变量a的值
Object value = a.get(person);
System.out.println(value);
//设置a的值
a.set(person,"阿颖");
System.out.println(person);
System.out.println("==================");
//Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符
Field[] declaredFields = personClass.getDeclaredFields();
for(Field field:declaredFields){
System.out.println(field);
}
//Field getDeclaredField(String name)
Field name = personClass.getDeclaredField("name");
//忽略范围权限修饰符,暴力反射
name.setAccessible(true);
Object value2 = name.get(person);
System.out.println(value2);
name.set(person,"李四");
System.out.println(person);
}
**2. 获取构造方法们**
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
创建对象:
* T newInstance(Object... initargs)
* 如果使用空参数构造方法创建对象,操作可以简化:Class对象的newInstance方法
```public static void main(String[] args) throws Exception {
//获取Person的Class对象
Class personClass = Person.class;
/**
2. 获取构造方法们
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*/
//Constructor<T> getConstructor(类<?>... parameterTypes)
Constructor constructors = personClass.getConstructor(String.class,int.class);
System.out.println(constructors);
//创建对象
Object person = constructors.newInstance("张三", 25);
System.out.println(person);
System.out.println("===============");
Constructor constructors1 = personClass.getConstructor();
System.out.println(constructors);
//创建对象
Object person1 = constructors1.newInstance();
System.out.println(person1);
Object o = personClass.newInstance();
System.out.println(o);
}
**3. 获取成员方法们:**
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
执行方法:
* Object invoke(Object obj, Object... args)
```java
在这里插入代码片
```public static void main(String[] args) throws Exception {
//获取Person的Class对象
Class personClass = Person.class;
/**
* 3. 获取成员方法们:
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
*/
//获取指定方法名称的方法
Method eat_method = personClass.getMethod("eat");
Object person = personClass.newInstance();
//执行方法
eat_method.invoke(person);
System.out.println("===========");
Method eat_method2 = personClass.getMethod("eat", String.class);
//执行方法
eat_method2.invoke(person,"油泼面");
}
**4. 获取全类名**
* String getName()
String name = personClass.getName();
System.out.println(name);