Java 如何从一个方法返回不同数据类型的多个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18248556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can return multiple values of different data types from a method?
提问by Anna89
I have following method,
我有以下方法,
public Response process(Applicant appl)
{
String responseString;
String requestString;
requestString = createRequestString(appl);
responseString = sendRequest(requestString);
Response response = parseResponse(responseString);
return response;
}
Here I want to return both responseString and response, one is of type String and other is an object of class Response. How can I do this?
这里我想同时返回 responseString 和 response,一个是 String 类型,另一个是 Response 类的对象。我怎样才能做到这一点?
采纳答案by Suresh Atta
No you cannot.
你不能。
You can only return the type you mentioned in method signature.
您只能返回您在方法签名中提到的类型。
What you can do is,
你能做的是,
Create a field in Response
class called responseString
and add setters
and getters
.
在Response
名为的类中创建一个字段responseString
并添加setters
和getters
。
public Response process(Applicant appl)
{
String responseString;
String requestString;
requestString = createRequestString(appl);
responseString = sendRequest(requestString);
Response response = parseResponse(responseString);
response.setResponseString(responseString);
return response;
}
回答by Konstantin Yovkov
You can create a custom type, which holds both of the values.
您可以创建一个自定义类型,它包含这两个值。
public class ResponseObject {
private String responseString;
private Response response;
//accessors
}
and then return an instance of that class as a result.
然后作为结果返回该类的一个实例。
回答by rocketboy
If both the objects are separate entities, the prefered way to do this is via composition:
如果两个对象都是单独的实体,则首选的方法是通过组合:
String responseString;
String requestString;
requestString = createRequestString(appl);
responseString = sendRequest(requestString);
Response response = parseResponse(responseString);
return new compositObj(response, responseString);
class compositObj{
Response res;
String resString;
compositObj(Response r, String s){
res = r;
resString = s;
}
//getters setters if needed
}
回答by allprog
You need to create a wrapper class for that. Unlike some other languages the Java language doesn't support tuple return values with automatic unpacking. Simply create a data class like this:
您需要为此创建一个包装类。与其他一些语言不同,Java 语言不支持自动解包的元组返回值。只需创建一个像这样的数据类:
class ResponseData {
public final String responseString;
public final Response response;
public ResponseData(String responseString, Response response) {
this.responseString = responseString;
this.response = response;
}
}
回答by Ahsan Shah
you can return array of objects as follows:
您可以按如下方式返回对象数组:
public Object[] process(Applicant appl)
{
...
but you need to keep track of index of the inserted objects (String, Response)
但您需要跟踪插入对象的索引(字符串、响应)
Another way, you can return Map<String, Object>
having keys representing the values.
另一种方式,您可以返回Map<String, Object>
具有代表值的键。
回答by erencan
Java methods are determined by its signatures. There can only be existed one unique signature in a scope.
Java 方法由其签名确定。一个范围内只能存在一个唯一的签名。
Method Signature: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
方法签名:方法声明的两个组成部分包括方法签名——方法的名称和参数类型。
As suggested in the description return types are not in signature, so it is impossible to have two methods that differs only by return types.
正如描述中所建议的,返回类型不在签名中,因此不可能有两种仅因返回类型而不同的方法。
However, there is several ways to return different object types.
但是,有多种方法可以返回不同的对象类型。
Object type
对象类型
Return Object
type since all classes are type of Object
, then cast it to proper type.
返回Object
类型,因为所有类都是 类型Object
,然后将其转换为正确的类型。
public Object process(Applicant appl);
Inheritance
遗产
Define a base class and deriveResponse
and ResponseString
from base
定义一个基类Response
并ResponseString
从基类派生和派生
class ResponseBase
{
}
and derive your classes
并派生你的课程
class Response extends ResponseBase{...}
class ResponseString ResponseBase{...}
and return base class from your method
并从您的方法返回基类
public ResponseBase process(Applicant appl);
公共响应库进程(申请人申请);
Composition
作品
Wrap your Response
and ResponseString
to a wrapper class and return it.
将您的Response
andResponseString
包装到一个包装类并返回它。
class ResponseWrapper
{
Response response;
ResponseString responseString;
//getter setters
}
and return ResponseWrapper
from your method
并ResponseWrapper
从你的方法返回
public ResponseWrapper process(Applicant appl);
See also
也可以看看
回答by MAK
option 1. you can return your CustomeResponse.
选项 1. 您可以返回您的 CustomeResponse。
public class CustomResponse {
private String responseString;
private Response response;
public void setResponseString(String responseString){
this.responseString = responseString;
}
public String getResponseString(){
return this.responseString;
}
public void setResponse(Response response){
this.response = response;
}
public Response getResponse(){
return this.response;
}
}
option 2. you can pass responseString as parameter. as non primitive datatypes passed as argument has the reference of the variable so when you will set responseString, your variable value will be updated.
选项 2. 您可以将 responseString 作为参数传递。由于作为参数传递的非原始数据类型具有变量的引用,因此当您设置 responseString 时,您的变量值将被更新。
public Response process(Applicant appl,String responseString)
{
String requestString;
requestString = createRequestString(appl);
responseString = sendRequest(requestString);
Response response = parseResponse(responseString);
return response;
}