Java 方法到 return 多个值
Java method to return multiple values
我有一种情况想从一个方法中 return 2 个值。我试图在 Java 中弄清楚如何做到这一点。在 C# 中,在这种情况下,我只会使用 2 个输出参数或一个结构,但不确定对 Java 最好做什么(除了 Pair,因为我可能必须将其更改为 3 个值,或者必须创建一个新的class 到 return 对象)。
我的例子是这样的:
public void myMethod(Signal signal){
MyEnum enum = MyEnum.DEFAULT;
String country = "";
// based on signal, I need to get 2 values, one is string, other is
// an enumeration
if (signal.getAction() == "Toyota"){
enum = MyEnum.TOYOTA;
country = "Japan";
} else if (signal.getAction() == "Honda"){
enum = MyEnum.HONDA;
country = "Japan";
} else if (signal.getAction() == "VW"){
enum = MyEnum.VW;
country = "Germany";
} else {
enum = MyEnum.DEFAULT;
country = "Domestic";
}
// how to return both enum and country?
return ???
}
这只是一个解释我需要什么的例子(returning one-something,有 2 个值,一个是字符串,另一个是枚举)。所以,忽略我的字符串比较或逻辑的任何问题,我的观点是如何 return 一些东西。例如,在 C# 中,我可以定义一个结构和 return 该结构,或者我可以使用 out 参数来 return 2 个值。但我不确定如何在 Java.
中优雅地做到这一点
我认为这主要是 Jon Skeet 建议的一个例子。 (编辑以包括国家/地区)
使您的 Enum 带有文本和转换功能。
public enum AutoMake {
HONDA("Honda", "Japan"),
TOYOTA("Toyota", "Japan"),
VW("Volkswagon", "Germany");
private String country;
private String text;
private AutoMake(String text, String country) {
this.text = text;
}
public static AutoMake getMake(String str){
AutoMake make = null;
AutoMake[] possible = AutoMake.values();
for(AutoMake m : possible){
if(m.getText().equals(str)){
make = m;
break;
}
}
return make;
}
/**
* @return the country
*/
public String getCountry() {
return country;
}
/**
* @return the text
*/
public String getText() {
return text;
}
}
然后将 make 作为枚举存储在汽车对象中
public class Car {
private AutoMake make;
private String model;
public Car() {
}
public Car(AutoMake make, String model) {
super();
this.make = make;
this.model = model;
}
/**
* @return the make
*/
public AutoMake getMake() {
return make;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param make the make to set
*/
public void setMake(AutoMake make) {
this.make = make;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
}
现在您可以从汽车对象中获取文本和枚举值
car.getMake() // Enum
car.getMake.getText() // Text
car.getMake.getCountry // Country
您可以使用
从文本转换为枚举
Enum make = AutoMake.getMake("Honda");
这意味着 AutoMake.getMake(Signal.getAction())
可以用包含品牌和国家/地区的结果枚举替换 myMethod(signal)
。
如果您真的很想在 java 中使用元组,您可以从 scala 标准库中导入它们。由于两种语言都被编译为相同的字节码,因此它们可以在一个项目中一起使用。
import scala.Tuple2;
public class ScalaInJava {
public static Tuple2<String, Integer> tupleFunction(){
return new Tuple2<>("Hello World", 1);
}
public static void main(String[] args) {
System.out.println(tupleFunction()._1());
}
}
我有一种情况想从一个方法中 return 2 个值。我试图在 Java 中弄清楚如何做到这一点。在 C# 中,在这种情况下,我只会使用 2 个输出参数或一个结构,但不确定对 Java 最好做什么(除了 Pair,因为我可能必须将其更改为 3 个值,或者必须创建一个新的class 到 return 对象)。
我的例子是这样的:
public void myMethod(Signal signal){
MyEnum enum = MyEnum.DEFAULT;
String country = "";
// based on signal, I need to get 2 values, one is string, other is
// an enumeration
if (signal.getAction() == "Toyota"){
enum = MyEnum.TOYOTA;
country = "Japan";
} else if (signal.getAction() == "Honda"){
enum = MyEnum.HONDA;
country = "Japan";
} else if (signal.getAction() == "VW"){
enum = MyEnum.VW;
country = "Germany";
} else {
enum = MyEnum.DEFAULT;
country = "Domestic";
}
// how to return both enum and country?
return ???
}
这只是一个解释我需要什么的例子(returning one-something,有 2 个值,一个是字符串,另一个是枚举)。所以,忽略我的字符串比较或逻辑的任何问题,我的观点是如何 return 一些东西。例如,在 C# 中,我可以定义一个结构和 return 该结构,或者我可以使用 out 参数来 return 2 个值。但我不确定如何在 Java.
中优雅地做到这一点我认为这主要是 Jon Skeet 建议的一个例子。 (编辑以包括国家/地区)
使您的 Enum 带有文本和转换功能。
public enum AutoMake {
HONDA("Honda", "Japan"),
TOYOTA("Toyota", "Japan"),
VW("Volkswagon", "Germany");
private String country;
private String text;
private AutoMake(String text, String country) {
this.text = text;
}
public static AutoMake getMake(String str){
AutoMake make = null;
AutoMake[] possible = AutoMake.values();
for(AutoMake m : possible){
if(m.getText().equals(str)){
make = m;
break;
}
}
return make;
}
/**
* @return the country
*/
public String getCountry() {
return country;
}
/**
* @return the text
*/
public String getText() {
return text;
}
}
然后将 make 作为枚举存储在汽车对象中
public class Car {
private AutoMake make;
private String model;
public Car() {
}
public Car(AutoMake make, String model) {
super();
this.make = make;
this.model = model;
}
/**
* @return the make
*/
public AutoMake getMake() {
return make;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param make the make to set
*/
public void setMake(AutoMake make) {
this.make = make;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
}
现在您可以从汽车对象中获取文本和枚举值
car.getMake() // Enum
car.getMake.getText() // Text
car.getMake.getCountry // Country
您可以使用
从文本转换为枚举Enum make = AutoMake.getMake("Honda");
这意味着 AutoMake.getMake(Signal.getAction())
可以用包含品牌和国家/地区的结果枚举替换 myMethod(signal)
。
如果您真的很想在 java 中使用元组,您可以从 scala 标准库中导入它们。由于两种语言都被编译为相同的字节码,因此它们可以在一个项目中一起使用。
import scala.Tuple2;
public class ScalaInJava {
public static Tuple2<String, Integer> tupleFunction(){
return new Tuple2<>("Hello World", 1);
}
public static void main(String[] args) {
System.out.println(tupleFunction()._1());
}
}