[英]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
:或者您可以只传递
A
到B
所需的所有参数:
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.