反射是框架设计的灵魂。
框架:半成品软件,可以在框架的基础上进行软件开发,简化代码
反射:将类的各个组成部分封装为其他的对象,这就是反射的机制
好处:
1.可以在程序运行过程中,操作这些对象。
2.可以解耦,提高程序的可扩展性。
获取class对象的方式:
1.Class.forName("全类名"):将字节码文件加载进内存,返回class对象
(多用于配置文件,将类名定义在配置文件中。读取文件,加载类)
2.类名.class:通过类名的属性class获取
(多用于参数的传递)
3.对象.getClass:getClass()方法在Object类中定义着
(多用于对象的获取字节码的方式)
定义一个Person类:
package cn.itcast.junit.domain;
public class Person {
private String name;
private int age;
public int num;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", num=" + num +
'}';
}
public void eat(){
System.out.println("eat==============");
}
public void eat(String food){
System.out.println("eat=============="+food);
}
}
获取class对象的方法:
package cn.itcast.junit.reflect;
import cn.itcast.junit.domain.Person;
public class ReflectDemo1 {
/**
* 获取class对象的方式:
*
* 1.Class.forName("全类名"):将字节码文件加载进内存,返回class对象
*
* 2.类名.class:通过类名的属性class获取
*
* 3.对象.getClass:getClass()方法在Object类中定义着
*/
public static void main(String[] args) throws Exception {
// 1.Class.forName("全类名")
Class cls1 = Class.forName("cn.itcast.junit.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)在一次程序运行过程中,只会被加载一次,不论通过哪一种方式获取的Class对象都是同一个
Class对象功能:
获取功能:
1.获取成员变量们
Field[] getFields() 获取所有public修饰的成员变量
Field getField(String name) 获取指定名称的 public修饰的成员变量
Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符
Field getDeclaredField(String name)
package cn.itcast.junit.reflect;
import cn.itcast.junit.domain.Person;
import java.lang.reflect.Field;
public class Reflectdemo2 {
/**
* 1.获取成员变量们
*
* Field[] getFields() 获取所有public修饰的成员变量
* Field getField(String name) 获取指定名称的 public修饰的成员变量
* Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符
* Field getDeclaredField(String name)
*
* 2.获取构造方法们
*
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*
* 3.获取成员方法
*
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
*
* 4.获取类名
*
* String getName()
*/
public static void main(String[] args) throws Exception {
//0.获取Person的Class对象
Class personClass = Person.class;
/**
1.获取成员变量们
*
* Field[] getFields() 获取所有public修饰的成员变量
* Field getField(String name) 获取指定名称的 public修饰的成员变量
* 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) 获取指定名称的 public修饰的成员变量
Field num = personClass.getField("num");
//获取成员变量num的值
Person p = new Person();
Object value = num.get(p);
System.out.println(value);
//设置num的值
num.set(p,666);
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 age = personClass.getDeclaredField("age");
//忽略访问权限修饰符的安全检查
age.setAccessible(true);//暴力反射
Object value2 = age.get(p);
System.out.println(value2);
}
}
2.获取构造方法们
Constructor<?>[] getConstructors()
Constructor<T> getConstructor(类<?>... parameterTypes)
Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
Constructor<?>[] getDeclaredConstructors()
package cn.itcast.junit.reflect;
import cn.itcast.junit.domain.Person;
import java.lang.reflect.Constructor;
public class Reflectdemo3{
/**
* 1.获取成员变量们
*
* Field[] getFields() 获取所有public修饰的成员变量
* Field getField(String name) 获取指定名称的 public修饰的成员变量
* Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符
* Field getDeclaredField(String name)
*
* 2.获取构造方法们
*
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*
* 3.获取成员方法
*
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
*
* 4.获取类名
*
* String getName()
*/
public static void main(String[] args) throws Exception {
//0.获取Person的Class对象
Class personClass = Person.class;
/**
2.获取构造方法们
*
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*
*/
Constructor constructor = personClass.getConstructor(String.class, int.class);
System.out.println(constructor);
//创建对象
Object person = constructor.newInstance("张三", 66);
System.out.println(person);
}
}
3.获取成员方法
Method[] getMethods()
Method getMethod(String name, 类<?>... parameterTypes)
Method[] getDeclaredMethods()
Method getDeclaredMethod(String name, 类<?>... parameterTypes)
4.获取类名
String getName()
package cn.itcast.junit.reflect;
import cn.itcast.junit.domain.Person;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Reflectdemo4 {
/**
* 1.获取成员变量们
*
* Field[] getFields() 获取所有public修饰的成员变量
* Field getField(String name) 获取指定名称的 public修饰的成员变量
* Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符
* Field getDeclaredField(String name)
*
* 2.获取构造方法们
*
* Constructor<?>[] getConstructors()
* Constructor<T> getConstructor(类<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
* Constructor<?>[] getDeclaredConstructors()
*
* 3.获取成员方法
*
* Method[] getMethods()
* Method getMethod(String name, 类<?>... parameterTypes)
* Method[] getDeclaredMethods()
* Method getDeclaredMethod(String name, 类<?>... parameterTypes)
*
* 4.获取类名
*
* String getName()
*/
public static void main(String[] args) throws Exception {
//0.获取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");
Person p = new Person();
//执行方法
eat_method.invoke(p);
Method eat_method2 = personClass.getMethod("eat", String.class);
//执行方法
eat_method2.invoke(p,"水");
//获取所有public修饰的方法
Method[] methods = personClass.getMethods();
for(Method method:methods){
System.out.println(method);
String name = method.getName();//获取名称
System.out.println(name);
}
//获取类名
String classname = personClass.getName();
System.out.println(classname);
}
}
Field:成员变量
操作:
设置值
void set(Object obj,Object value)
获取值
get(Object obj)
忽略访问权限修饰符的安全检查
setAccessible(true):暴力反射
Constructor:构造方法
创建对象:
T newInstance(object...initargs)
如果使用空参数构造方法创建对象,操作可以简化:Class对象的newInstance方法
Method:方法对象
执行方法:
Object invoke(Object obj,Object....args)
获取方法名称:
String getName:获取方法名