反射(1)——构造方法

来源:互联网 发布:苹果淘宝下载 编辑:程序博客网 时间:2024/05/29 10:27

反射(1)——构造方法

1:(1)构造方法——返回一个具体的具有public属性的构造函数
Constructor getConstructor(Class[] params)根据构造函数的参数,返回一个具体的具有public属性的构造函数

 public static void main(String[] args) {                //返回一个 Constructor 对象,该对象反映此 Class 对象所表示的类或接口的指定构造方法。                    try {                          //返回一个 Constructor 对象,它反映此 Class 对象所表示的类的指定公共构造方法。                          Constructor<?> cc = c.getConstructor();                          System.out.println(cc);                    } catch (SecurityException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    } catch (NoSuchMethodException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                } catch (ClassNotFoundException e) {                    e.printStackTrace();                }            }        }}

结果显示:
public com.sram.ss.User()

(2)构造方法——查找所有公共构造方法
Constructor getConstructors() 返回所有具有public属性的构造函数数组

public class UserClass1 {        public static void main(String[] args) {                try {                    Class<?> c = Class.forName("com.sram.ss.User");                    //所有公共构造方法                     Constructor<?>[] constructors = c.getConstructors() ;                     for (Constructor<?> constructor : constructors) {                        System.out.println(constructor);                } catch (ClassNotFoundException e) {                    e.printStackTrace();                }            }        }}

结果显示:
public com.sram.ss.User()

(3)构造方法——返回一个具体的构造函数
Constructor getDeclaredConstructor(Class[] params) 根据构造函数的参数,返回一个具体的构造函数(不分public和非public属性)

//返回一个 Constructor 对象,该对象反映此 Class 对象所表示的类或接口的指定构造方法。                    public class UserClass1 {        public static void main(String[] args) {                //返回一个 Constructor 对象,该对象反映此 Class 对象所表示的类或接口的指定构造方法。                    try {                        Constructor<?> cc = c.getDeclaredConstructor();                        System.out.println(cc);                    } catch (SecurityException e) {                        e.printStackTrace();                    } catch (NoSuchMethodException e) {                        e.printStackTrace();                    }            }        }}

结果显示:
public com.sram.ss.User()

(4)构造方法——查找所有构造方法
Constructor getDeclaredConstructors() 返回该类中所有的构造函数数组(不分public和非public属性)

 public static void main(String[] args) {                 Constructor<?>[] cc =c. getDeclaredConstructors();                          for (Constructor<?> constructor : cc) {                                System.out.println(constructor);                            }                    } catch (SecurityException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                } catch (ClassNotFoundException e) {                    e.printStackTrace();                }                }}

结果显示:
public com.sram.ss.User(java.lang.String,java.lang.String,int,java.lang.String)
public com.sram.ss.User(java.lang.String,int,java.lang.String)
public com.sram.ss.User()

测试的实体类

总结:    Class类提供了四个public方法,用于获取某个类的构造方法    Constructor getConstructor(Class[] params)根据构造函数的参数,返回一个具体的具有public属性的构造函数    Constructor getConstructors()     返回所有具有public属性的构造函数数组    Constructor getDeclaredConstructor(Class[] params)根据构造函数的参数,返回一个具体的构造函数(不分public和非public属性)    Constructor getDeclaredConstructors()    返回该类中所有的构造函数数组(不分public和非public属性)区别:(1)getConstructorgetDeclaredConstructor区别getDeclaredConstructor:返回指定参数类型、所有声明的(包括private)构造函数getConstructor:返回指定参数类型、具有public访问权限的构造函数代码分析:A {    private String str;    public A() {         str = "first";     }    private A(String str) {           this.str = str;    }    public String getStr() {       return str;     }}void main(String[] args) {    Class cls = Class.forName("com.test.A");    Class[] paramsType = { String.class };    Object[] params = { "second" };    Constructor con = cls.getDeclaredConstructor(paramsType);    con.setAccessible(true);    //public void setAccessible(boolean flag)    //将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。 如果该成员变量为私有,可用该方法冲破封装对成员变量进行赋值,获取属性值    A a = (A)con.newinstanct(params);    a.getStr();// second}
原创粉丝点击