[英]How to multiple different types of values from a method in java and use it in the main class?
I have made a method which I will use several times in my main, but change the paremeters each time. 我已经制定了一种方法,该方法将在我的主体中多次使用,但是每次都要更改参数。 I cannot figure out how to call each different values from that method. 我无法弄清楚如何从该方法调用每个不同的值。 below is what I have so far for my method, it contains type Strings, int and JLABEL. 下面是到目前为止我的方法,它包含Strings,int和JLABEL类型。
private static void method(String a, int b, int c, int d, String e, JLabel f) {
try {
a = (String) field1.getText();
b = (int) (Double.parseDouble(studentid.getText());
c = (studentname.getText()));
d = (Integer.parseInt(hourtravel.getText()));
countCommandsAdded.setText("e");
contentPane.remove(f);
}catch (NumberFormatException arg0){
}
}
How can I return, in this case I need to return String a, int b, int c, int d, and remove JLabel f from my design and change text of String e. 如何返回,在这种情况下,我需要返回String a,int b,int c,int d,并从设计中删除JLabel f并更改String e的文本。
Thanks. 谢谢。
You can have methods with the same name in the same class, with different parameters, and different return types. 您可以在同一类中使用具有相同名称,不同参数和不同返回类型的方法。 This is called Overloading and it is better explained here http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html . 这称为“ 重载” ,请在http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html中更好地进行说明。
In your case, though it is not the most brilliant solution, you can have multiple method(...)
with the parameters you want, performing different logic. 在您的情况下,尽管这不是最出色的解决方案,但是您可以使用具有所需参数的多种method(...)
,执行不同的逻辑。
As other poster suggest, a better, more OOP compliant solution would be to use generics , or to use a class as wrapper for your different values. 正如其他发布者所建议的那样,更好的,更符合OOP的解决方案将是使用泛型或将类用作不同值的包装。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.