简体   繁体   English

如何从Java中的方法中选择多个不同类型的值并在主类中使用它?

[英]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.

相关问题 如何在java的main方法中声明一个带有2个不同类型参数的方法? - How to declare a method with 2 parameters of different types in the main method in java? Java - 如何在导入主类时使用方法中的变量 - Java - How to use a variable from a method when imported into the main class 如何在Java中使用多个不同类型的扫描仪? - How to use multiple scanners of different types in java? Java从类方法中获取多个值 - Java get multiple values from a class method 在java中从具有不同数据类型的单个方法返回多个变量 - Return multiple variables from single method with different data types in java Java - 如何通过 main 方法在不同的类中存储变量? - Java - How to store a variable via main method in a different class? 如何在Java中使用其他类的set方法? - How to use set method of a different class in java? 如何在java中从用户获取多个值输入(具有不同类型并用逗号分隔) - How to take Multiple values input(with different types and separated with comma) from user in java 如何从动作侦听器/类构造函数访问变量以在java的main方法中使用 - How to access variable from action listener/class constructor to use in main method in java 如何使用另一个 class 的方法在主 class 中打印 ArrayList? - How to use a method from another class to print an ArrayList in the Main class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM