当前位置:网站首页>< T> Generic method demonstration
< T> Generic method demonstration
2022-07-25 09:24:00 【zk_ Kang】
The method you use , Make a note of .( The code is typed by hand , There are mistakes , Please forgive me )
Method : Assign values to properties in the object
// Set object property values , Returns the object
public <T> T saveValue(T t,String name,String value){
// Class<? extends Object> c = t.getClass();
// Get the property -- Specific one
Field field = t.getClass().getDeclaredField(name);
/*
You can also get all attributes ,for loop
Field [] fields = t.getClass().getDeclaredFields();
for...// Omit
*/
// This property must be set to true, If the object property is private private . Must be set , Otherwise, we can't get the value
field.setAccessible(true);
// take t Medium Field Is set to value
field.set(t,value);
return t;
}Method : Get the value of one of the generic attributes
// Get the object attribute value , Return property value
public <T> Object saveValue(T t,String name){
// Get the property -- Specific one
Field field = t.getClass().getDeclaredField(name);
// This property must be set to true, If the object property is private private . Must be set , Otherwise, we can't get the value
field.setAccessible(true);
return field.get(t);
}Method : iterator
// Own diary ,--jsonarray yes list object ,it You can iterate like this
for(Iterator it = JsonArray.iterator();it.hasNext();){
String jj = (String)it.next();
}Method :json Transfer object
//user As object ,obj by JSONObject
User user = new ObjectMapper().readValue(obj.toString(),User.class);
边栏推荐
- 一文搞懂try、catch、finally(包含return)执行流程(全网最详细解析)
- 什么是贫血模型和充血模型?
- ActiveMQ -- JDBC Journal of persistent mechanism
- 抽象类和接口的区别(最详细)
- Bi business interview with data center and business intelligence (I): preparation for Industry and business research
- 『每日一问』ReentrantLock加锁解锁
- Shell脚本
- 『每日一问』简单聊聊JMM/说说对JMM的了解
- jsPDF生成PDF文件,文件不全问题,后台进行文件下载,前台不下载
- Thymeleaf 笔记
猜你喜欢

DVWA练习一 暴力破解
![[C language] dynamic memory management, flexible array](/img/da/b9455885df0cb6646908e3655d62c5.png)
[C language] dynamic memory management, flexible array
![[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box](/img/5c/52ccc0583451eba15fec18adb1499e.png)
[deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box

保姆级Scanner类使用详解

ActiveMQ -- message retry mechanism
[learn rust together] a preliminary understanding of rust package management tool cargo
![[stl]list Simulation Implementation](/img/92/2a78382700c1ebf299c6505d962c9c.png)
[stl]list Simulation Implementation

Do you know these methods of MySQL database optimization?

How can technologists start their personal brand? Exclusive teaching of top five KOLs

Disable module (attribute node) in LabVIEW
随机推荐
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
C#语言和SQL Server数据库技术
『怎么用』装饰者模式
Write two channel (stereo) immediately Wav file
ActiveMQ -- JDBC code of persistent mechanism
Uniapp intercepts route jumps through addinterceptor to control whether the page needs to log in
[machine learning] Finally, the important steps of machine learning modeling have been clarified
Sort out Huawei ap-3010dn_ V2 configuration create WiFi
有误差的字符串型时间比较方法String.compareTo
How can technologists start their personal brand? Exclusive teaching of top five KOLs
Programmers can't SQL? Ashes Engineer: all waiting to be eliminated! This is a must skill!
Composition of the interview must ask items
Detailed explanation of pipeline pipeline mechanism in redis
Activemq-- asynchronous delivery
sqli-labs Basic Challenges Less11-22
动态添加多tab,并初始化每个tab页面
『每日一问』volatile干嘛的
将list集合的某一字段拼接单个String
Common tool classes under JUC package
Silicon Valley classroom lesson 15 - Tencent cloud deployment