反射是框架设计的灵魂,框架:它是一个半成品,可以拿来使用,添加上自己的业务代码。提高开发效率。
反射就是把类中成员抽取成其他类的过程。这就是反射。
2.如何获取反射类对象---Class有三种:
(1)通过Class.forName获取反射对象
Class.forName("全路径")
--spring他就是使用的该模式
(2)通过类名.class获取
类名.class
---代理类---
SqlSession.getMapper(StudentDao.class)
(3)通过对象.getClass()方法获取
对象.getClass();
---当知道对象时可以通过这种方式获取反射对象
package demo02;
public class People {
private String name;
public int age;
@Override
public String toString() {
return "People{" +
"name='" + name + ''' +
", 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;
}
public void fun(){
System.out.println(name);
}
}
package demo02;
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException {
//1.通过Class.forName来获取反射类对象
Class> aClass = Class.forName("demo02.People");
//2.通过类名调用.class获取反射类对象
Class aClass1 = People.class;
//3.通过对象获取反射类对象
People people = new People();
Class extends People> aClass2 = people.getClass();
//思考:上面三个反射对象的引用地址是否一致! 是一致的。 一个类只会被加在到内存中一次。
System.out.println(aClass==aClass1);
System.out.println(aClass2==aClass1);
System.out.println(aClass==aClass2);
}
}
2.通过反射类获取对应的类对象
aclass.newInstance();
1.需要先获取反射类对象
2.由反射类创建类对象----->调用无参构造函数
package demo02;
public class Test02 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
//1.获取反射类对象
Class aClass = People.class;
//2.由反射类创建类对象--->调用为无参构造函数
People people = aClass.newInstance();
People people1 = aClass.newInstance();
//3.它们的地址是否相同
System.out.println(people==people1);
}
}
3.通过反射获取对应的Field属性对象
//获取本类中指定的属性对象 Field nameField = aClass.getDeclaredField("name");//获取本类以及父类中指定的属性---必须为public修饰的属性 Field field = aClass.getField("address");//获取本类中所有的属性对象 Field[] declaredFields = aClass.getDeclaredFields();//获取本类以及父类中所有public修饰的属性对象 Field[] fields = aClass.getFields();
package demo02;
import java.lang.reflect.Field;
public class Test03 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//获取反射类对象
Class> aClass = Class.forName("demo02.People");
//获取本类中指定的属性对象
Field nameField = aClass.getDeclaredField("name");
System.out.println(nameField);
//获取本类以及父类中指定的属性---必须为public修饰的属性
Field field = aClass.getField("address");
System.out.println(field);
//获取本类中所有的属性对象
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field1 : declaredFields) {
System.out.println(field1);
}
//获取本类以及父类中所有public修饰的属性对象
Field[] fields = aClass.getFields();
for (Field field1 : fields) {
System.out.println(field1);
}
}
}
3.1.Field属性对象中常见的方法
getName():获取属性名
setAccessible(true):设置属性可访问。
set(o,v):为对象o的属性赋值v
get(o):获取对象o的属性值
package demo02;
import java.lang.reflect.Field;
public class Test04 {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException {
//获取反射对象
Class aClass = People.class;
//获取本类中指定的属性对象
Field nameField = aClass.getDeclaredField("name");
//1.获取属性的名称
String name = nameField.getName();
System.out.println("属性名称:"+name);
//2.为属性赋值
//Object obj,对象
//Object value
People people = aClass.newInstance();
System.out.println(people);
//为nameField设置可访问权限,打破了封装性
nameField.setAccessible(true);
nameField.set(people,"张三");
System.out.println(people);
//3.获取属性值
Object o = nameField.get(people);
System.out.println(o);
//nameField.getAnnotation(); 获取nameField属性上的注解对象
}
}
4.通过反射获取对应的Method方法对象
//获取本类中所有的方法 Method[] declaredMethods = aClass.getDeclaredMethods();//获取本类以及父类中所有public修饰的方法 Father类继承了Object类所以有这么多方法 Method[] methods = aClass.getMethods();//本类中指定的方法对象 //无参 Method fun = aClass.getDeclaredMethod("fun"); //有参 Method setName = aClass.getDeclaredMethod("setName", String.class);//获取本类以及父类中指定名称的方法对象 Father类继承了Object类所以有equals方法 Method equals = aClass.getMethod("equals", Object.class);
package demo02;
import java.lang.reflect.Method;
public class Test05 {
public static void main(String[] args) throws NoSuchMethodException {
//获取反射对象
Class aClass = People.class;
//获取本类中所有的方法
Method[] declaredMethods = aClass.getDeclaredMethods();
for (Method method : declaredMethods){
System.out.println(method);
}
System.out.println("----------------------------------------------");
//获取本类以及父类中所有public修饰的方法 Father类继承了Object类所以有这么多方法
Method[] methods = aClass.getMethods();
for (Method method : methods) {
System.out.println(method);
}
System.out.println("----------------------------------------------");
//本类中指定的方法对象
//无参
Method fun = aClass.getDeclaredMethod("fun");
System.out.println(fun);
//有参
Method setName = aClass.getDeclaredMethod("setName", String.class);
System.out.println(setName);
System.out.println("----------------------------------------------");
//获取本类以及父类中指定名称的方法对象 Father类继承了Object类所以有equals方法
Method equals = aClass.getMethod("equals", Object.class);
System.out.println(equals);
}
}
4.1.Method类中常见的方法
//method.invoke(对象,方法参数值);
invoke(people,"15");//回调。动态代理
//本类中指定的方法对象
//无参
Method fun = aClass.getDeclaredMethod("fun");
System.out.println(fun);
//有参
Method setName = aClass.getDeclaredMethod("setName", String.class);
System.out.println(setName);
People p = aClass.newInstance();
Object o = fun.invoke(p);
Object invoke = setName.invoke(p, "李四");
System.out.println(p);
5.获取相应注解对象
//获取反射对象 ClassaClass = Student.class; //获取类对象的指定注解 MyAnnotation annotation = aClass.getAnnotation(MyAnnotation.class); //获取属性对象的指定注解 Field idField = aClass.getDeclaredField("id"); MyAnnotation annotation1 = idField.getAnnotation(MyAnnotation.class);//获取方法对象的指定注解 Method fun = aClass.getDeclaredMethod("fun",String.class); MyAnnotation annotation2 = fun.getAnnotation(MyAnnotation.class);
获取类对象的指定注解的值默认为null,默认时源码 java经历到字节码阶段,运行阶段注解会获取不到,所以需要在自定义注解上设置生效到运行阶段@Retention(RetentionPolicy.RUNTIME)
package demo02;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test06 {
public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
//获取反射对象
Class 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()+" "+annotation1.sex());
//获取方法对象的指定注解
Method fun = aClass.getDeclaredMethod("fun",String.class);
MyAnnotation annotation2 = fun.getAnnotation(MyAnnotation.class);
System.out.println(annotation2.value()+" "+annotation2.sex());
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
String value() default "";
String sex() default "男";
}
@MyAnnotation(value = "tab_student")
class Student{
@MyAnnotation("uid")
private Integer id;
@MyAnnotation("sex")
private String sex;
@MyAnnotation("fun")
public void fun(String sex){
System.out.println(sex);
}
}
手撕ORM
ORM:(Object relative Mapping)对象关系映射框架。
帮你自动把数据库中的记录和java实体类对应映射在一起。
暑期编程PK赛 得CSDN机械键盘等精美礼品!


