javase-反射字段学习记录
来源:互联网 发布:linux修改字符集为gbk 编辑:程序博客网 时间:2024/05/16 21:57
public class Demo5 {
// public String name="张三";
@Test
public void test1() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Class clazz=Class.forName("cn.reflect.Person");
Person person=(Person) clazz.newInstance();
Field f= clazz.getField("name");
Object value=f.get(person);
Class type =f.getType();
if (type.equals(String.class)) {
String svalue = (String) value;
System.out.println(svalue);
}
//设置值
f.set(person, "xxxxxxxxxxxxxx");
System.out.println(person.name);
}
//private int password;
@Test
public void test2() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Class clazz=Class.forName("cn.reflect.Person");
Person person = new Person();
Field f =clazz.getDeclaredField("password");
f.setAccessible(true);
System.out.println(f.get(person));
f.set(person, 423);
System.out.println(f.get(person));
}
// private static int age;
@Test
public void test3() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Class clazz=Class.forName("cn.reflect.Person");
Person person = new Person();
Field f =clazz.getDeclaredField("age");
f.setAccessible(true);
System.out.println(f.get(null));
f.set(person, 423);
System.out.println(f.get(null));
}
}
// public String name="张三";
@Test
public void test1() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Class clazz=Class.forName("cn.reflect.Person");
Person person=(Person) clazz.newInstance();
Field f= clazz.getField("name");
Object value=f.get(person);
Class type =f.getType();
if (type.equals(String.class)) {
String svalue = (String) value;
System.out.println(svalue);
}
//设置值
f.set(person, "xxxxxxxxxxxxxx");
System.out.println(person.name);
}
//private int password;
@Test
public void test2() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Class clazz=Class.forName("cn.reflect.Person");
Person person = new Person();
Field f =clazz.getDeclaredField("password");
f.setAccessible(true);
System.out.println(f.get(person));
f.set(person, 423);
System.out.println(f.get(person));
}
// private static int age;
@Test
public void test3() throws ClassNotFoundException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException{
Class clazz=Class.forName("cn.reflect.Person");
Person person = new Person();
Field f =clazz.getDeclaredField("age");
f.setAccessible(true);
System.out.println(f.get(null));
f.set(person, 423);
System.out.println(f.get(null));
}
}
0 0
- javase-反射字段学习记录
- javase-反射方法学习记录
- javase-反射构造函数学习记录
- JavaSE学习笔记--反射
- JavaSE学习--反射
- javaSE学习笔记之反射
- JavaSE学习总结-反射Reflect
- 一 javase学习记录01
- 一 javase学习记录06
- 一 javase学习记录07
- 一 javase学习记录08
- 一 javase学习记录09
- 一 javase学习记录10
- 一 javase学习记录11
- 一 javase学习记录12
- JavaSE 学习参考:反射机制(1)
- javaSE-反射
- [JavaSE]反射
- Socket编程服务端可能出现阻塞的位置,以及解决方法
- git使用入门以及一些问题
- 数组---(一维、二维数组,求最值,排序)
- eclipse导入hadoop示例源码on ubuntu14.04
- 常用适配器SimpleAdapter
- javase-反射字段学习记录
- Arrays工具类
- myeclipse 10 在mac中字体模糊问题解决方案
- TP-基础
- 大多数RPM包安装的文件位置
- epoll实现原理 赏析
- anroid4.0调用系统相机拍摄完照片点确定系统没反应,点取消崩溃bug修复
- centos7使用无线wifi连接
- java的poi技术读取和导入Excel