当前位置:网站首页><T>泛型方法演示
<T>泛型方法演示
2022-07-25 09:17:00 【zk_Kang】
自己用到的方法,记录一下。(代码纯手敲的,有错误地方,请见谅)
方法:给对象中的属性赋值
//设置对象属性值,返回对象
public <T> T saveValue(T t,String name,String value){
// Class<? extends Object> c = t.getClass();
//得到属性--具体某一个
Field field = t.getClass().getDeclaredField(name);
/*
也可以得到所有属性,for循环
Field [] fields = t.getClass().getDeclaredFields();
for...//省略
*/
//这个属性必须设置为true,如果对象属性为private私有。则必须设置,否则取不到值
field.setAccessible(true);
//将t中的Field的值设置为value
field.set(t,value);
return t;
}方法:得到泛型属性中的某一个属性值
//得到对象属性值,返回属性值
public <T> Object saveValue(T t,String name){
//得到属性--具体某一个
Field field = t.getClass().getDeclaredField(name);
//这个属性必须设置为true,如果对象属性为private私有。则必须设置,否则取不到值
field.setAccessible(true);
return field.get(t);
}方法:迭代器
//自己日记,--jsonarray是list对象,it可以这样迭代
for(Iterator it = JsonArray.iterator();it.hasNext();){
String jj = (String)it.next();
}方法:json转对象
//user为对象,obj为JSONObject
User user = new ObjectMapper().readValue(obj.toString(),User.class);
边栏推荐
- JDBC的API解析
- redis操作利用游标代替keys
- Probe into Druid query timeout configuration → who is the querytimeout of datasource and jdbctemplate effective?
- 『每日一问』LockSupport怎么实现线程等待、唤醒
- Activemq-- delayed delivery and scheduled delivery
- [learn rust together] a preliminary understanding of rust package management tool cargo
- Redis-哨兵,主从部署详细篇
- Shell脚本
- Troubleshooting error: NPM install emojis list failed
- CentOS changes MySQL database directory
猜你喜欢

API健康状态自检
![[arm] Xintang nuc977 transplants wk2124 drive](/img/0c/fa21d7a44e0263dde4d3135a78818f.png)
[arm] Xintang nuc977 transplants wk2124 drive

ActiveMQ -- dead letter queue

Do you know these methods of MySQL database optimization?

Opencv realizes simple face tracking

【Nacos】NacosClient在服务注册时做了什么

分享一个避免递归的部门设计方法

The garbage classification data set used in the excellent Yolo target detection training is shared - about 3000 labeled

idea实用tips---如今将pom.xml(红色)改为pom.xml(蓝色)
![[C language] dynamic memory management, flexible array](/img/da/b9455885df0cb6646908e3655d62c5.png)
[C language] dynamic memory management, flexible array
随机推荐
有误差的字符串型时间比较方法String.compareTo
[STL]list模拟实现
Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in
How to write the code of wechat applet implementation tab
Redis operation uses cursor instead of keys
Leetcode组合总和+剪枝
28. Slot
activemq--可持久化机制之AMQ
centos更改mysql数据库目录
sql注入
Detailed explanation of pipeline pipeline mechanism in redis
360 degree drag panorama plug-in tpanorama.js
How does Youxuan database encrypt data?
PHP date() function does not support processing numbers greater than 2147483648? "Suggested collection"
activemq--可持久化机制
『每日一问』LockSupport怎么实现线程等待、唤醒
office文件对应的Content-Type类型
黑马程序员JDBC
Comparison between symmetric encryption and asymmetric encryption
JMeter test plan cannot be saved solution