反射获取构造方法与练习

 1 package ClassTest.ClassTest01;
 2 
 3 import java.lang.reflect.Constructor;
 4 import java.lang.reflect.InvocationTargetException;
 5 
 6 /**
 7  * FileName: ReflectDemo03
 8  * Author:   lps
 9  * Date:     2022/4/11 16:15
10  * Sign:刘品水 Q:1944900433
11  */
12 public class ReflectDemo03 {
13     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
14 
15         Class<?> c = Class.forName("ClassTest.ClassTest01.Student");
16         Constructor<?>[] cons = c.getConstructors();
17         for (Constructor con : cons) {
18             System.out.println(con);
19         }
20         System.out.println("======");
21         Constructor<?>[] decons = c.getDeclaredConstructors();
22         for (Constructor decon : decons) {
23             System.out.println(decon);
24         }
25         System.out.println("======");
26         //String.class,int.class,String.class
27         Constructor<?> con = c.getConstructor();
28         //T newInstance() 已过时。
29         //此方法传播由nullary构造函数抛出的任何异常,包括已检查的异常。 使用此方法有效地绕过编译器异常检查,否则将由编译器执行。
30         Object o = con.newInstance();
31         System.out.println(o);
32         Constructor<?> con11 = c.getConstructor(String.class, int.class, String.class);
33         Object o0 = con11.newInstance("刘品水", 21, "南京");
34         System.out.println(o0);
35 
36 
37         System.out.println("=========");
38         //private Student(String name)
39         //*Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 返回一个 Constructor对象,该对象反映由此 Class对象表示的类或接口的指定构造函数
40         Constructor<?> deco = c.getDeclaredConstructor(String.class);
41 
42         //暴力反射
43         //void setAccessible(boolean flag) 将此反射对象的 accessible标志设置为指示的布尔值。
44         //setAccessible
45         //public void setAccessible(boolean flag)将此反射对象的accessible标志设置为指示的布尔值。
46         // 值为true表示反射对象应该在使用Java语言访问控制时抑制检查。
47         // 值为false表示反射对象应该在使用Java语言访问控制时执行检查,并在类描述中指出变体。
48 
49         deco.setAccessible(true);
50 
51         Object o1 = deco.newInstance("刘品水");
52         System.out.println(o1);
53         //class ClassTest.ClassTest01.ReflectDemo04 cannot access a member of class ClassTest.
54         // ClassTest01.Student with modifiers "private"
55 
56 
57     }
58 }

 

package ClassTest.ClassTest01;

/**
 * FileName: Student
 * Author:   lps
 * Date:     2022/4/11 15:22
 * Sign:刘品水 Q:1944900433
 */
public class Student {
    private String name;
    int age;
    public String adress;

    public Student() {
    }

    private Student(String name) {
        this.name = name;
    }

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student(String name, int age, String adress) {
        this.name = name;
        this.age = age;
        this.adress = adress;
    }

    private void function(){
        System.out.println("function");
    }
    public void method1(){
        System.out.println("method");
    }
    public void method2(String s){
        System.out.println("method:"+s);
    }
    public String method3(String s, int i){
        return s+","+i;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", adress='" + adress + '\'' +
                '}';
    }
}

 

 

 

posted @ 2022-04-11 18:16  刘品水  阅读(27)  评论(0编辑  收藏  举报