java.lang.reflect操作对象属性(域)的值
package reflect; import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; /*2015-10-28*/
public class RefactorDemo { /**
* @param args
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
String name = "Demo";
double salary = 20000.0;
String nameFieldName = "name";
String salaryFieldName = "salary"; Constructor<?>[] constructors = Person.class.getConstructors();
for (Constructor<?> constructor : constructors) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
System.out.println(parameterType.getName());
}
} System.out.println("====================================");
//constructor
// 这个地方需要关注:如果是基本类型,就是double.class,不是Double.class
Constructor<Person> constructor = Person.class.getConstructor(String.class, double.class);
Person tom = constructor.newInstance("Tom", 3000000.0);
System.out.println(tom); // get private Field
Person person = new Person(name, salary);
Field field = person.getClass().getDeclaredField(nameFieldName);
field.setAccessible(true);// 访问控制
System.out.println(field.get(person)); person = new Person(name, salary);
field = person.getClass().getDeclaredField(salaryFieldName);
field.setAccessible(true);
System.out.println(field.getDouble(person)); // set private Field
person = new Person();
field = person.getClass().getDeclaredField(nameFieldName);
field.setAccessible(true);
field.set(person, name);
field = person.getClass().getDeclaredField(salaryFieldName);
field.setAccessible(true);
field.setDouble(person, salary);
System.out.println(person); // process method
person = new Person();
field = person.getClass().getDeclaredField(salaryFieldName);
field.setAccessible(true);
field.setDouble(person, 100000.9);
Method method = person.getClass().getDeclaredMethod("doubleSalary");
method.invoke(person);
System.out.println(field.getDouble(person));
}
} class Person {
private String name;
private double salary; public Person() {
super();
} public Person(String name, double salary) {
super();
this.name = name;
this.salary = salary;
} public void doubleSalary() {
this.salary = this.salary * 2;
} @Override
public String toString() {
return "Person [name=" + name + ", salary=" + salary + "]";
} }
Output:
java.lang.String
double
====================================
Person [name=Tom, salary=3000000.0]
Demo
20000.0
Person [name=Demo, salary=20000.0]
200001.8
java.lang.reflect操作对象属性(域)的值的更多相关文章
- java 编程基础 Class对象 反射 :数组操作java.lang.reflect.Array类
java.lang.reflect包下还提供了Array类 java.lang.reflect包下还提供了Array类,Array对象可以代表所有的数组.程序可以通过使 Array 来动态地创建数组, ...
- 利用java.lang.reflect.Constructor动态实例化对象
} } } } } Student t = co ...
- java.lang.reflect.Method.getAnnotation()方法示例【通过反射获取到方法对象再获取方法对象上的注解信息】
转: java.lang.reflect.Method.getAnnotation()方法示例 java.lang.reflect.Method.getAnnotation(Class <T&g ...
- java 编程基础 Class对象 反射:动态代理 和AOP:java.lang.reflect.Proxy:(Proxy.newProxyInstance(newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h))
为什么我们使用动态代理 静态代理会让类变多了,多了代理类,工作量变大了,且不易扩展.比如我们上节课的例子,要实现不同的扩展方法就要编写不同的代理类,非常麻烦. Proxy类的使用规则 Proxy提 ...
- Java反射API研究(2)——java.lang.reflect详细内容与关系
对于最新的java1.8而言,reflect中接口的结构是这样的: java.lang.reflect.AnnotatedElement java.lang.reflect.AnnotatedType ...
- java.lang.reflect.Field
java.lang.reflect.Field 一.Field类是什么 Field是一个类,位于java.lang.reflect包下. 在Java反射中 Field类描述的是 类的属性信息,通俗来讲 ...
- JAVA反射系列之Field,java.lang.reflect.Field使用获取方法
JAVA反射系列之Field,java.lang.reflect.Field使用获取方法. 转载https://my.oschina.net/u/1407116/blog/209383 摘要 ja ...
- 本地tomcat调用远程接口报错:java.lang.reflect.InvocationTargetException
今天碰到一个奇怪的问题,本地Eclipse起了一个tomcat通过http去调一个外部接口,结果竟然报了一个反射的异常,先看下完整日志: , :: 下午 org.apache.catalina.sta ...
- java.lang.reflect.Method
java.lang.reflect.Method 一.Method类是什么 Method是一个类,位于java.lang.reflect包下. 在Java反射中 Method类描述的是 类的方法信息, ...
随机推荐
- ibatis实战之OR映射
相对Hibernate等ORM实现而言,ibatis的映射配置更为简洁直接,以下是一个典型的配置文件. <?xml version="1.0" encoding=" ...
- EF 主键自增、级联删除
一.主键自增 1.设置数据库中,主键自增 2.设置VS中Model1.edmx
- 实现app上对csdn的文章列表上拉刷新下拉加载以及加入缓存文章列表的功能 (制作csdn app 四)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/23698511 今天继续对我们的csdn客户端未完成的功能进行实现,本篇博客接着客 ...
- tolower (Function)
this is a function that Convert uppercase letter to lowercase Converts c to its lowercase equivalent ...
- textarea文本字段的宽度和高度(width、height)自己主动适应不断变化的处理
来源:http://www.cnblogs.com/jice/archive/2011/08/07/2130069.html <HTML> <HEAD> <TITLE&g ...
- 【Linux探索之旅】第二部分第四课:文件操纵,鼓掌之中
内容简介 1.第二部分第四课:文件操纵,鼓掌之中 2.第二部分第五课预告:用户和权限 文件操纵,鼓掌之中 既然上一课我们学习了Linux中的文件组织方式,那么现在就该是玩弄,啊不,是操纵它们的时候了. ...
- UVA - 12338 Anti-Rhyme Pairs (哈希)
Description D Anti-Rhyme Pairs Input: Standard Input Output: Standard Output Often two words that rh ...
- 1.cocos2dx 3.2环境结构
1 所需软件 jdk-7u25-windows-i586.exe python-2.7.8.amd64.msi cocos2d-x-3.2.zip apache-ant-1.9.4.zi ...
- IIS使用 URL Rewrite Module 2.0组件 设置伪静态的方法
简体中文版WIn10无法安装,需要改注册表, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp MajorVersion 项,这个也是 dword 值 10 ...
- 【软件使用技巧】PL/SQL Developer实现双击table询
二手plsql都知道,在表名默认双击[开展/关闭]. 习惯了MySql Workbench要么Sqlserver Management Studio无法适应其他管理工具. 直接在溶液: Tools - ...