getConstructor(Classpublic class Student { private String name; private int age; private Student ( ) { System . out. println ( “无参构造器执行” ) ; } public Student ( String name) { System . out. println ( “有参构造器执行” ) ; this . name = name; } public Student ( String name, int age) { System . out. println ( “有参构造器执行” ) ; this . name = name; this . age = age; } public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public int getAge ( ) { return age; } public void setAge ( int age) { this . age = age; } @Override public String toString ( ) { return “Student{“ + “name='” + name + ‘\” + “, age=” + age + ‘}’ ; } } getConstructors()获取全部public修饰的构造器
public static void main ( String [ ] args) { Class c = Student . class ; Constructor [ ] constructors = c. getConstructors ( ) ; for ( Constructor constructor : constructors) { System . out. println ( constructor. getName ( ) + "-->" + constructor. getParameterCount ( ) ) ; } }
getConstructors()获取全部构造器包括private修饰的构造器
public static void main ( String [ ] args) { Class c = Student . class ; Constructor [ ] constructors = c. getDeclaredConstructors ( ) ; for ( Constructor constructor : constructors) { System . out. println ( constructor. getName ( ) + "-->" + constructor. getParameterCount ( ) ) ; } }
getConstructor()返回指定的单个构造器(public修饰的);
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Constructor constructor = c. getConstructor ( String . class , int . class ) ; System . out. println ( constructor. getName ( ) + "-->" + constructor. getParameterCount ( ) ) ; }
getDeclaredConstructor()返回指定的单个构造器(包括private修饰的)
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Constructor constructor1 = c. getDeclaredConstructor ( ) ; System . out. println ( constructor1. getName ( ) + "-->" + constructor1. getParameterCount ( ) ) ; Constructor constructor2 = c. getDeclaredConstructor ( String . class ) ; System . out. println ( constructor2. getName ( ) + "--->" + constructor2. getParameterCount ( ) ) ; }
获取构造器的作用: 依然是初始化一个对象返回。
Constructor类中用于创建对象的方法如下 :
setAccessible作用, 如果遇到私有构造器可以使用setAccessible方法暴力反射, 将权限打开(只打开一次 )
符号 说明 newInstance(Object… initargs) 根据指定的构造器创建对象 setAccessible(boolean flag) 设置为true,表示取消访问检查,进行暴力反射
获取构造器演示代码 :
无参私有构造器创建对象
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Constructor constructor1 = c. getDeclaredConstructor ( ) ; System . out. println ( constructor1. getName ( ) + "-->" + constructor1. getParameterCount ( ) ) ; constructor1. setAccessible ( true ) ; Student stu = ( Student ) constructor1. newInstance ( ) ; System . out. println ( stu) ; }
公开有参构造器创建对象
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Constructor constructor2 = c. getDeclaredConstructor ( String . class ) ; System . out. println ( constructor2. getName ( ) + "-->" + constructor2. getParameterCount ( ) ) ; Student stu2 = ( Student ) constructor2. newInstance ( "chenyq" ) ; System . out. println ( stu2) ; }
反射获取成员变量对象 流程如下 :
利用反射技术获取成员变量并使用 :
反射的第一步是先得到类对象,然后从类对象中获取类的成分对象。
Class类中用于获取成员变量的方法如下 :
方法 说明 getFields() 返回所有成员变量对象的数组(只能拿public的) getDeclaredFields() 返回所有成员变量对象的数组,存在就能拿到 getField(String name) 返回单个成员变量对象(只能拿public的) getDeclaredField(String name) 返回单个成员变量对象,存在就能拿到
获取成员变量演示代码 :
首先还是定义一个Student类进行测试
public class Student { private String name; private int age; public static String schoolName; public static final String COUNTTRY = "中国" ; private Student ( ) { System . out. println ( "无参构造器执行" ) ; } public Student ( String name) { System . out. println ( "有参构造器执行" ) ; this . name = name; } public Student ( String name, int age) { System . out. println ( "有参构造器执行" ) ; this . name = name; this . age = age; } public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public int getAge ( ) { return age; } public void setAge ( int age) { this . age = age; } @Override public String toString ( ) { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}' ; } }
getFields()方法, 返回public修饰的所有成员变量对象的数组
public static void main ( String [ ] args) { Class c = Student . class ; Field [ ] fields = c. getFields ( ) ; for ( Field field : fields) { System . out. println ( field. getName ( ) ) ; } }
getDeclaredFields()方法, 返回所有成员变量对象的数组
public static void main ( String [ ] args) { Class c = Student . class ; Field [ ] fields = c. getDeclaredFields ( ) ; for ( Field field : fields) { System . out. println ( field. getName ( ) ) ; } }
getField()方法, 根据名称获取单个public修饰的代码
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Field field = c. getField ( "schoolName" ) ; System . out. println ( field. getName ( ) ) ; }
getDeclaredField()方法, 根据名称获取单个任何修饰符修饰的代码
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Field field = c. getDeclaredField ( "name" ) ; System . out. println ( field. getName ( ) ) ; }
获取成员变量的作用: 依然是在某个对象中取值、赋值
Field类中用于取值、赋值的方法 :
Object obj
: 要赋值的对象
符号 说明 set(Object obj, Object value) 赋值 get(Object obj) 获取值, 返回Object类型。 setAccessible(boolean flag) 设置为true,表示取消访问检查,进行暴力反射
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Field fName = c. getDeclaredField ( "name" ) ; System . out. println ( fName. getName ( ) ) ; fName. setAccessible ( true ) ; Student stu = new Student ( ) ; fName. set ( stu, "abc" ) ; String name = ( String ) fName. get ( stu) ; System . out. println ( name) ; }
注意: 反射可以破坏封装性,私有的也可以执行了 。
反射获取方法对象
利用反射技术获取方法对象并使用 :
反射的第一步是先得到类对象,然后从类对象中获取类的成分对象。
Class类中用于获取成员方法的方法 :
方法 说明 getMethods() 返回所有成员方法对象的数组(只能拿public的) getDeclaredMethods() 返回所有成员方法对象的数组,存在就能拿到 getMethod(String name, Classpublic class Student { private void running ( String name) { System . out. println ( name + “学生在跑步” ) ; } public void eating ( ) { System . out. println ( “学生在吃饭” ) ; } public void swimming ( ) { System . out. println ( “学生在游泳” ) ; } } getMethods()方法, 获取所有public修饰的成员方法
public static void main ( String [ ] args) { Class c = Student . class ; Method [ ] methods = c. getMethods ( ) ; for ( Method method : methods) { System . out. println ( method. getName ( ) ) ; } }
getDeclaredMethods()方法, 获取所有的方法
public static void main ( String [ ] args) { Class c = Student . class ; Method [ ] methods = c. getDeclaredMethods ( ) ; for ( Method method : methods) { System . out. println ( method. getName ( ) ) ; } }
getMethod()方法, 根据方法名获取public修饰的单个方法
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Method method = c. getMethod ( "swimming" ) ; System . out. println ( method. getName ( ) ) ; }
getDeclaredMethod()方法, 根据方法名获取任意修饰符的单个方法
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Method method = c. getDeclaredMethod ( "running" ) ; System . out. println ( method. getName ( ) ) ; }
获取成员方法的作用依然是在某个对象中进行执行此方法
Method类中用于触发执行的方法 :
符号 说明 invoke(Object obj, Object… args) 运行方法 参数一:用哪个对象调用该方法 参数二:调用方法的传递的参数(如果没有就不写) 返回值:方法的返回值(如果没有就不接返回值)
public static void main ( String [ ] args) throws Exception { Class c = Student . class ; Method method = c. getDeclaredMethod ( "running" , String . class ) ; System . out. println ( method. getName ( ) ) ; method. setAccessible ( true ) ; Student stu = new Student ( ) ; method. invoke ( stu, "小黑" ) ; }