简体   繁体   English

如何将变量从main方法类转移到另一个类?

[英]How to transfer a variable from main method class to another class?

Okay... in my main class, I ask the user to enter an input int value. 好的...在我的主类中,我要求用户输入一个输入int值。 Now I need to take this value to a different class to do some coding. 现在我需要将这个值带到另一个类来进行编码。 How? 怎么样?

Code from main class: 主类代码:

neededValue = keyboard.nextInt();

object.methodName(xxxx, yyyy):

Code from a different method in a different class: 来自不同类中的不同方法的代码:

public void methodName(double xxxx, int yyyy) {

        int index;

        for (index = 0; index < array.length; index++) {
            xxxx = array[index].xxxx;
            if (xxxx > **neededValue**) {
                //some more code

            }

        }
    }

Lets say user input is 4, how can I transfer that "4" to other classes? 让我们说用户输入是4,如何将“4”转移到其他类?

You need to pass neededValue as argument 您需要传递neededValue作为参数

neededValue = keyboard.nextInt();   
object.methodName(xxxx, yyyy,neededValue):

Then in methodName function 然后在methodName函数中

public void methodName(double xxxx, int yyyy, int neededValue) {

    int index;

    for (index = 0; index < array.length; index++) {
        xxxx = array[index].xxxx;
        if (xxxx > **neededValue**) {
            //some more code
        }
    }
}

Hope it helped. 希望它有所帮助。

From main: 从主要:

neededValue = keyboard.nextInt();

object.methodName(xxxx, yyyy, neededValue):

From the class: 来自班级:

public void methodName(double xxxx, int yyyy, int neededValue){
    int index;

    for (index = 0; index < array.length; index++) {
        xxxx = array[index].xxxx;
        if (xxxx > **neededValue**) {
            //some more code
        }
    }
}

Add a parameter to your another class method and pass the value from your main class.. same as below: 将参数添加到另一个类方法并从主类传递值。如下所示:

class A{
...
neededValue = keyboard.nextInt();

DifferentClass object = new DifferentClass();
object.methodName(xxxx, yyyy, neededValue):
...
}

class DifferentClass{
public void methodName(double xxxx, int yyyy, int neededValue) {
...
}
}
  1. You can pass the nextValue variable to the method directly, like: 您可以将nextValue变量直接传递给方法,例如:

object.methodName(xxxx, yyyy, nextValue)

  1. or you can declare a int variable in the class A of object: 或者你可以在对象的类A中声明一个int变量:

    int someValue;

and set it by constructor or setter 并通过构造函数或setter设置它

object = new A(nextValue);
or object.setNextValue(nextValue)

then, your invoke object.methodName(xxxx, yyyy) will work with the 'someValue' field inside the object 然后,你的invoke object.methodName(xxxx, yyyy)将使用对象内的'someValue'字段

Multiple ways to do it, few are listed below, 有多种方法,下面列出的很少,

i) Create parameter in your method (explained in other solutions). i)在您的方法中创建参数(在其他解决方案中解释)。

ii) Create a variable in your class with get/set methods in your class (explained in other solutions). ii)使用类中的get / set方法在类中创建变量(在其他解决方案中进行了解释)。

iii) Create a static variable in your class (explained below) iii)在你的班级中创建一个静态变量(如下所述)

You can have a static variable in your class, 您可以在班级中拥有静态变量,

private static int neededValue;

And in your main, you can assign the value for it. 在您的主要内容中,您可以为其分配值。

ClassName.neededValue = keyboard.nextInt();

object.methodName(xxxx, yyyy):

And you can use directly in your class method, 你可以直接在你的类方法中使用,

public void methodName(double xxxx, int yyyy){
int index;

for (index = 0; index < array.length; index++) {
    xxxx = array[index].xxxx;
    if (xxxx > ClassName.neededValue) {
        //some more code
    }
 }
}

Disclaimer: Know more about static keyword before using it. 免责声明:在使用之前了解有关静态关键字的更多信息。

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

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