简体   繁体   English

如何将变量从一个参数化方法传递到另一个?

[英]How to pass variable from one parameterized method to another?

How do I send the variable from main method to the Method A , and what Parameter should I pass within A() , in method B .如何将变量从 main 方法发送到 Method A ,以及在方法B中我应该在A()中传递什么参数。

Code:代码:

public class MethodCall {
    
    public void A(String c) {
        
        System.out.println(c);
    }
    
    public void B() {
        A(); // What parameter do I pass here. method B is dependent on A
        
    }
    @Test
    public void D() {
        B();
        MethodCall mc = new MethodCall();
        mc.A("Hello");
    }

}

In method A you expect one parameter of type String therefore you have to provide a parameter for String can be just an empty String "" or null .在方法A中,您需要一个String类型的参数,因此您必须为String提供一个参数,它可以只是一个空的 String ""null


Another option depending on your use case;另一种选择取决于您的用例; you could use varargs:你可以使用可变参数:

public void a(String... varargs) {...}

In this case it's possible to give zero or multiple parameters of type String .在这种情况下,可以提供零个或多个String类型的参数。 So this works:所以这有效:

a("Hello World");

this:这个:

a();

But also this:但也是这样:

a("Hello", "World");

Or you could just pass all parameters necessary for A through B :或者您可以只传递AB所需的所有参数:

public class MethodCall {
    public void A(String c) {
        System.out.println(c);
    }
    
    public void B(String c) {
        A(c);
    }
    @Test
    public void D() {
        MethodCall mc = new MethodCall();
        mc.B("Hello World");
        mc.A("Hello");
    }

}

You should pass the String parameter for within A(), in method B.您应该在方法 B 中在 A() 中传递字符串参数。

public class MethodCall {

    public void A(String c) {
    
        System.out.println(c);
    }

    public void B() {
        A("Pass the String parameter"); 
    
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MethodCall mc = new MethodCall();
        mc.A("Hello");
        mc.B();
    }
}

when you passing the String type as a parameter its mean you should use to same data type when you giving the value to the parameter.当您将 String 类型作为参数传递时,这意味着您应该在为参数赋值时使用相同的数据类型。 you cant use any other types like Integer, float etc. you just need to pass the String value to the given parameter.您不能使用任何其他类型,如 Integer、float 等。您只需将 String 值传递给给定参数。

When you are using non-static method it cannot call directly.当您使用非静态方法时,它不能直接调用。 so use static keyword to call the method in directly.所以使用 static 关键字直接调用方法。

ANSWER-回答-

public class MethodCall {
    
    public static void A(String c) {
        
        System.out.println(c);
    }
    
    public static void B() {
        A("HI "); //you can put `null` or `empty`
        
    }

    public static  void main(String[] args) {
        B();
        MethodCall mc = new MethodCall();
        mc.A("Hello");
    }

}

I am not so sure about your question but here is my take:我不太确定你的问题,但这是我的看法:

    public class MethodCall {

    public void A(String c) {

    System.out.println(c);
    }

    public void B(String s) {
    A(s); //If you want to call A() here, then you would have to pass a variable in the parameters of A(), as A() is a parameterized method by definition.

    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    MethodCall mc = new MethodCall();
    mc.A("Hello"); //This part is right, that is how you pass value of a non static function through main method.
    }

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

相关问题 如何将字符串变量 java 从一种方法传递到另一种方法 - How pass string variable java from one method to another method android将变量从一种方法传递到另一种方法 - android pass variable from one method to another 如何将变量从一个JFrame传递到另一个 - how to pass a variable from one JFrame to another 如何将变量从一个活动传递到另一个活动? - How to pass variable from one activity to another? 如何将变量从一种方法传递到另一种方法? - How to pass variables from one method to another? 如何将更新的全局类变量从一种方法传递到另一种方法? - How do you pass an updated global class variable from one method into another? 如何在Java中将变量值从一种方法传递到另一种方法作为数据库查询的参数 - how to pass variable value from one method onto another as a parameter of database query in java 我如何通过 ArrayList<string> 在 Java 中从一种方法到另一种方法。 变量未初始化错误</string> - How do I pass an ArrayList<String> from one method to another in Java. Variable not initialised error 如何将值从一种方法传递到另一种方法 - How to pass the value from one method to another method 如何在Java中将字符串从一个方法传递到另一个方法 - How to pass a string from one method to another method in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM