猿问

当我调用不同的包时,如何在 Java 中返回多个对象类型(String、int、int)?

我试图将多个对象类型(String、int、int)传递到另一个 package1 中,并让 package1 返回(String、int、int)。但我想做到这一点,而不通过传递一个类将原始 package2 导入到 package1 中。

方法1:传递一个对象列表,并在调用者处将其转换回 String/int。(我读过传递对象是一种禁忌,应该传递类或仅传递一种特定类型)

//package1 - cannot import any other packages

public List<Object> returnvalue (String aString, int aInt, int bInt) {

  //Just an example of processed values

  aString = aString + "append"; 

  aInt++;

  bInt = bInt+2;


  List<Object> returnvalue = new ArrayList<>();


  returnvalue.add(aString);

  returnvalue.add(aInt);

  returnvalue.add(bInt);


  return returnvalue;

}


//package2 - can import package1 inside

int aInt = 1;

int bInt = 1;

List<Object> receivevalue = package1.returnvalue("aString",aInt,bInt);

String receivestring = (String)receivevalue.get(0);

String receiveint1 = (int)receivevalue.get(1);

String receiveint2 = (int)receivevalue.get(2);

方法2:传递一个字符串列表,并在方法内对其进行转换。(这样我就不会传递对象)


//package1 - cannot import any other packages

public List<String> returnvalue (String aString, String aInt, String bInt) {

  //Just an example of processed values

  aString = aString + "append";


  int tmpInt = Integer.valueOf(aInt);

  tmpInt++;

  aInt = String.valueOf(tmpInt);


  int tmpInt2 = Integer.valueOf(bInt);

  tmpInt2=tmpInt2-2;

  bInt = String.valueOf(tmpInt2);


  List<String> returnvalue = new ArrayList<>();


  returnvalue.add(aString);

  returnvalue.add(aInt);

  returnvalue.add(bInt);


  return returnvalue;

}


//package2 - can import package1 inside

int aInt = 1;

int bInt = 1;

List<String> receivevalue = package1.returnvalue("aString",aInt,bInt);

String receivestring = receivevalue.get(0);

String receiveint1 = receivevalue.get(1);

String receiveint2 = receivevalue.get(2);

从我读到的内容来看,大多数人似乎建议创建一个类并传递该类,这样它就具有类型安全性并且更具可读性。但是,如果不将 package2 调用到 package1 中,我相信这是不可能的?我想在不导入任何模型类的情况下执行此操作,package1 必须完全独立。


我想知道哪一个是最好的,是否有更好的方法来实现我想要实现的目标?


三国纷争
浏览 175回答 2
2回答

HUH函数

在 package1 中创建一个 POJO 类来保存您的输入/输出值,例如:class Data {&nbsp; &nbsp; public String s;&nbsp; &nbsp; public int a1;&nbsp; &nbsp; public int a1;}那么您可以更改以下签名:public List<Object> returnvalue (String aString, int aInt, int bInt)&nbsp;到public Data returnValue(Data aData)更新假设 package1 中有 2 个类Data.java:package package1;public class Data {&nbsp; &nbsp; public String s;&nbsp; &nbsp; public int a1;&nbsp; &nbsp; public int a2;}和Solution.java:package package1;public class Solution {&nbsp; &nbsp; public Data returnValue(Data input) {&nbsp; &nbsp; &nbsp; &nbsp; Data data = new Data();&nbsp; &nbsp; &nbsp; &nbsp; data.s = input.s + "append";&nbsp; &nbsp; &nbsp; &nbsp; data.a1 = input.a1 + 1;&nbsp; &nbsp; &nbsp; &nbsp; data.a2 = input.a2 + 2;&nbsp; &nbsp; &nbsp; &nbsp; return data;&nbsp; &nbsp; }}您可以在 packate2 中使用它们,如下App.java:package package2;import package1.Data;import package1.Solution;public class App {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Data input = new Data();&nbsp; &nbsp; &nbsp; &nbsp; input.s = "ipsum";&nbsp; &nbsp; &nbsp; &nbsp; input.a1 = 1;&nbsp; &nbsp; &nbsp; &nbsp; input.a2 = 2;&nbsp; &nbsp; &nbsp; &nbsp; Solution solution = new Solution();&nbsp; &nbsp; &nbsp; &nbsp; Data result = solution.returnValue(input);&nbsp; &nbsp; &nbsp; &nbsp; // use result&nbsp; &nbsp; }}

慕哥9229398

您可以定义一个java类,它具有所有三个(String,int,int)属性,然后您可以返回它的对象。希望能帮助到你。
随时随地看视频慕课网APP

相关分类

Java
我要回答