In many languages, the parameters of functions are divided into input (in), output (out) and input and output (inout). In the C/C language, object references (&) can be used to implement the output (out) and input/output (inout) of function parameters. However, in the Java language, although there is no similar function for object references, the output (out) and input and output (inout) of function parameters can be realized by modifying the field values of the parameters. Here, we call the data structure corresponding to this output parameter the Holder (support) class.
Holder class implementation code:
/** 长整型支撑类 */@Getter@Setter@ToStringpublic class LongHolder { /** 长整型取值 */ private long value; /** 构造函数 */ public LongHolder() {} /** 构造函数 */ public LongHolder(long value) { this.value = value; } }
Holder class use case:
/** 静态常量 *//** 页面数量 */private static final int PAGE_COUNT = 100;/** 最大数量 */private static final int MAX_COUNT = 1000;/** 处理过期订单 */public void handleExpiredOrder() { LongHolder minIdHolder = new LongHolder(0L); for (int pageIndex = 0; pageIndex < PAGE_COUNT; pageIndex++) { if (!handleExpiredOrder(pageIndex, minIdHolder)) { break; } } }/** 处理过期订单 */private boolean handleExpiredOrder(int pageIndex, LongHolder minIdHolder) { // 获取最小标识 Long minId = minIdHolder.getValue(); // 查询过期订单(按id从小到大排序) List<OrderDO> orderList = orderDAO.queryExpired(minId, MAX_COUNT); if (CollectionUtils.isEmpty(taskTagList)) { return false; } // 设置最小标识 int orderSize = orderList.size(); minId = orderList.get(orderSize - 1).getId(); minIdHolder.setValue(minId); // 依次处理订单 for (OrderDO order : orderList) { ... } // 判断还有订单 return orderSize >= PAGE_SIZE; }
In fact, a generic support class can be implemented, which is suitable for more data types.
The above is the detailed content of How does Java define the output of Holder class implementation parameters?. For more information, please follow other related articles on the PHP Chinese website!