JavaSE-24.2.2【反射获取构造方法并使用】

 1 package day15.lesson2;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.InvocationTargetException;
 5 
 6 /*
 7 2.3 反射获取构造方法并使用
 8 
 9     (1)Class类获取构造方法对象的方法
10         Constructor<?>[] getConstructors() 返回所有公共构造方法对象的数组
11         Constructor<?>[] getDeclaredConstructors() 返回所有构造方法对象的数组
12         Constructor getConstructor(Class<?>... parameterTypes) 返回单个公共构造方法对象
13         Constructor getDeclaredConstructor(Class<?>... parameterTypes) 返回单个构造方法对象
14             Class<?>... parameterTypes:参数为要获取的构造方法的参数的个数和数据类型对应的字节码文件对象
15 
16     (2)Constructor类用于创建对象的方法
17         T newInstance(Object...initargs) 根据指定的构造方法创建对象
18  */
19 public class Demo2Reflect {
20     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
21         //获取Class对象
22         Class<?> c = Class.forName("day15.lesson2.Student");
23 
24         //获取公共构造方法
25         Constructor<?>[] cons = c.getConstructors();
26         for (Constructor con: cons){
27             System.out.println(con);
28         }
29         /*
30         public day15.lesson2.Student(java.lang.String,int,java.lang.String)
31         public day15.lesson2.Student()
32         */
33 
34         //-------------------------------------------------------
35 
36         //获取所有构造方法
37         Constructor<?>[] cons2 = c.getDeclaredConstructors();
38         for (Constructor con: cons2){
39             System.out.println(con);
40         }
41         /*
42         day15.lesson2.Student(java.lang.String,int)
43         private day15.lesson2.Student(java.lang.String)
44         public day15.lesson2.Student(java.lang.String,int,java.lang.String)
45         public day15.lesson2.Student()
46          */
47 
48         //-------------------------------------------------------
49 
50         //获取单个公共构造方法
51         Constructor<?> con = c.getConstructor(); //抛出异常NoSuchMethodException
52         //通过类的字节码文件对象c得到构造方法对象con
53 
54         /*Student s = new Student(); //无参构造
55         System.out.println(s);*/
56         //通过反射实现上行功能
57         Object obj = con.newInstance(); //抛出异常
58         System.out.println(obj); //Student{name='null', age=0, address='null'}  <--重写toString()后不输出地址
59 
60         //-------------------------------------------------------
61 
62         //获取单个构造方法(公共、私有、默认皆可)
63         Constructor<?> con2 = c.getDeclaredConstructor(String.class, int.class, String.class);
64         Object obj2 = con2.newInstance("tom", 20, "南京");
65         System.out.println(obj2); //Student{name='tom', age=20, address='南京'}
66     }
67 }
 1 class Student{
 2     //成员变量
 3     private String name; //私有
 4     int age; //默认
 5     public String address; //公共
 6 
 7     //构造方法
 8     public Student() { //公共
 9     }
10 
11     public Student(String name, int age, String address) { //公共
12         this.name = name;
13         this.age = age;
14         this.address = address;
15     }
16 
17     private Student(String name){ //私有
18         this.name = name;
19     }
20 
21     Student(String name, int age){ //默认
22         this.name = name;
23         this.age = age;
24     }
25 
26     //成员方法
27     private void function(){ //私有
28         System.out.println("function");
29     }
30 
31     public void method1(){ //公共
32         System.out.println("method");
33     }
34 
35     public void method2(String s){ //公共
36         System.out.println("method:" + s);
37     }
38 
39     public String method3(String s, int i){ //公共
40         return s + "," + i;
41     }
42 
43     @Override
44     public String toString() { //公共
45         return "Student{" +
46                 "name='" + name + '\'' +
47                 ", age=" + age +
48                 ", address='" + address + '\'' +
49                 '}';
50     }
51 }
 1 package day15.lesson2;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.InvocationTargetException;
 5 
 6 /*
 7 2.4 反射获取构造方法并使用练习1
 8 
 9     通过反射获取公共的构造方法并创建对象
10 
11     通过反射实现如下操作
12         Student s = new Student("tom", 23, "南京");
13         System.out.println(s);
14 
15 2.5 反射获取构造方法并使用练习2
16 
17     通过反射获取私有构造方法并创建对象
18 
19     通过反射实现如下操作
20         Student s = new Student("tom");
21         System.out.println(s);
22  */
23 public class Demo3Reflect {
24     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
25         //获取Student类对应的字节码文件Class对象
26         Class<?> c = Class.forName("day15.lesson2.Student");
27 
28         Constructor<?> con = c.getConstructor(String.class, int.class, String.class); //构造方法对象,该方法只能获取公共构造
29         //基本数据类型也可以通过.class得到对应的class类型,而不需要借助其包装类
30 
31         Object obj = con.newInstance("tom", 23, "南京"); //通过反射得到Student类的对象obj
32         System.out.println(obj); //Student{name='tom', age=23, address='南京'}
33 
34         // ---------------------------------------------------------------------------------------
35 
36         Constructor<?> con2 = c.getDeclaredConstructor(String.class); //该方法可以获取所有类型的构造,包括私有、默认
37 
38 //        Object obj2 = con2.newInstance("tom"); //java.lang.IllegalAccessException
39         //由于构造方法是私有的,正常来说是不能外部访问的,报错!
40 
41         //但反射可以实现该功能-->暴力反射
42         con2.setAccessible(true); // 值为true,取消java的访问检查
43         Object obj2 = con2.newInstance("tom"); //Student{name='tom', age=0, address='null'}
44         System.out.println(obj2);
45     }
46 }

 

posted @ 2021-06-20 19:02  yub4by  阅读(47)  评论(0编辑  收藏  举报