java反射对象赋值_Java反射详解

反射(Reflection)能够让运行于 JVM 中的程序检测和修改运行时的行为。

Class类提供了以下方法

四个public方法,用于获取某个类的构造方法:

Constructor getConstructor(Class[] params)

// 根据构造函数的参数,返回一个具体的具有public属性的构造函数

Constructor getConstructors()

// 返回所有具有public属性的构造函数数组

Constructor getDeclaredConstructor(Class[] params)

// 根据构造函数的参数,返回一个具体的构造函数(不分public和非public属性)

Constructor getDeclaredConstructors()

// 返回该类中所有的构造函数数组(不分public和非public属性)

四种获取成员方法的方法

Method getMethod(String name, Class[] params)

// 根据方法名和参数,返回一个具体的具有public属性的方法

Method[] getMethods()

// 返回所有具有public属性的方法数组

Method getDeclaredMethod(String name, Class[] params)

// 根据方法名和参数,返回一个具体的方法(不分public和非public属性)

Method[] getDeclaredMethods()

// 返回该类中的所有的方法数组(不分public和非public属性)

四种获取成员属性的方法

Field getField(String name)

// 根据变量名,返回一个具体的具有public属性的成员变量

Field[] getFields()

// 返回具有public属性的成员变量的数组

Field getDeclaredField(String name)

// 根据变量名,返回一个成员变量(不分public和非public属性)

Field[] getDelcaredFields()

// 返回所有成员变量组成的数组(不分public和非public属性)

getDeclaredFields 和 getFields 的区别:

getDeclaredFields()获得某个类的所有申明的字段,即包括public、private和proteced,但是不包括父类的申明字段。

getFields()获得某个类的所有的公共(public)的字段,包括父类。

获取父类的方法

Class> superclass = class1.getSuperclass() ;

获取接口

Class>[] interS = class1.getInterfaces() ;

// 获取所有的接口

实例化

Object object = class1.newInstance() ;

//创建实例化:相当于 new 了一个对象

实例

Person.class

package reflection;

public class Person {

private String id;

private String name;

public String age;

public static String str = "Json";

//构造函数1

public Person() {

System.out.println( "构造函数 无参" );

}

//构造函数2

public Person(String id) {

this.id = id;

System.out.println( "构造函数 id : " + id );

}

//构造函数3

public Person(String id, String name) {

this.id = id;

this.name = name;

System.out.println( "构造函数 id : " + id + " name: " + name );

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getAge() {

return age;

}

public void setAge(String age) {

this.age = age;

}

/**

* 静态方法

*/

public static String update(int id) {

return str+id;

}

}

练习一:通过反射,获取对象实例,并且操作对象的方法

package reflection;

public class T1 {

public static void main(String[] args) {

try {

//创建类

Class> class1 = Class.forName("reflection.Person");

//创建实例化:相当于 new 了一个对象

Object object = class1.newInstance();

//向下转型

Person person = (Person) object;

person.setId("100");

person.setName("jack");

System.out.println("id: " + person.getId() + " name: " + person.getName());

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

}

运行结果:

id: 100 name: jack

练习二:通过反射获取对象字段属性,并且赋值

package reflection;

import java.lang.reflect.Field;

public class T1 {

public static void main(String[] args) {

try {

//创建类

Class> class1 = Class.forName("reflection.Person");

//创建实例

Object person = class1.newInstance();

//获得id 属性

Field idField = class1.getDeclaredField( "id" ) ;

//打破封装 实际上setAccessible是启用和禁用访问安全检查的开关,并不是为true就能访问为false就不能访问

//由于JDK的安全检查耗时较多.所以通过setAccessible(true)的方式关闭安全检查就可以达到提升反射速度的目的

idField.setAccessible( true );

//给id 属性赋值

idField.set(person, "100") ;

//打印 person 的属性值

System.out.println( idField.get( person ));

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace() ;

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

id 这个属性的是 private 私有的,不能修改它的值。需要添加 idField.setAccessible( true );

练习三:通过反射操作属性和方法

package reflection;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class T1 {

public static void main(String[] args) {

try {

//创建类

Class> class1 = Class.forName("reflection.Person");

//创建实例

Object person = class1.newInstance();

//获得id 属性

Field idField = class1.getDeclaredField( "id" ) ;

//打破封装 实际上setAccessible是启用和禁用访问安全检查的开关,并不是为true就能访问为false就不能访问

//由于JDK的安全检查耗时较多.所以通过setAccessible(true)的方式关闭安全检查就可以达到提升反射速度的目的

idField.setAccessible( true );

//给id 属性赋值

idField.set( person , "100") ;

//获取 setName() 方法

Method setName = class1.getDeclaredMethod( "setName", String.class ) ;

//打破封装

setName.setAccessible( true );

//调用setName 方法。

setName.invoke( person , "jack" ) ;

//获取name 字段

Field nameField = class1.getDeclaredField( "name" ) ;

//打破封装

nameField.setAccessible( true );

//打印 person 的 id 属性值

String id_ = (String) idField.get( person ) ;

System.out.println( "id: " + id_ );

//打印 person 的 name 属性值

String name_ = ( String)nameField.get( person ) ;

System.out.println( "name: " + name_ );

//获取 getName 方法

Method getName = class1.getDeclaredMethod( "getName" ) ;

//打破封装

getName.setAccessible( true );

//执行getName方法,并且接收返回值

String name_2 = (String) getName.invoke( person ) ;

System.out.println( "name2: " + name_2 );

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace() ;

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

运行结果:

id: 100

name: jack

name2: jack

练习四:静态属性、静态方法调用

package reflection;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

public class T1 {

public static void main(String[] args) {

try {

//创建类

Class> class1 = Class.forName("reflection.Person");

//获取 nameField 属性

Field nameField = class1.getDeclaredField( "str" ) ;

//获取 nameField 的值

String name_ = (String) nameField.get( nameField ) ;

//输出值

System.out.println( name_ );

//有返回值,有参数

Method getUpdateMethod = class1.getDeclaredMethod( "update" , int.class ) ;

String result_4 = (String) getUpdateMethod.invoke( null , 1 ) ;

System.out.println( "返回值: "+ result_4 );

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace() ;

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

练习五:调用构造函数,创建对象实例

package reflection;

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

public class T1 {

public static void main(String[] args) {

try {

//创建类

Class> class1 = Class.forName("reflection.Person");

//无参构造函数

Object object = class1.newInstance() ;

//有参构造函数:一个参数

Constructor> constructor = class1.getDeclaredConstructor( String.class ) ;

constructor.newInstance( "1000" ) ;

//有参构造函数:二个参数

Constructor> constructor2 = class1.getDeclaredConstructor( String.class , String.class ) ;

constructor2.newInstance( "1001" , "jack" ) ;

} catch (InstantiationException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (SecurityException e) {

e.printStackTrace() ;

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

参考文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值