《Java工程师必读手册》——Java编程技巧系列——Java编程技巧之输入输出参数(6)

简介: 《Java工程师必读手册》——Java编程技巧系列——Java编程技巧之输入输出参数(6)

接上篇:https://developer.aliyun.com/article/1228181?spm=a2c6h.13148508.setting.20.358c4f0eIHLsiZ


四、 利用方法返回值实现

 

本章将从方法返回值入手,实现参数的输入输出功能。

 

1. 利用结果类实现

 

理论依据

 

引入返回值对象(Introduce Return Object):当一个方法的需要返回多个值时,就可以考虑将返回值封装成一个对象类。将返回值封装成对象类后,提高了代码的可读性,并且该返回值对象类也可以重用。以后,如果增加或删除返回值,方法本身不需要修改,只需要修改返回值对象类就可以了。

 

这里,可以利用引入返回值对象重构方法,定义一个返回值对象类,来实现参数的输入输出功能。

 

 

 

代码实现

 

/**

* 几何辅助类

*/

public final class GeometryHelper {

 

   /** 原有静态常量 */

   ......

 

   /**

    * 划分线串

    *

    * @param lineString 原始线串

    * @param segmentLengthes 分段长度数组

    * @return 线串数组

    */

   public static LineString[] splitLineString(LineString lineString, double[] segmentLengthes) {

       // 原有计算逻辑

       ......

 

       // 初始化参数值

       int index = 1;

       Coordinate[] coordinates = lineString.getCoordinates();

       Coordinate coordinate = coordinates[0];

       int length = targetLengthes.length;

       LineString[] lineStrings = new LineString[length];

 

       // 添加前面N段

       for (int i = 0; i < length - 1; i++) {

           ReturnResult result = combineLineString(coordinates, index, coordinate, targetLengthes[i]);

           index = result.getIndex();

           coordinate = result.getCoordinate();

           lineStrings[i] = result.getLineString();

       }

 

       // 添加最后一段

       lineStrings[length - 1] = buildLineString(coordinates, index, coordinate);

 

       // 返回线串数组

       return lineStrings;

   }

 

   /**

    * 组装线串

    *

    * @param coordinates 坐标数组

    * @param index 当前序号

    * @param coordinate 当前坐标

    * @param targetLength 目标长度

    * @return 返回值

    */

   private static ReturnResult combineLineString(Coordinate[] coordinates, int index, Coordinate coordinate, long targetLength) {

       // 添加线串坐标

       ......

 

       // 返回输出结果

       return new ReturnResult(index, coordinate, buildLineString(coordinateList));

   }

 

   /** 原有静态方法 */

   ......

 

   /**

    * 返回值类

    */

   @Getter

   @Setter

   @ToString

   @NoArgsConstructor

   @AllArgsConstructor

   private static class ReturnResult {

       /** 当前序号 */

       private int index;

       /** 当前坐标 */

       private Coordinate coordinate;

       /** 线串对象 */

       private LineString lineString;

   }

 

}

 

 

 

2. 利用元组类实现

 

理论依据

 

参考3.3章节的元组(Tuple)的定义和特性。元组(Tuple)可以用于方法的参数值,也可以用于方法的返回值。当一个方法需要返回多个值时,又不愿意定义自己的结果类时,可以采用元组(Tuple)实现多个值的返回。

 

代码实现

 

/**

* 几何辅助类

*/

public final class GeometryHelper {

 

   /** 原有静态常量 */

   ......

 

   /**

    * 划分线串

    *

    * @param lineString 原始线串

    * @param segmentLengthes 分段长度数组

    * @return 线串数组

    */

   public static LineString[] splitLineString(LineString lineString, double[] segmentLengthes) {

       // 原有计算逻辑

       ......

 

       // 初始化参数值

       int index = 1;

       Coordinate[] coordinates = lineString.getCoordinates();

       Coordinate coordinate = coordinates[0];

       int length = targetLengthes.length;

       LineString[] lineStrings = new LineString[length];

 

       // 添加前面N段

       for (int i = 0; i < length - 1; i++) {

           Triple triple = combineLineString(coordinates, index, coordinate, targetLengthes[i]);

           index = triple.getLeft();

           coordinate = triple.getMiddle();

           lineStrings[i] = triple.getRight();

       }

 

       // 添加最后一段

       lineStrings[length - 1] = buildLineString(coordinates, index, coordinate);

 

       // 返回线串数组

       return lineStrings;

   }

 

   /**

    * 组装线串

    *

    * @param coordinates 坐标数组

    * @param index 当前序号

    * @param coordinate 当前坐标

    * @param targetLength 目标长度

    * @return 返回值

    */

   private static Triple combineLineString(Coordinate[] coordinates, int index, Coordinate coordinate, long targetLength) {

       // 添加线串坐标

       ......

 

       // 返回输出结果

       return ImmutableTriple.of(index, coordinate, buildLineString(coordinateList));

   }

 

   /** 原有静态方法 */

   ......

 

}


接下篇:https://developer.aliyun.com/article/1228177?groupCode=java

相关文章
|
缓存 NoSQL Java
Spring Cache简化缓存开发
Spring Cache简化缓存开发
454 0
|
9天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
3905 19
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
11天前
|
人工智能 自然语言处理 供应链
|
17天前
|
人工智能 开发工具 iOS开发
Claude Code 新手完全上手指南:安装、国产模型配置与常用命令全解
Claude Code 是一款运行在终端环境中的 AI 编程助手,能够直接在命令行中完成代码生成、项目分析、文件修改、命令执行、Git 管理等开发全流程工作。它最大的特点是**任务驱动、终端原生、轻量高效、多模型兼容**,无需图形界面、不依赖 IDE 插件,能够深度融入开发者日常工作流。
3631 14
|
13天前
|
人工智能 Linux BI
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek
JeecgBoot AI专题研究 一键脚本:Claude Code + JeecgBoot Skills + DeepSeek 全平台接入 一行命令装好 Claude Code + JeecgBoot Skills + DeepSeek 接入,无需翻墙使用 Claude Code,支持 Wind
3067 7
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek
|
20天前
|
Shell API 开发工具
Claude Code 快速上手指南(新手友好版)
AI编程工具卷疯啦!Claude Code凭借任务驱动+终端原生的特性,成了开发者的效率搭子。本文从安装、登录、切换国产模型到常用命令,手把手带新手快速上手,全程避坑,30分钟独立用起来。
3769 25
|
4天前
|
存储 定位技术 数据库
CodeGraph 如何让 Claude Code减少 7 成工具调用?
CodeGraph 为 Coding Agent 提供本地代码知识图谱,把函数、类、调用链和框架路由提前整理成“项目地图”,减少盲目搜索和文件读取。它不是新 Agent,而是上下文基础设施,让 Agent 更快找到正确代码路径,平均减少 7 成工具调用。
537 0