共计 5052 个字符,预计需要花费 13 分钟才能阅读完成。
Java 在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为 java 语言的反射机制。
[TOC]
反射机制的相关类
与 Java 反射相关的类如下:
类名 | 用途 |
Class 类 | 代表类的实体,在运行的 Java 应用程序中表示类和接口 |
Field 类 | 代表类的成员变量(成员变量也称为类的属性) |
Method 类 | 代表类的方法 |
Constructor 类 | 代表类的构造方法 |
Class 类
Class 代表类的实体,在运行的 Java 应用程序中表示类和接口。在这个类中提供了很多有用的方法,这里对他们简单的分类介绍。
获得类相关的方法
方法 | 用途 | |
asSubclass(Class<U> clazz) | 把传递的类的对象转换成代表其子类的对象 | |
Cast | 把对象转换成代表类或是接口的对象 | |
getClassLoader() | 获得类的加载器 | |
getClasses() | 返回一个数组,数组中包含该类中所有公共类和接口类的对象 | |
getDeclaredClasses() | 返回一个数组,数组中包含该类中所有类和接口类的对象 | |
forName(String className) | 根据类名返回类的对象 | |
getName() | 获得类的完整路径名字 | |
newInstance() | 创建类的实例 | |
getPackage() | 获得类的包 | |
getSimpleName() | 获得类的名字 | |
getSuperclass() | 获得当前类继承的父类的名字 | |
getInterfaces() | 获得当前类实现的类或是接口 |
获得类中属性相关的方法
方法 | 用途 |
getField(String name) | 获得某个公有的属性对象 |
getFields() | 获得所有公有的属性对象 |
getDeclaredField(String name) | 获得某个属性对象 |
getDeclaredFields() | 获得所有属性对象 |
获得类中注解相关的方法
方法 | 用途 |
getAnnotation(Class annotationClass) | 返回该类中与参数类型匹配的公有注解对象 |
getAnnotations() | 返回该类所有的公有注解对象 |
getDeclaredAnnotation(Class annotationClass) | 返回该类中与参数类型匹配的所有注解对象 |
getDeclaredAnnotations() | 返回该类所有的注解对象 |
获得类中构造器相关的方法
方法 | 用途 |
getConstructor(Class…<?> parameterTypes) | 获得该类中与参数类型匹配的公有构造方法 |
getConstructors() | 获得该类的所有公有构造方法 |
getDeclaredConstructor(Class…<?> parameterTypes) | 获得该类中与参数类型匹配的构造方法 |
getDeclaredConstructors() | 获得该类所有构造方法 |
获得类中方法相关的方法
方法 | 用途 |
getMethod(String name, Class…<?> parameterTypes) | 获得该类某个公有的方法 |
getMethods() | 获得该类所有公有的方法 |
getDeclaredMethod(String name, Class…<?> parameterTypes) | 获得该类某个方法 |
getDeclaredMethods() | 获得该类所有方法 |
类中其他重要的方法
方法 | 用途 |
isAnnotation() | 如果是注解类型则返回 true |
isAnnotationPresent(Class<? extends Annotation> annotationClass) | 如果是指定类型注解类型则返回 true |
isAnonymousClass() | 如果是匿名类则返回 true |
isArray() | 如果是一个数组类则返回 true |
isEnum() | 如果是枚举类则返回 true |
isInstance(Object obj) | 如果 obj 是该类的实例则返回 true |
isInterface() | 如果是接口类则返回 true |
isLocalClass() | 如果是局部类则返回 true |
isMemberClass() | 如果是内部类则返回 true |
Field 类
Field 代表类的成员变量(成员变量也称为类的属性)。
方法 | 用途 | |
equals(Object obj) | 属性与 obj 相等则返回 true | |
get(Object obj) | 获得 obj 中对应的属性值 | |
set(Object obj, Object value) 设置 obj 中对应属性值 |
Method 类
Method 代表类的方法。
方法 | 用途 | |
invoke(Object obj, Object… args) | 传递 object 对象及参数调用该对象对应的方法 |
Constructor 类
Constructor 代表类的构造方法。
方法 | 用途 |
newInstance(Object… initargs) | 根据传递的参数创建类的对象 |
示例:
没反射之前创建
Person p = new Person(); | |
p.setAge("1"); | |
p.setName("zs"); | |
p.setSex("0"); | |
p.show(); | |
p.display("china"); |
使用反射 获取运行时类的信息
- 1 通过反射创建对象
//Class<T> 这里可以使用泛型 | |
Class clazz = Person.class; | |
Person person = (Person) clazz.newInstance(); | |
person.setSex("1"); | |
; | |
person.setName("zs2"); | |
person.setAge("2"); | |
person.show(); | |
person.display("Keroar"); |
- 2 通过反射设置对象属性的指定属性
使用反射动态的设置
Person person2 = (Person) clazz.newInstance(); | |
// 这种这只属性只能是 public 修饰的 | |
Field field = clazz.getField("name"); | |
field.set(person2, "我是名字"); | |
System.out.println(person2); | |
//private 或者 其他的需要设置访问权限 | |
Field field1 = clazz.getDeclaredField("sex"); | |
field1.setAccessible(true); | |
field1.set(person2, "我是 sex"); | |
System.out.println(person2); |
-
3 通过反射获取指定类的指定方法属性
获取方法 根据方法名 以及 parameterTypes 可变参数类型,没有参数就不写了
public Method getMethod(String name, Class<?>… parameterTypes)
1 获取设置除 public 修饰的 | |
// 调用传参方法 display | |
Method method = clazz.getMethod("show"); | |
method.invoke(person2); | |
//2 同样 设置除 public 之外的访问 | |
//public Method getDeclaredMethod(String name, Class<?>... parameterTypes) | |
// method.setAccessible(true); | |
// 获取方法上注解以及方法参数类型 返回类型 方法名 | |
System.out.println("方法名:" + method.getName()); | |
System.out.println("参数类型:" + method.getTypeParameters()); | |
System.out.println("返回类型:" + method.getReturnType()); | |
//System.out.println("返回所有注解:" + method.getAnnotations()); | |
//System.out.println("返回所有参数注解:" + method.getParameterAnnotations()); | |
System.out.println("返回方法参数注解:" + method.getAnnotation(clazz)); | |
// 调用方法 | |
//public Object invoke(Object obj, Object... args) obj 调用的对象,args 传入方法可变参数 | |
// 调用传参方法 display | |
Method methods = clazz.getMethod("display", String.class); | |
methods.invoke(person2, "我是传入的国籍参数"); |
Class<T> 反射的源头
-
每一个运行类只加载一次
有了 Class 实例才能进行1 创建对象 2 获取类中所有的属性 方法 父类 内部类 构造 异常 等等... 3 调用属性和方法等 4 反射的应用 : 动态代理
通过调用 getClass , 获取 Person 类运行时类的 calss 对象
Class pClass = p.getClass(); | |
System.out.println(pClass); |
创建 Class 四种方法
//1 通过运行类对象调用 getClass 方法 | |
//2 调用本身.class 属性 | |
Class clazz1 = Person.class; | |
Class clazz2 = String.class; | |
// 3 通过 Class 静态方法获取 | |
Class<?> aClass = Class.forName("reflection.Person"); | |
System.out.println(aClass); | |
// 4 类的加载器 | |
ClassLoader classLoader = this.getClass().getClassLoader(); | |
Class<?> aClass1 = classLoader.loadClass("reflection.Person"); | |
System.out.println(aClass1); |
类加载器作用
除了加载类之外类加载器可以用来干嘛?
加载属性文件
//1 | |
ClassLoader classLoader = aClass.getClassLoader(); | |
// 加载类 | |
// classLoader.loadClass(); | |
// 例如读取 jdbc 配置文件 | |
String name = "jdbc.properties"; | |
InputStream resourceAsStream = classLoader.getResourceAsStream(name); | |
Properties properties = new Properties(); | |
properties.load(resourceAsStream); | |
String property = properties.getProperty("key"); |
package reflection; | |
public class Person { | |
public String name; | |
private String age; | |
private String sex; // 0 男 1 女 | |
public Person(String name, String age, String sex) { | |
this.name = name; | |
this.age = age; | |
this.sex = sex; | |
} | |
public Person(String name) {this.name = name;} | |
public Person(String name, String age) { | |
this.name = name; | |
this.age = age; | |
} | |
public Person(){} | |
public String getName() {return name;} | |
public void setName(String name) {this.name = name;} | |
public String getAge() {return age;} | |
public void setAge(String age) {this.age = age;} | |
public String getSex() {return sex;} | |
public void setSex(String sex) {this.sex = sex;} | |
public void show(){System.out.println("emmmmmmm"); | |
} | |
public void display(String contry){System.out.println("我的国籍是" + contry); | |
} | |
@Override | |
public String toString() { | |
return "Person{" + | |
"name='" + name + '\'' + | |
", age='" + age + '\'' + | |
", sex='" + sex + '\'' + | |
'}'; | |
} | |
} |
正文完
发表至: java
2019-09-09