利用字节码文件的对象,调用构造器:
作者:互联网
利用字节码文件的对象,调用构造器:
package com.Reflect;
//利用字节码文件的对象,调用构造器
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class aClass = Class.forName("com.Reflect.Demo01");
//declaredConstructor可以调所有的运行构造器
Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class,double.class,int.class);
Object o = declaredConstructor.newInstance("小明", 186.8, 120);
//constructor可以调用有参构造器
Constructor constructor = aClass.getConstructor(String.class, double.class, int.class);
Object o1 = constructor.newInstance("小红", 175.3, 110);
System.out.println(o);
System.out.println(o1);
}
}
标签:aClass,调用,字节,constructor,构造,Constructor,declaredConstructor,class,String 来源: https://www.cnblogs.com/188221creat/p/16186006.html