反射概述
反射的简介
- Reflection(反射)是被视为动态语言的关键,反射机制允许程序在执行期借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法。
- 加载完类之后,在堆内存的方法区中就产生了一个
Class
类型的对象(一个类只有一个Class
对象),这个对象就包含了完整的类的结构信息。我们可以通过这个对象看到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以,我们形象的称之为:反射。
框架 = 注解 + 反射 + 设计模式
反射机制提供的功能
- 在运行时判断任意一个对象所属的类
- 在运行时构造任意一个类的对象
- 在运行时判断任意一个类所具有的成员变量和方法
- 在运行时获取泛型信息
- 在运行时调用任意一个对象的成员变量和方法
- 在运行时处理注解
- 生成动态代理
相关API
-
java.lang.Class
:代表一个类 -
java.lang.reflect.Method
:代表类的方法 -
java.lang.reflect.Field
:代表类的成员变量 -
java.lang.reflect.Constructor
:代表类的构造器
反射的使用
代码示例:
Person
类:
public class Person {
private String name;
// public权限
public int age;
public Person() {
System.out.println("Person()");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 私有构造器
private Person(String name) {
this.name = name;
}
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 void show() {
System.out.println("大家好,我是冯宝宝");
}
// 私有方法
private String showNation(String nation) {
System.out.println("我的国籍是:" + nation);
return nation;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
未使用反射,对于Person
的操作:
public class ReflectionTest {
// 反射之前,对于Person的操作
@Test
public void test1() {
// 1.创建Person类的对象
Person p1 = new Person("王也", 18);
// 2.通过对象,调用其内部的属性、方法
p1.age = 20;
// 在Person类外部,不可以通过Person类的对象调用其内部私有结构,比如下面两个:
// p1.name = "张灵玉"; // error
// p1.showNation("China"); // error
System.out.println(p1.toString());
p1.show();
}
}
使用反射,对于Person
的操作:
@Test
public void test2() throws Exception {
Class clazz = Person.class;
// 1.通过反射创建类的对象
Constructor cons = clazz.getConstructor(String.class, int.class);
Object obj = cons.newInstance("王也", 18);
Person p = (Person) obj;
System.out.println(p.toString());
// 2.通过反射,调用对象指定的属性、方法
// 调用属性
Field age = clazz.getDeclaredField("age");
age.set(p, 10);
System.out.println(p.toString());
// 调用方法
Method show = clazz.getDeclaredMethod("show");
show.invoke(p);
System.out.println("----------我是一条分割线---------");
// 通过反射,可以调用Person类的私有结构,比如:私有的构造器、方法、属性
// 调用私有的构造器
Constructor cons1 = clazz.getDeclaredConstructor(String.class);
cons1.setAccessible(true);
Person p1 = (Person) cons1.newInstance("Jerry");
System.out.println(p1);
// 调用私有的属性
Field name = clazz.getDeclaredField("name");
name.setAccessible(true);
name.set(p1, "张楚岚");
System.out.println(p1);
// 调用私有的方法
Method showNation = clazz.getDeclaredMethod("showNation", String.class);
showNation.setAccessible(true);
// nation 就是Person类中showNation()方法的返回值
String nation = (String) showNation.invoke(p1, "China");
System.out.println(nation);
}
Class类
对于java.lang.Class类的理解
类的加载过程:
- 程序经过
javac.exe
命令以后,会生成一个或多个字节码文件(.class
结尾)。接着我们使用java.exe
命令对某个字节码文件进行解释运行。相当于将某个字节码文件加载到内存中。此过程就称为类的加载。加载到内存中的类,我们就称为运行时类,此运行时类,就作为Class
的一个实例。换句话说,Class
的实例就对应着一个运行时类。 - 加载到内存中的运行时类,会缓存一定的时间。在此时间之内,我们可以通过不同的方式来获取此运行时类。
获取Class实例的方法
示例代码:
@Test
public void test3() throws ClassNotFoundException {
// 方式一:调用运行时类的属性:.class
Class<Person> clazz1 = Person.class;
//Class clazz1 = Person.class;
System.out.println(clazz1);
// 方式二:通过运行时类的对象
Person p1 = new Person();
Class clazz2 = p1.getClass();
System.out.println(clazz2);
// 方式三:调用Class的静态方法 forNmae(String classPath)
Class clazz3 = Class.forName("com.sleep.reflection.Person");
System.out.println(clazz3);
System.out.println(clazz1 == clazz2);
System.out.println(clazz1 == clazz3);
// 方式四:使用类的加载器 ClassLoader
ClassLoader classLoader = ReflectionTest.class.getClassLoader();
Class clazz4 = classLoader.loadClass("com.sleep.reflection.Person");
System.out.println(clazz1 == clazz4);
}
Class实例可以代表的结构
-
class
:外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类 -
interface
:接口 -
[]
:数组 -
enum
:枚举 -
annotation
:注解@interface
-
primitive type
:基本数据类型 -
void
在Java中,万事万物皆对象
代码示例:
@Test
public void test4(){
Class<Object> c1 = Object.class;
Class<Comparable> c2 = Comparable.class;
Class<String[]> c3 = String[].class;
Class<int[][]> c4 = int[][].class;
Class<ElementType> c5 = ElementType.class;
Class<Override> c6 = Override.class;
Class<Integer> c7 = int.class;
Class<Void> c8 = void.class;
Class<Class> c9 = Class.class;
int[] i1 = new int[10];
int[] i2 = new int[100];
Class<? extends int[]> c10 = i1.getClass();
Class<? extends int[]> c11 = i2.getClass();
// 只要数组的元素类型与维度一样,就是同一个Class
System.out.println(c10 == c11);//true
}
类的加载
理解类的加载过程
当程序主动使用某个类时,如果该类还未被加载到内存中,则系统会通过以下三个步骤对该类进行初始化。

- 加载:将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后生成一个代表这个类的
java.lang.Class
对象,作为方法区中类数据的访问入口(即引用地址)。所有需要访问和使用类数据只能通过这个Class
对象。这个加载的过程需要类加载器参与。 - 链接:将Java类的二进制代码合并到JVM的运行状态之中的过程。
- 验证:确保加载的类信息符合JVM规范,例如:以cafe开头,没有安全方面的问题。
- 准备:正式为类变量(
static
)分配内存并设置类变量默认初始值的阶段,这些内存都将在方法区中进行分配。 - 解析:虚拟机常量池内的符号引用(常量名)替换为直接引用(地址)的过程。
- 初始化:
- 执行类构造器
<clinit>()
方法的过程。类构造器<clinit>()
方法是由编译期自动收集类中所有类变量的赋值动作和静态代码块中的语句合并产生的。(类构造器是构造类信息的,不是构造该类对象的构造器)。 - 当初始化一个类的时候,如果发现其父类还没有进行初始化,则需要先触发其父类的初始化。
- 虚拟机会保证一个类的
<clinit>()
方法在多线程环境中被正确加锁和同步。
- 执行类构造器
代码示例:
public class ClassLoadingTest{
public static void main (String [] args){
System.out.println(test.m);
}
}
class test{
static {
m = 300;
}
static int m = 100;
}
//第一步:加载
//第二步:链接结束后m=0
//第三步:初始化结束后,m的值由<clinit>()方法执行决定
/*
这个test构造器<clinit>()方法由类变量的赋值和静态代码块中的语句按照顺序合并产生,类似于
<clinit>(){
m = 300;
m = 100;
}
*/
Java类编译、运行的流程

类的加载器的作用
- 将
class
文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数据结构,然后在堆中生成一个代表这个类的java.lang.Class
对象,作为方法区中类数据的访问入口。 - 类缓存:标准的 JavaSE类加载器可以按要求查找类,但一旦某个类被加载到类加载器中,它将维持加载(缓存)一段时间。不过JVM垃圾回收机制可以回收这些
Class
对象
类加载器的分类

代码示例:
public class ClassLoaderTest {
@Test
public void test1() {
// 对于自定义类,使用系统类加载器进行加载
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
System.out.println(classLoader);
// 调用系统类加载器的getParent():获取扩展类加载器
ClassLoader classLoader1 = classLoader.getParent();
System.out.println(classLoader1);
// 调用扩展类加载器的getParent():无法获取引导类加载器
// 引导类加载器主要负责加载Java的核心类库,无法加载自定义类的。
ClassLoader classLoader2 = classLoader1.getParent();
System.out.println(classLoader2); // null
// String类是引导类加载器加载的,这里获取不到加载器信息,说明引导类加载器无法直接获取
ClassLoader classLoader3 = String.class.getClassLoader();
System.out.println(classLoader3); // null
}
}
使用Classloader加载src目录下的配置文件
/**
* Properties类:用来读取配置.properties文件
*/
@Test
public void test2() {
// FileInputStream fis = null;
// try {
// // 读取配置文件的方式一:
// // 路径这么写,此时的文件默认在当前工程的目录下。
// // 如果你的代码在工程目录下module里,那么文件默认就在当前的module下。
// Properties pros = new Properties();
// fis = new FileInputStream("jdbc.properties");
// pros.load(fis);
//
// String user = pros.getProperty("user");
// String password = pros.getProperty("password");
// System.out.println("user = " + user + ",password = " + password);
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// if (fis != null) {
// try {
// fis.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// 读取配置文件的方式二:使用ClassLoader
// 配置文件默认识别为:当前module的src下;如果没有Module,就是当前工程目录的src下
InputStream is = null;
try {
ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
is = classLoader.getResourceAsStream("jdbc1.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("passwrod");
System.out.println("user = " + user + ",password = " + password);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
反射的应用
创建运行时类的对象
代码示例:
public class NewInstanceTest {
@Test
public void test1() throws Exception {
// 方式一:
Class<Person> clazz1 = Person.class;
// 方式二:
Class<Person> clazz2 = (Class<Person>) Class.forName("com.sleep.reflection.Person");
Person person1 = clazz1.newInstance();
Person person2 = clazz2.newInstance();
System.out.println(person1);
System.out.println(person2);
}
}
说明:
newInstance()
:调用此方法,创建对应的运行时类的对象。内部调用了运行时类的空参的构造器。
要想此方法正常的创建运行时类的对象,要求:
- 运行时类必须提供空参的构造器。
- 空参的构造器的访问权限得够。通常,设置为
public
。
在javabean
中要求提供一个public
的空参构造器。原因:
- 便于通过反射,创建运行时类的对象。
- 便于子类继承此运行时类时,默认调用
super()
时,保证父类有此构造器。
体会反射的动态性
示例代码:
@Test
public void test2() {
for(int i = 0;i < 100;i++){
int num = new Random().nextInt(3);//0,1,2
String classPath = "";
switch(num){
case 0:
classPath = "java.util.Date";
break;
case 1:
classPath = "java.lang.Object";
break;
case 2:
classPath = "com.sleep.reflection.Person";
break;
}
try {
Object obj = getInstance(classPath);
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 创建一个指定类的对象。
* classPath:指定类的全类名
* 只有在程序运行起来之后,我们才能知道创建的是哪个对象,这就是动态性的体现
*/
public Object getInstance(String classPath) throws Exception {
Class<?> clazz = Class.forName(classPath);
return clazz.newInstance();
}
获取运行时类的完整结构
我们可以通过反射,获取对应的运行时类中所有的属性、方法、构造器、父类、接口、父类的泛型、包、注解、异常等。
-
实现的全部接口:
public Class<?>[] getInterfaces()
确定此对象所表示的类或接口实现的接口。 -
所继承的父类:
public Class<? Super T> getSuperclass()
返回表示此Class
所表示的实体(类、接口、基本类型)的父类的Class
。 -
全部的构造器:
public Constructor<T>[] getConstructors()
返回此Class对象所表示的类的所有public构造方法。
public Constructor<T>[] getDeclaredConstructors()
返回此Class对象表示的类声明的所有构造方法。
在
Constructor
类中:- 取得修饰符:
public int getModifiers();
- 取得方法名称:
public String getName();
- 取得参数的类型:
public Class<?> getParameterTypes();
- 取得修饰符:
-
全部的方法:
public Method[] getDeclaredMethods()
返回此Class对象所表示的类或接口的全部方法
public Method[] getMethods()
返回此Class对象所表示的类或接口的
public
的方法Method
类中:-
public Class<?> getReturnType()
取得全部的返回值 -
public Class<?>[] getParameterTypes()
取得全部的参数 -
public int getModifiers()
取得修饰符 -
public Class<?> [] getEXceptionTypes()
取得异常信息
-
-
全部的
Field
:public Field[] getFields()
返回此
Class
对象所表示的类或接口的public
的Field
。public Field[] getDeclaredFields()
返回此Class对象所表示的类或接口的全部
Field
Field
方法中-
public int getModifiers()
以整数形式返回此Field
的修饰符 -
public Class<?> getType()
得到Field
的属性类型 -
public String getName()
返回Field
的名称。
-
-
Annotation
相关get Annotation(Class<T> annotationClass)
getDeclaredAnnotations()
-
泛型相关
获取父类泛型类型:
Type getGenericSuperclass()
泛型类型:
ParameterizedType
获取实际的泛型类型参数数组:
getActualTypeArguments()
-
类所在的包
Package getPackage()
准备工作,创建下列的类、接口、注解:
Creature
类:
public class Creature<T> implements Serializable {
private char gender;
public double weight;
private void breath() {
System.out.println("生物呼吸");
}
public void eat() {
System.out.println("生物吃东西");
}
}
MyAnnotation
注解:
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String[] value() default "hello";
}
MyInterface
接口:
public interface MyInterface {
void info();
}
Person
类:
@MyAnnotation(value = "hi")
public class Person extends Creature<String> implements Comparable<String>, MyInterface {
private String name;
int age;
public int id;
public Person() {
}
@MyAnnotation(value = "abc")
private Person(String name) {
this.name = name;
}
Person(String name, int age) {
this.name = name;
this.age = age;
}
@MyAnnotation
private String show(String nation) {
System.out.println("我的国籍是:" + nation);
return nation;
}
public String display(String insterests) {
return insterests;
}
@Override
public void info() {
System.out.println("我是冯宝宝");
}
@Override
public int compareTo(String o) {
return 0;
}
private static void showDesc() {
System.out.println("机智一批冯宝宝");
}
}
获取运行时类的属性(Field)结构
代码示例:
public class FieldTest {
@Test
public void test1() {
Class<Person> clazz = Person.class;
// 获取属性结构
// getFields():获取当前运行时类及其父类中声明为public访问权限的属性
Field[] fields = clazz.getFields();
for (Field f : fields) {
System.out.println(f);
}
System.out.println();
// getDeclaredFields():获取当前运行时类中声明的所有属性。(不包含父类中声明的属性)
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f : declaredFields) {
System.out.println(f);
}
}
//权限修饰符 数据类型 变量名
@Test
public void test2() throws ClassNotFoundException {
Class<?> clazz = Class.forName("com.sleep.reflection.structure.Person");
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f :
declaredFields) {
//1.权限修饰符
int modifiers = f.getModifiers();
System.out.print(Modifier.toString(modifiers)+"\t");
//2.数据类型
Class<?> type = f.getType();
System.out.print(type.getName()+"\t");
//3.变量名
String fName = f.getName();
System.out.print(fName);
System.out.println();
}
}
}
获取运行时类的方法(Method)结构
代码示例:
public class MethodTest {
@Test
public void test1() {
Class<Person> clazz = Person.class;
//getMethods():获取当前运行时类及其所有父类中声明为public权限的方法
Method[] methods = clazz.getMethods();
for (Method m : methods) {
System.out.println(m);
}
System.out.println("============");
//getDeclaredMethods():获取当前运行时类中声明的所有方法。(不包含父类中声明的方法)
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods) {
System.out.println(m);
}
}
/*
@Xxxx
权限修饰符 返回值类型 方法名(参数类型1 形参名1,...) throws XxxException{}
*/
@Test
public void test2() throws ClassNotFoundException {
Class<?> clazz = Class.forName("com.sleep.reflection.structure.Person");
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods) {
// 1.获取方法声明的注解
Annotation[] annos = m.getAnnotations();
for (Annotation a : annos) {
System.out.println(a);
}
// 2.权限修饰符
System.out.print(Modifier.toString(m.getModifiers())+"\t");
// 3.返回值类型
System.out.print(m.getReturnType().getName() + "\t");
// 4.方法名
System.out.print(m.getName());
System.out.print("(");
// 5.形参列表
Class<?>[] parameterTypes = m.getParameterTypes();
if (!(parameterTypes == null && parameterTypes.length == 0)) {
for (int i = 0; i < parameterTypes.length; i++) {
if (i == parameterTypes.length - 1) {
System.out.print(parameterTypes[i].getName() + " args_" + i);
break;
}
System.out.print(parameterTypes[i].getName() + "args_" + i + ",");
}
}
System.out.print(") ");
// 6.抛出的异常
Class<?>[] exceptionTypes = m.getExceptionTypes();
if (exceptionTypes.length > 0){
System.out.print("throws ");
for (int i = 0; i < exceptionTypes.length; i++) {
if (i==exceptionTypes.length -1){
System.out.print(exceptionTypes[i].getName());
break;
}
System.out.print(exceptionTypes[i].getName()+", ");
}
}
System.out.println();
}
}
}
获取运行时的其他结构
代码示例:
public class OtherTest {
/*
获取构造器结构
*/
@Test
public void test1() {
Class<Person> clazz = Person.class;
//getConstructors():获取当前运行时类中声明为public的构造器
Constructor<?>[] constructors = clazz.getConstructors();
for (Constructor c : constructors) {
System.out.println(c);
}
System.out.println("================");
//getDeclaredConstructors():获取当前运行时类中声明的所有的构造器
Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors();
for (Constructor c : declaredConstructors) {
System.out.println(c);
}
}
/*
获取运行时类的父类
*/
@Test
public void test2(){
Class<Person> clazz = Person.class;
Class<? super Person> superclass = clazz.getSuperclass();
System.out.println(superclass);
}
/*
获取运行时类的带泛型的父类
*/
@Test
public void test3(){
Class<Person> clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
System.out.println(genericSuperclass);
}
/*
获取运行时类的带泛型的父类的泛型
代码:逻辑性代码 vs 功能性代码
*/
@Test
public void test4(){
Class clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass;
//获取泛型类型
Type[] actualTypeArguments = paramType.getActualTypeArguments();
// System.out.println(actualTypeArguments[0].getTypeName());
System.out.println(((Class)actualTypeArguments[0]).getName());
}
/*
获取运行时类实现的接口
*/
@Test
public void test5(){
Class clazz = Person.class;
Class[] interfaces = clazz.getInterfaces();
for(Class c : interfaces){
System.out.println(c);
}
System.out.println();
//获取运行时类的父类实现的接口
Class[] interfaces1 = clazz.getSuperclass().getInterfaces();
for(Class c : interfaces1){
System.out.println(c);
}
}
/*
获取运行时类所在的包
*/
@Test
public void test6(){
Class clazz = Person.class;
Package pack = clazz.getPackage();
System.out.println(pack);
}
/*
获取运行时类声明的注解
*/
@Test
public void test7(){
Class clazz = Person.class;
Annotation[] annotations = clazz.getAnnotations();
for(Annotation annos : annotations){
System.out.println(annos);
}
}
}
调用运行时类的指定结构
调用指定的属性
在反射机制中,可以直接通过Field
类操作类中的属性,通过Field
类提供的set()
和get()
方法就可以完成设置和取得属性内容的操作。
-
public Field getField(String name)
返回此Class
对象表示的类或接口的指定的public
的Field
。 -
public Field getDeclaredField(String name)
返回此Class
对象表示的类或接口的指定的Field
。在
Field
中: -
public Object get(object obj)
取得指定对象obj上此Field
的属性内容 -
public void set(Object obj,Object value)
设置指定对象obj上此Field
的属性内容
代码示例:
public class ReflectionTest {
@Test
public void testField() throws Exception {
Class clazz = Person.class;
//创建运行时类的对象
Person p = (Person) clazz.newInstance();
//1. getDeclaredField(String fieldName):获取运行时类中指定变量名的属性
Field name = clazz.getDeclaredField("name");
//2.保证当前属性是可访问的
name.setAccessible(true);
//3.获取、设置指定对象的此属性值
name.set(p,"Tom");
System.out.println(name.get(p));
}
}
调用指定的方法(常用)
通过反射,调用类中的方法,通过Method类完成。步骤:
- 通过Class类的getMethod(String name,Class… parameterTypes)方法取得一个Method对象,并设置此方法操作时所需要的参数类型。
- 之后使用 Object invoke(Object obj, Object[] args)进行调用,并向方法中传递要设置的ob对象的参数信息。

Object invoke(object obj,Object... args)
方法:
-
Object
对应原方法的返回值,若原方法无返回值,此时返回null
- 若原方法若为静态方法,此时形参
obj
可为null
- 若原方法形参列表为空,则
args
为null
- 若原方法声明为
private
,则需要在调用此invoke()
方法前,显式调用方法对象的setAccessible(true)
方法,将可访问private
的方法。
关于setAccessible()
方法的使用:
-
Method
和Field
、Constructor
对象都有setAccessible()
方法。 -
setAccessible
是启动和禁用访问安全检查的开关 - 参数值为
true
则指示反射的对象在使用时应该取消Java语言访问检査。 - 提高反射的效率。如果代码中必须用反射,而该句代码需要频繁的被调用,那么请设置为
true
. 使得原本无法访问的私有成员也可以访问 - 参数值为
false
则指示反射的对象应该实施Java语言访问检査。
代码示例:
@Test
public void testMethod() throws Exception {
Class<Person> clazz = Person.class;
//创建运行时类的对象
Person person = clazz.newInstance();
/*
1.获取指定的某个方法
getDeclaredMethod():参数1 :指明获取的方法的名称 参数2:指明获取的方法的形参列表
*/
Method show = clazz.getDeclaredMethod("show", String.class);
//2.保证当前方法是可访问的
show.setAccessible(true);
/*
3. 调用方法的invoke():参数1:方法的调用者 参数2:给方法形参赋值的实参
invoke()的返回值即为对应类中调用的方法的返回值。
*/
Object returnValue = show.invoke(person, "CHN");
System.out.println(returnValue);
System.out.println("*************如何调用静态方法*****************");
Method showDesc = clazz.getDeclaredMethod("showDesc");
showDesc.setAccessible(true);
//如果调用的运行时类中的方法没有返回值,则此invoke()返回null
//Object returnVal = showDesc.invoke(null); // 调用静态方法,可以写null为参数
Object returnVal = showDesc.invoke(Person.class);
System.out.println(returnVal); // null
}
调用指定的构造器
代码示例:
@Test
public void testConstructor() throws Exception {
Class clazz = Person.class;
//private Person(String name)
/*
1.获取指定的构造器
getDeclaredConstructor():参数:指明构造器的参数列表
*/
Constructor constructor = clazz.getDeclaredConstructor(String.class);
//2.保证此构造器是可访问的
constructor.setAccessible(true);
//3.调用此构造器创建运行时类的对象
Person per = (Person) constructor.newInstance("Tom");
System.out.println(per);
}
网友评论