反射是框架设计的灵魂,框架:它是一个半成品,可以拿来使用,添加上自己的业务代码。提高开发效率。
反射就是把类中成员抽取成其他类的过程。这就是反射。
1.通过Class.forName获取反射对象.
Class.forName("全路径")
--spring它就是使用的该模式
//People是我定义的一个类
//1.通过Class.froName获取
Class aClass = Class.forName("com.gsh.getReflection.People");
2.通过类名.class获取
类名.class;
---代理类--->SqlSession.getMapper(StudentDao.class)
//2.通过类名调用.class来获取反射类对象
Class peopleClass = People.class;
3.通过对象.getClass()方法获取
对象.getClass();
---当知道对象时可以通过这种方式获取反射对象
//3.通过对象获取反射类
People people=new People();
Class aClass1 = people.getClass();
注: 上面三个反射对象的引用地址是否一致?
是一致的。 一个类只会被加在到内存中一次。
反射类.newInstance();
注:由反射类创建类对象需要调用无参构造
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
//获取反射类对象
Class peopleClass = People.class;
//2.由反射类创建类对象,调用无参构造
People people = peopleClass.newInstance();
People people1=peopleClass.newInstance();
//3.判断它们地址是否相同
System.out.println(people==people1);
}
反射类.getDeclaredField("属性名")
反射类.getField("属性名")
反射类.getDeclaredFields("属性名")
反射类.getFields("属性名")
public static void main(String[] args)throws Exception {
//获取反射类对象
Class peopleClass = People.class;
//1.获取本类以及父类中指定的公有属性public
Field name = peopleClass.getField("name");
System.out.println(name);
//2.获取本类中指定的的属性
Field declaredField = peopleClass.getDeclaredField("age");
System.out.println(declaredField);
//3.获取本类中的所有属性
Field[] declaredFields = peopleClass.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println(field);
}
System.out.println("*******************");
//获取本类及父类中所有的public修饰的属性对象
Field[] fields = peopleClass.getFields();
for (Field field : fields) {
System.out.println(field);
}
}
属性对象.getName():
属性对象.setAccessible(true);
属性对象.set(对象,值)
属性对象为你获取到的属性对象,对象为你要赋值属性所属的对象。
属性对象.get(对象)
这里属性对象为你获取到的属性对象,对象为你要获取属性值所属的对象。
//获取反射类对象
Class peopleClass = People.class;
//获取指定属性
Field fieldname = peopleClass.getDeclaredField("name");
//1.获取属性的名称
String name = fieldname.getName();
System.out.println(name);
//2.为属性赋值
People people = peopleClass.newInstance();
//为fieldname设置可访问权限,打破封装
fieldname.setAccessible(true);
//第一个参数是:对象
//第二个参数是:要赋的值
fieldname.set(people,"李四");
System.out.println(people);
//3.获取属性值
Object o = fieldname.get(people);
System.out.println(o);
反射类.getDeclaredMethods();
得到的是一个存放所有方法的数组
反射类..getMethods();
得到的是一个存放所有方法的数组
反射类.getDeclaredMethod("方法名",方法参数);
反射类.getMethod("方法名", 方法参数);
public static void main(String[] args)throws Exception {
//获取反射类
Class peopleClass = People.class;
//1.获取本类及父类中所有public修饰的方法
Method[] methods = peopleClass.getMethods();
for(Method method:methods)
System.out.println(method);
System.out.println("_---------------------------------");
//2.获取本类中所有的方法
Method[] declaredMethods = peopleClass.getDeclaredMethods();
for(Method method:declaredMethods)
{
System.out.println(method);
};
System.out.println("******************");
//3.获取本类中指定的方法对象
Method setName = peopleClass.getDeclaredMethod("setName", String.class);
System.out.println(setName);
System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
//4.获取本类及父类中指定名称的方法对象
Method method = peopleClass.getMethod("equals", Object.class);
System.out.println(method);
}
回调,动态代理中也使用了回调
method.invoke(对象,方法参数值);
反射类.getAnnotation(注解名.class)
通过反射类获取到的属性.getAnnotation(注解名.class)
获取的注解对象.value()