Java反射
1.反射(Reflection)
反射就是:加载类,并允许以编程的方式解剖类中的各种成分(成员变量、方法、构造器等)
反射的具体内容:获取类的信息,它们
1.反射第一步:加载类,获取类的字节码:Class对象
2.获取类的构造器:Constructor对象
3.获取类的成员变量:Field对象
4.获取类的成员方法:Method对象
1.反射第一步:加载类,获取类的字节码:Class对象
获取Class对象的三种方式
Class c1 = 类名.class | |
---|---|
调用Class提供方法:public static Class forName(String package) | 静态方法 |
Object提供的方法:public Class getClass();Class c3 = 对象.getClass() |
package fanshe;
public class StudentTest {
public static void main(String[] args) throws ClassNotFoundException {
Class c1 = Student.class;
System.out.println(c1.getName());// 全类名:fanshe.Student
System.out.println(c1.getSimpleName());// 类名:Student
Class c2 = Class.forName("fanshe.Student");
System.out.println(c1==c2);// true
Student s = new Student();
Class c3 = s.getClass();
System.out.println(c1==c3);// true
}
}
2.获取类的构造器:Constructor对象
方法 | 说明 |
---|---|
Constructor<?>[] getConstructors() | 获取全部构造器(只能获取public修饰的) |
Constructor<?>[] getDeclareConstructors() | 获取全部构造器(只要存在就能拿到) |
Constructor<?>[] getConstructor(Class<?>…parameterTypes) | 获取某个构造器(只能获取public修饰的) |
Constructor<?>[] getDeclareConstructor(Class<?>…parameterTypes) | 获取某个构造器(只有存在就能拿到) |
package fanshe;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class StudentTest {
@Test
public void testGetConstructors(){
//反射第一步,获取Student类对象
Class c = Student.class;
//获取构造方法
Constructor[] constructors = c.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor.getName()+
"--->"+constructor.getParameterCount());
}
}
}
package fanshe;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(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;
}
}
获取全部构造器(只能获取public修饰的)
package fanshe;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class StudentTest {
@Test
public void testGetConstructors(){
//反射第一步,获取Student类对象
Class c = Student.class;
//获取构造方法
Constructor[] constructors = c.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor.getName()+
"--->"+constructor.getParameterCount());
}
System.out.println("-----------------------------------");
Constructor[] constructors1 = c.getDeclaredConstructors();
for (Constructor constructor : constructors1) {
System.out.println(constructor.getName()+
"--->"+constructor.getParameterCount());
}
}
}
package fanshe;
public class Student {
private String name;
private int age;
public Student() {
}
private Student(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;
}
}
获取全部构造器(只要存在就能拿到)
package fanshe;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class StudentTest {
@Test
public void testGetConstructors() throws NoSuchMethodException {
//反射第一步,获取Student类对象
Class c = Student.class;
//获取无参数构造方法
Constructor constructor = c.getConstructor();
System.out.println(constructor.getName() +
"--->" + constructor.getParameterCount());
//获取有参数构造方法
Constructor constructor2 =
c.getDeclaredConstructor(String.class, int.class);
System.out.println(constructor2.getName()+
"--->"+constructor2.getParameterCount());
}
}
package fanshe;
public class Student {
private String name;
private int age;
public Student() {
}
private Student(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;
}
}
3.获取类的成员变量和方法
方法 | 说明 |
---|---|
public Field[] getFields() | 获取类的全部成员变量(只能获取bupilc修饰的) |
public field[] getDeclaredFields() | 获取类的全部成员变量(只存在就能拿到) |
public Field getField(String name) | 获取类的某个成员变量(只能获取public修饰的) |
public Field getDeclaredField(String name) | 获取类的某个成员变量(只要存在就能拿到) |
获得成员变量的作业:依然是赋值、取值
方法 | 说明 |
---|---|
void set(Object objec,Object value) | 赋值 |
Object get(Object object) | 取值 |
public void setAccessible(Boolean flag) | 设置为ture,表示禁止检查访问控制(暴力反射) |
package fanshe;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class StudentTest {
@Test
public void testGetConstructors() throws NoSuchMethodException, NoSuchFieldException {
//反射第一步,获取Student类对象
Class student = Student.class;
//获取所有public修饰的属性
Field[] fields1 = student.getFields();
// 获取所有属性
Field [] fields = student.getDeclaredFields();
for (Field field:fields1){
System.out.println(field.getName());
}
System.out.println("==========================");
for (Field field:fields){
System.out.println(field.getName());
}
System.out.println("==========================");
Field field = student.getField("ClassId");
System.out.println(field.getName());
Field field1 = student.getDeclaredField("name");
System.out.println(field1.getName());
Field field2 = student.getDeclaredField("age");
System.out.println(field2.getName());
}
}
package fanshe;
public class Student {
public String ClassId;
private String name;
private int age;
public Student() {
}
private Student(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;
}
}
package fanshe;
import org.junit.Test;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
public class StudentTest {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
Class studentClass = Student.class;
Field field = studentClass.getDeclaredField("name");
Student student = new Student();
field.setAccessible(true);
field.set(student,"张三");
System.out.println(student.getName());
//Object get(Object object)
String name = (String)field.get(student);
System.out.println(name)
}
}
package fanshe;
public class Student {
public String ClassId;
private String name;
private int age;
public Student() {
}
private Student(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;
}
}
4.Class提供了从类中获取成员方法的ApI
方法 | 说明 |
---|---|
Method[] getMethod() | 获取类的全部成员方法(只能获取public修饰的) |
Method[] getDeclaredMethods() | 获取类的全部成员方法(只要存在就能拿到) |
Method getMethod(String name,class<?>…parameterType) | 获取类的某个成员方法(只能获取public修饰的) |
Method getDeclaredMethod(String name,Class<?>…parameterType) | 获取类的某个成员方法(只能存在就能拿到) |
成员方法的作用:依然是执行
Method提供的方法 | 说明 |
---|---|
public Object invoke(Object obj,Object…args) | 触发某个对象的该方法执行 |
public void setAccessible(boolean flag) | 设置为ture,表示禁止检查访问控制 |
package fanshe;
import org.junit.Test;
import java.lang.reflect.*;
public class StudentTest {
public static void main(String[] args) throws IllegalAccessException,
NoSuchMethodException, InvocationTargetException {
Class studentClass = Student.class;
Student student = new Student();
Method method = studentClass.getDeclaredMethod("love", String.class);
method.setAccessible(true);
Object invoke =method.invoke(student, "某某某");
System.out.println(invoke);
}
}
package fanshe;
public class Student {
public String ClassId;
private String name;
private int age;
public Student() {
}
private Student(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;
}
private String love(String name){
return "我喜欢的同学"+name;
}
}
5.作用、应用场景
反射的作用?
基本作用:可以得到一个类的全部成分然后操作
可以破环封装性
e Student(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;
}
private String love(String name){
return “我喜欢的同学”+name;
}
}
#### 5.作用、应用场景
反射的作用?
基本作用:可以得到一个类的全部成分然后操作
可以破环封装性
**最重要的用途是:适合做java的框架,基本上,主流的框架都会基于反射设计出一些通用的功能。**