1.什么是反射
反射是框架设计的灵魂,框架:它是一个半成品,可以拿来使用,添加上自己的业务代码。提高开发效率。
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
反射就是把类中成员抽取成其他类的过程。这就是反射。
2. 如何获取反射类对象--Class
(1)通过Class.forName获取反射对象
Class.forName("全路径")
--spring它就是使用的该模式<bean class="全路径">
(2)通过类名.class获取
类名.class;
---代理类--->SqlSession.getMapper(StudentDao.class)
(3) 通过对象.getClass()方法获取
对象.getClass();
---当知道对象时可以通过这种方式获取反射对象
实例如下
public class Test {
public static void main(String[] args) throws Exception{
// 通过Class.forName来获取反射类对象
Class aClass = Class.forName("demo04.People");
// 通过类名调用.class获取反射对象
Class peopleClass = People.class;
// 通过对象获取反射类对象
People p=new People();
Class aClass1 = p.getClass();
// 上面三个反射对象的引用地址是一致的,一个类只会被加载到内存中一次
System.out.println(aClass==peopleClass);
System.out.println(peopleClass==aClass1);
}
}
实体类
public class People extends Father{
private String name;
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show (){
System.out.println("1111111"+name);
}
}
class Father {
public String address;
private int age;
}
3. 通过反射类获取对应的类对象
class Test01{
public static void main(String[] args) throws Exception {
// 获取反射类对象
Class<People> peopleClass = People.class;
// 由反射类创建类对象--调用为无参构造函数
People people=peopleClass.newInstance();
People people01=peopleClass.newInstance();
// 地址不相同
System.out.println( people==people01);
}
}
4. 通过反射获取对应的Field属性对象。
class Test02{
public static void main(String[] args) throws Exception{
// 通过Class.forName来获取反射类对象
Class aClass = Class.forName("demo04.People");
//获取本类中指定的属性对象
Field name = aClass.getDeclaredField("name");
System.out.println(name);
//获取本类以及父类中指定的属性---必须为public修饰的。
Field address = aClass.getField("address");
System.out.println(address);
//获取本类中所有的属性对象
Field[] declaredFields = aClass.getDeclaredFields();
for(Field field:declaredFields){
System.out.println(field);
}
//获取本类以及父类中所有public修饰的属性对象
Field[] fields = aClass.getFields();
for(Field field:fields){
System.out.println(field);
}
}
}
Field属性对象中常见的方法。
getName():获取属性名
setAccessible(true):设置属性可访问。
set(o,v):为对象o的属性赋值v
get(o):获取对象o的属性值
class Test03{
public static void main(String[] args) throws Exception{
// 通过类名调用.class获取反射对象
Class<People> peopleClass = People.class;
// 获取属性的名称
Field name = peopleClass.getDeclaredField("name");
String name01= name.getName();
System.out.println(name01);
// 2.为属性赋值 Object 对象 object value
People people = peopleClass.newInstance();
System.out.println(people);
name.setAccessible(true);//为nameFiled 设置可访问权限,打破封装性
name.set(people,"张三");
System.out.println(people);
//获取属性值
Object o = name.get(people);
System.out.println(o);
}
}
5. 通过反射获取对应的Method方法对象
class Test04{
public static void main(String[] args) throws Exception{
Class aClass = Class.forName("demo04.People");
// 获取本类中所有的方法
Method[] declaredMethods = aClass.getDeclaredMethods();
for(Method method:declaredMethods){
System.out.println(method);
}
System.out.println("=============");
// 获取本类和父类中的所有public修饰的方法
Method[] methods = aClass.getMethods();
for(Method method:methods){
System.out.println(method);
}
// 获取本类中指定的方法对象
Method setName = aClass.getDeclaredMethod("setName", String.class);
System.out.println(setName);
//获取本类以及父类中指定名称的方法对象
Method equals = aClass.getMethod("equals", Object.class);
System.out.println(equals);
}
}
public class Test {
public static void main(String[] args) throws Exception{
Class<Student> aClass = Student.class;
//获取类对象的指定注解
MyAnnotation annotation = aClass.getAnnotation(MyAnnotation.class);
System.out.println(annotation.value());
Field idField = aClass.getDeclaredField("id");
MyAnnotation annotation1 = idField.getAnnotation(MyAnnotation.class);
System.out.println(annotation1.value());
System.out.println(annotation1.sex());
}
}
Method类中常见的方法
//method.invoke(对象,方法参数值); invoke(people,"15");//回调。动态代理