import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//动态的创建对象,通过反射
public class Test07 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获得Class对象
Class c1 = Class.forName("com.shisan.reflection.User");
//构造一个对象
User user = (User)c1.newInstance(); //本质上时调用了类的无参构造器
System.out.println(user);
//通过构造器创建对象
Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class
java通过反射,动态的创建对象以及分析性能
最新推荐文章于 2024-07-18 08:00:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)