简体   繁体   English

在Java中的方法之间共享多个变量

[英]Sharing multiple variables between methods in Java

What is the best way to pass multiple variables between methods? 在方法之间传递多个变量的最佳方法是什么? I have done it one way in which there is a return statement in the first method, but I want to access every variable from the first method in the second method. 我已经完成了第一种方法中有return语句的一种方式,但是我想从第二种方法中的第一种方法访问每个变量。 The variable "value0" in method "addTasks0" is not recognized. 无法识别方法“ addTasks0”中的变量“ value0”。

public Integer addValues(){
int value0 = sliderExample.getValue();
int value1 = sliderExample1.getValue();
int value2 = sliderExample2.getValue();
int value3 = sliderExample3.getValue();
int value4 = sliderExample4.getValue();
int value5 = sliderExample5.getValue();
int value6 = sliderExample6.getValue();
int value7 = sliderExample7.getValue();
int totalValue = (value0 + value1 + value2 + value3 + value4 + value5 + value6 +     value7);

return totalValue;
}


public void addTasks0(){      
    Integer totalValue = addValues();

      if (totalValue <= 100) {
      progressBar.setValue(value0);
  }
  else{
      int overkill = (totalValue - 100);
      int margin = (value0 - overkill);
      String taskTitle = inputText.getText();
      String marginmessage = "The maximum priority of " + taskTitle + " is" + Integer.toString(margin) + "%";
      sliderExample.setValue(0);
          //custom title, warning icon
    JOptionPane.showMessageDialog(null, marginmessage, "Check Math", JOptionPane.WARNING_MESSAGE);
  }
}

In your code you are showing that you have still access to the sliderExample in the addTasks0 method, so as in addValues you can again do 在你的代码是显示您仍可以访问到sliderExampleaddTasks0方法,从而在addValues你可以再做一次

int value0 = sliderExample.getValue();

in the addTasks0 method. addTasks0方法中。 You do not need to pass the value. 您不需要传递值。

However if you really want to you could return an int array from addValues where arr[0] == value0 and arr[1] == totalValue 但是,如果您确实需要,可以从addValues返回一个int数组,其中arr [0] == value0和arr [1] == totalValue

or of course you could just move these variables as class fields 或者当然,您可以将这些变量作为类字段移动

You could define your variables in the beginning of your class so that they are accessible to all methods of the class. 您可以在类的开头定义变量,以便类的所有方法都可以访问它们。

class ClassName {
    private int value0, value1, value2...;

    public Integer addValues() {
        value0 = sliderExample.getValue();
        value1 = sliderExample1.getValue();
        value2 = sliderExample2.getValue();
        // etc.
    }

    public void addTasks0() {
        Integer totalValue = addValues();

        if (totalValue <= 100) {
            progressBar.setValue(value0)    // Initialized in addValues method
        }

I would do it this way 我会这样

ArrayList<SliderExample> sliderExamples = new ArrayList<>();

public int addValues() {
    int sum = 0;
    for(SliderExample se: sliderExamples) sum += se.getValue();
    return sum;
}

public void addTask(int taskNr) {
    SliderExample se = sliderExamples.get(taskNr);
    int value = se.getValue();
    ...
}

You can try to pass in a list of key value pairs where each function can access the requisite data by using a specific key name 您可以尝试传递键值对的列表,其中每个函数都可以使用特定的键名来访问必需的数据

Map<String key,Object value) params;
...
funcA(Map params){
 params.add(somePredefinedKey,newProperty);
...
funcB(params);
}

funcB(Map params){
Object paramA=params.get(somePredefinedKey);

... } ...}

This is particularaly useful if you are passing your parameters through multiple methods in sequence 如果您要依次通过多个方法传递参数,这特别有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM