反射
反射:加载类,并允许以编程的方式解剖类中的各种成分(成员变量,方法,构造器等)
反射学什么?
学习获取类的信息、操作他们
1.反射第一步:加载类、获取类的字节码:Class对象
package com.itheima.reflect;
public class reflect1 {
public static void main(String[] args) throws ClassNotFoundException {
Class studentClass = Student.class;
System.out.println(studentClass.getName());//全名
System.out.println(studentClass.getSimpleName());//简名
Class aClass = Class.forName("com.itheima.reflect.Student");
System.out.println(aClass == studentClass);
Student student = new Student();
Class aClass1 = student.getClass();
System.out.println(aClass1);
}
}
2.获取类的构造器:Constructor对象
package com.itheima.reflect;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class reflect2 {
@Test
public void StudentReflect() throws Exception {
//1.得到类的Class对象
Class studentClass = Student.class;
//2.获取private的,获取类的构造器,无参构造器
Constructor[] declaredConstructors = studentClass.getDeclaredConstructors();
for (Constructor declaredConstructor : declaredConstructors) {
System.out.println(declaredConstructor.getName() + declaredConstructor.getParameterCount());
}
//3.获取public的
Constructor[] constructors = studentClass.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor.getName());
}
}
@Test
public void StudentGetController() throws Exception {
//1.获取类Class对象
Class studentClass = Student.class;
//2.获取public类型的
Constructor constructor = studentClass.getConstructor(
String.class, int.class
);
System.out.println(constructor.getName());
//3.获取private类型的
Constructor declaredConstructor = studentClass.getDeclaredConstructor(String.class, int.class);
System.out.println(declaredConstructor.getName());
}
}
3.获取类的成员变量:Field对象
//第一个方法的设置就是让private对象可以被公开创建
//禁止检查访问权限
constructor.setAccessible(true);
//强转为Student对象
Student student = (Student)constructor.newInstance();
//
declaredConstructor.setAccessible(true);
Student student1 = (Student)declaredConstructor.newInstance("张三", 20);
获取成员变量的前提就是要有对象
public class reflect3 {
@Test
public void getAttribute() throws Exception {
//创建对象
Class studentClass = Student.class;
//将属性存放在一个数组内
Field[] declaredFields = studentClass.getDeclaredFields();
//遍历数组获取属性
for (Field declaredField : declaredFields) {
System.out.println(declaredField.getName());
}
Class studentClass1 = Student.class;
Field name = studentClass1.getDeclaredField("name");
System.out.println(name);
}
}
Student student = new Student();
name.setAccessible(true);
name.set(name,"111");
String name1 = (String)name.get(student);
System.out.println(name1);
4.获取类的成员方法:Method对象
package com.itheima.reflect;
import org.junit.Test;
import java.lang.reflect.Method;
public class reflect4 {
@Test
public void getMethod() throws Exception {
//1.创建对象
Class studentClass = Student.class;
//2.获取对象全部方法
Method[] declaredMethods = studentClass.getDeclaredMethods();
//3.遍历方法获取对象方法参数:方法名,方法数,方法返回值类型
for (Method declaredMethod : declaredMethods) {
System.out.println(declaredMethod.getName()
+"--->"+declaredMethod.getParameterCount()
+"--->"+declaredMethod.getReturnType()
);
}
//4.根据参数获取对象方法
Method getName = studentClass.getDeclaredMethod("getName");
System.out.println(getName.getName()
+"--->"+getName.getParameterCount()
+"--->"+getName.getReturnType()
);
Student student = new Student();
getName.setAccessible(true);
Object rs = getName.invoke(Student);
System.out.println(rs);
}
}
-
构造对象
-
使用对象get方法然后获取想要的方法,属性,构造器等
-
遍历方法获取对象方法,属性,构造器等
-
get…s就是获取所有的东西,没有s就要加参数,获取特定的想要的东西,然后是否加Declared就是表明是否要强制获取private的东西
反射的作用
-
基本作用:可以得到一个类的全部成分然后操作
-
可以破坏封装性
-
最重要的用途:适合做Java框架,基本上,主流的框架都会基于反射设计出一些通用的功能
public class Test5 {
@Test
public void Test5Frame() throws Exception {
Student student = new Student("李宇航",11,'1',111,"1111");
Teacher teacher = new Teacher("111", 11111);
ObjectFrame.saveObject(student);
ObjectFrame.saveObject(teacher);
}
}
public class ObjectFrame {
//目标,保存任意信息到文本文件中去
public static void saveObject(Object obj) throws Exception {
//创建管道,输出到文件中去
PrintStream printStream = new PrintStream(new FileOutputStream("D:\\javacangku\\云顶视频练习\\Junit1\\src\\main\\java\\com\\itheima\\reflect1\\a.txt",true));
//1.Object是任意对象,到底有多少个字段保存
Class objClass = obj.getClass();
Field[] fields = objClass.getDeclaredFields();
//2.从这个类中提取它的全部成员变量
for(Field field : fields){
String name = field.getName();
field.setAccessible(true);
String value = field.get(obj) + "";
printStream.println(name + " = "+ value);
}
printStream.close();
}
}
标签:反射,java,getName,public,学习,Student,println,studentClass,class
From: https://blog.csdn.net/2301_80005856/article/details/140286676