简体   繁体   English

java-如何将多个整数/字符串值作为单个对象传递给函数-并从该对象重建值

[英]java- how to pass multiple integer/string values as single object to function-and reconstruct values from the object

In a java app, I have to first pass multiple integer/string values packaged as a single object parameter to a target function, and then the target function has to correctly re-construct the integer/string values from the object that has been passed to it as an input parameter. 在Java应用程序中,我必须首先将打包为单个对象参数的多个整数/字符串值传递给目标函数,然后目标函数必须从已传递给对象的对象中正确地重建整数/字符串值。它作为输入参数。

What is the best/recommended way for doing this? 最佳/推荐方式是什么?

The target function will be invoked from another java function only(in a separate program)... 仅从另一个Java函数(在单独的程序中)调用目标函数...

Create a new class for packing all the parameters. 创建一个用于包装所有参数的新类。 In this case, the class acts as a transfer object for the parameters. 在这种情况下,该类充当参数的传递对象。 For example, instead of doing this: 例如,不要这样做:

void myMethod(int param1, String param2, boolean param3)

Do this: 做这个:

void myMethod(ParamTO param)

And define a transfer object like this: 并定义一个传输对象,如下所示:

public class ParamTO {
    private int param1;
    private String param2;
    private boolean param3;
    // get/set methods for the attributes
}

The way to "correctly re-construct the integer/string values from the object" is by simply calling the get/set methods corresponding to each attribute. “正确地从对象重构整数/字符串值”的方法是通过简单地调用与每个属性相对应的get / set方法。 If you don't know beforehand the attributes that need to be retrieved, you can use reflection for obtaining the attribute's values. 如果您事先不知道需要检索的属性,则可以使用反射来获取属性的值。

Alternatively, you could pass a Map with the parameters, where the key is the parameter's name and the value the parameter's value. 或者,您可以传递带有参数的Map ,其中键是参数的名称,值是参数的值。 Of course, you'll have to cast the parameters to the appropriate type, or use reflection: 当然,您必须将参数转换为适当的类型,或使用反射:

void myMethod(Map<String, Object> params)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:从哈希表获取对象值 <Integer,Hashtable<String,Object> &gt; - Java: get Object values from Hashtable<Integer,Hashtable<String,Object>> 如何使用按位运算符将多个Integer值传递给Java函数? - How to use a bitwise operator to pass multiple Integer values into a function for Java? 如何从 Java 的单行输入中读取多个 Integer 值? - How to read multiple Integer values from a single line of input in Java? 如何使用 BufferedReader 对象从 Java 中的一行读取多个整数值? - How to read multiple integer values from one line in Java using BufferedReader object? Java-调用对象数组的整数部分 - Java- Calling the integer part of an object array 如何使用单个流操作从对象中获取多个值? - How to get multiple values from an object using a single stream operation? 将值从不同的函数传递到单个对象 - passing values from different function to a single object 如何使用Java根据json字符串值将单个json对象分为两个json对象? - How to separate single json object into two json object based on json string values using java? 如何使用 mapstruct 将 map 值从 protobuf object 到 java 字符串 - How to map values from protobuf object to java String with mapstruct 如何从 java 中的 json object 读取多个值 - How to read multiple values from the json object in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM