很抱歉这个新手简单的问题,但是我需要将String从一个方法传递给另一个方法,现在我有点无能
我希望传递的字符串值(callbackURL& donationId)在此方法中:
public void postData() {
.
.
.
.
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
}
我希望将值传递给另一个方法,假设它叫做private examplePayment()
。我怎么能这样做?
谢谢
答案 0 :(得分:4)
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
}
答案 1 :(得分:3)
通过声明方法签名中的参数,将值传递给另一个方法,例如:
private void examplePayment(String callbackURL, String donationId) {
//your logic in here
}
然后您可以在代码中调用它:
public void postData() {
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL, donationId);
}
答案 2 :(得分:2)
public void postData() {
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL,donationId);
}
private void examplePayment(String callbackURL,String donationId){
}
答案 3 :(得分:2)
你的意思是你想把字符串作为参数传递给函数吗?
public void postData() {
.
.
.
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL, donationId);
}
.
.
.
private void examplePayment(String callbackURL, String donationId) {
.
.
.
}
答案 4 :(得分:1)
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
}
答案 5 :(得分:1)
为什么不用参数创建方法,例如 创建一个类似以下的方法
private examplePayment(String callbackURL, String donationId){
...do your work
}
在调用时可以在postData方法中传递参数
public void postData() {
.
.
.
.
String callbackURL = tokens.nextToken();
String donationId = tokens.nextToken();
examplePayment(callbackURL, donationId);// call the mthod
}
答案 6 :(得分:0)
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 );
}
}
现在,如果要启用examplePayment方法以了解发送字符串的方法,请添加另一个方法参数,例如“pCallingMethod”。
该方法看起来像
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);
}
只要您拨打CallingMethodName
,就可以将examplePayment()
参数传递给此方法。
这可以是一个解决方案。