[英]Passing a String from one method to another method
Sorry for this newbie simple question, but I need to pass a String from one method to another method and now I'm a bit clueless 抱歉,这个新手简单的问题,但是我需要将String从一个方法传递给另一个方法,现在我有点笨了
the String values (callbackURL & donationId) I wanna pass are inside this method: 我想传递的String值(callbackURL和donationId)在此方法内:
public void postData() {
.
.
.
.
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
}
and I want to pass the values to another method, let's say it's called private examplePayment()
. 我想将值传递给另一个方法,假设它称为
private examplePayment()
。 How am I able to perform that? 我该如何执行? Thank you
谢谢
public void postData(){
.
.
.
.
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL, donationId);
}
private void examplePayment(String callback, String donationid){
//deal with your callback and donationid variables
}
You pass values to another method by declaring the parameters in the method signature, for example: 通过在方法签名中声明参数,将值传递给另一个方法,例如:
private void examplePayment(String callbackURL, String donationId) {
//your logic in here
}
Then you can call it in your code like so: 然后您可以像这样在代码中调用它:
public void postData() {
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL, donationId);
}
public void postData() {
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL,donationId);
}
private void examplePayment(String callbackURL,String donationId){
}
You mean you want to pass the strings to the function as arguments? 您是说要将字符串作为参数传递给函数?
public void postData() {
.
.
.
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL, donationId);
}
.
.
.
private void examplePayment(String callbackURL, String donationId) {
.
.
.
}
public String postData() {
String callbackURL = tokens.nextToken();
String someID = tokens.nextToken();
this.examplePayment(callbackURL, someID);
}
OR
private examplePayment(String callbackURL, String someID){
// DO whatever you want with callback and someID
}
Why dont you create method with arguments eg create a method like the following 为什么不创建带有参数的方法,例如创建类似以下的方法
private examplePayment(String callbackURL, String donationId){
...do your work
}
while calling you can pass arguments in postData method 调用时可以在postData方法中传递参数
public void postData() {
.
.
.
.
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL, donationId);// call the mthod
}
public class StringTest{
public void postData() {
String callbackURL = "abc.cde.com";
String donationId = "1233445";
examplePayment(callbackURL,donationId);
}
private void examplePayment(String pStrCallbackURL , String pStrDonationId){
System.out.println("Callback url"+ pStrCallbackURL );
System.out.println("DonationId "+ pStrDonationId );
}
}
Now if you want to enable your examplePayment method to know which method is sending the strings , then add another method parameter , say "pCallingMethod" . 现在,如果要启用examplePayment方法来知道哪个方法正在发送字符串,则添加另一个方法参数,例如“ pCallingMethod”。
The method will look like 该方法看起来像
private void examplePayment(String pStrCallbackURL , String pStrDonationId , pStrCallingMethod){
System.out.println("Callback url"+ pStrCallbackURL );
System.out.println("DonationId "+ pStrDonationId );
System.out.println("Calling Method"+ pStrCallingMethod);
}
Pass the CallingMethodName
as parameter to this method whenever you call examplePayment()
. 每当调用
examplePayment()
时,都将CallingMethodName
作为参数传递给此方法。
This can be a solution. 这可以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.