java
对象的属性、方法、构造方法等JDBC
加载驱动连接 Class.forname("com.mysql.jdbc.Driver")
;动态加载mysql
驱动;Spring
容器框架IOC
实例化对象Aop
) mybatis orm
Class类 代表类的实体,在运行的Java应用程序中表示类和接口
Field类 代表类的成员变量(成员变量也称为类的属性)
Method类 代表类的方法
Constructor类 代表类的构造方法
getField、getMethod和getCostructor
方法可以获得指定名字的域、方法和构造器。
getFields、getMethods和getCostructors
方法可以获得类提供的public域、方法和构造器数组,其中包括超类的共有成员。
getDeclatedFields、getDeclatedMethods
和getDeclaredConstructors
方法可以获得类中声明的全部域、方法和构造器,其中包括私有和受保护的成员,但不包括超类的成员。
//方式一
Class userClass = Class.forName("com.example.test.反射.User");
System.out.println(userClass);
System.out.println("*************");
//方式二
Class userClass2 = User.class;
System.out.println(userClass);
System.out.println("*************");
//方式三
User user = new User();
Class userClass3 = user.getClass();
System.out.println(userClass3);
System.out.println("***************");
//调用无参构造函数
User user1 = (User) userClass2.newInstance();
//调用有参构造函数
Constructor<?> declaredConstructor =userClass2.getDeclaredConstructor(String.class,Integer.class,String.class);
User user2 = (User)declaredConstructor.newInstance("张三",28,"张三丰");
System.out.println(user2.toString());
System.out.println(user1==user2);
//公有属性查找、赋值
User user4 = (User)userClass.newInstance();
Field publicName = userClass.getDeclaredField("publicName");
publicName.set(user4,"晓果冻");
System.out.println(user4.toString());
//私有属性查找、赋值
User user5 = (User)userClass.newInstance();
Field name = userClass.getDeclaredField("name");
//反射访问私有属性
name.setAccessible(true);
name.set(user5,"晓果冻5");
System.out.println(user5.toString());
System.out.println(user4==user5);
//获取所有方法
User user6 = (User)userClass.newInstance();
//获取所有方法
Method[] methods = userClass.getMethods();
for(Method method:methods){
System.out.println(method);
}
//只能获取公共的方法
Method getName = userClass.getMethod("getName");
Method sumMethod = userClass.getDeclaredMethod("sum",Integer.class,Integer.class);
sumMethod.setAccessible(true);
Object total = sumMethod.invoke(user6,1,2);
System.out.println(total);
//反射越过泛型检查 只在编译器检查
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("晓果冻");
Class<? extends ArrayList> aClass = arrayList.getClass();
Method addMethod = aClass.getDeclaredMethod("add", Object.class);
addMethod.invoke(arrayList,1);
System.out.println(arrayList);
全部代码
User.java
/**
* @author 晓果冻
* @version 1.0
* @date 2022/5/24 14:19
*/
public class User {
private String name;
private Integer age;
public String publicName;
public User(String name, Integer age, String publicName) {
System.out.println("有参构造函数");
this.name = name;
this.age = age;
this.publicName = publicName;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPublicName() {
return publicName;
}
public void setPublicName(String publicName) {
this.publicName = publicName;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", publicName='" + publicName + '\'' +
'}';
}
private Integer sum(Integer a,Integer b){
return a+b;
}
}
Main.java
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* @author 晓果冻
* @version 1.0
* @date 2022/5/24 14:20
*/
public class Main {
public static void main(String[] args) throws Exception {
//方式一
Class userClass = Class.forName("com.example.test.反射.User");
System.out.println(userClass);
System.out.println("*************");
//方式二
Class userClass2 = User.class;
System.out.println(userClass);
System.out.println("*************");
//方式三
User user = new User();
Class userClass3 = user.getClass();
System.out.println(userClass3);
System.out.println("***************");
//调用无参构造函数
User user1 = (User) userClass2.newInstance();
//调用有参构造函数
Constructor<?> declaredConstructor =userClass2.getDeclaredConstructor(String.class,Integer.class,String.class);
User user2 = (User)declaredConstructor.newInstance("张三",28,"张三丰");
System.out.println(user2.toString());
System.out.println(user1==user2);
//公有属性查找、赋值
User user4 = (User)userClass.newInstance();
Field publicName = userClass.getDeclaredField("publicName");
publicName.set(user4,"晓果冻");
System.out.println(user4.toString());
//私有属性查找、赋值
User user5 = (User)userClass.newInstance();
Field name = userClass.getDeclaredField("name");
//反射访问私有属性
name.setAccessible(true);
name.set(user5,"晓果冻5");
System.out.println(user5.toString());
System.out.println(user4==user5);
//获取所有方法
User user6 = (User)userClass.newInstance();
//获取所有方法
Method[] methods = userClass.getMethods();
for(Method method:methods){
System.out.println(method);
}
//只能获取公共的方法
Method getName = userClass.getMethod("getName");
Method sumMethod = userClass.getDeclaredMethod("sum",Integer.class,Integer.class);
sumMethod.setAccessible(true);
Object total = sumMethod.invoke(user6,1,2);
System.out.println(total);
//反射越过泛型检查 只在编译器检查
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("晓果冻");
Class<? extends ArrayList> aClass = arrayList.getClass();
Method addMethod = aClass.getDeclaredMethod("add", Object.class);
addMethod.invoke(arrayList,1);
System.out.println(arrayList);
}
}
代码仓库地址:https://gitee.com/cgd0526/demo/tree/master/src/main/java/com/example/test/%E5%8F%8D%E5%B0%84