Reflection -Construct-Java

 1 import net.dsmxx.PersonFk;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.InvocationTargetException;
 5 
 6 /**
 7  * Created with IDEA
 8  * author:foreign
 9  * Date:2019/9/29
10  * Time:20:02
11  */
12 public class ReflectionFk {
13     public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
14         . Clazz = PersonFk class class ;
 15          // . 1 acquired public constructor 
16          the Constructor [] = Constructors clazz.getConstructors ();
 . 17          for (the Constructor constructor: Constructors) {
 18 is              System.out.println ( "public constructor: "+ constructor);
 . 19          }
 20 is          // 2 configuration parameter acquisition method 
21 is          the constructor constructor = clazz.getConstructor ( new new class [] {String. class , Integer. class , Boolean. class });
 22 is          System.out.println ( "String type of construction method:" +constructor);
 23 is          // . 3 constructor parameter 
24          Class [] The parameterTypes = Constructor.getParameterTypes ();
 25          for (para Class: The parameterTypes) {
 26 is              System.out.println ( "constructor parameters:" + para);
 27          }
 28          @ using a constructor object is instantiated class 
29          the constructor constructor1 = clazz.getConstructor ( new new class [] {String. class , Integer. class , Boolean. class });
 30          Object O = constructor1.newInstance ( "Foreign", 27, to true );
 31 is         System.out.println("对象:" + o);
32         PersonFk personFk = (PersonFk) o;
33         System.out.println("对象name:" + personFk.getName());
34         System.out.println("对象name:" + personFk.getAge());
35         System.out.println("对象name:" + personFk.getGender());
36     }
37 }

 

Guess you like

Origin www.cnblogs.com/fangke/p/11609974.html