【问题标题】:Passing a String from one method to another method将字符串从一种方法传递到另一种方法
【发布时间】:2011-12-23 09:37:44
【问题描述】:

我需要将一个字符串从一种方法传递给另一种方法,现在我有点不知所措。

我要传递的字符串值(callbackURL 和donationId)在这个方法中:

public void postData() {
.
.
.
.
            String callbackURL = tokens.nextToken();
            String donationId = tokens.nextToken();
                
    } 

我想将值传递给另一个方法,假设它称为private examplePayment()。我怎样才能做到这一点?

【问题讨论】:

  • 说真的,如果你是这种绿色,请不要从 android 开始。从 Plain Old Java 开始,当你对它有一定的了解后开始使用 android。

标签: java android


【解决方案1】:
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
}

【讨论】:

  • 但是它是如何读取字符串来自 postData() 方法的呢??
  • 它没有读取字符串,您将其显式传递给 examplePayment() 方法。
【解决方案2】:

您可以通过在方法签名中声明参数来将值传递给另一个方法,例如:

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);
}

【讨论】:

    【解决方案3】:

    您的意思是要将字符串作为参数传递给函数?

    public void postData() {
       .
       .
       . 
       String callbackURL = tokens.nextToken();
       String donationId = tokens.nextToken();
       examplePayment(callbackURL, donationId);
    }
    .
    .
    .
    private void examplePayment(String callbackURL, String donationId) {
        .
        . 
        .
    }
    

    【讨论】:

      【解决方案4】:
      public void postData() {
                  String callbackURL = tokens.nextToken();
                  String donationId = tokens.nextToken();
      
                 examplePayment(callbackURL,donationId);
          } 
      private void examplePayment(String callbackURL,String donationId){
      
      
      }
      

      【讨论】:

        【解决方案5】:
        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
        }
        

        【讨论】:

          【解决方案6】:

          为什么不创建带有参数的方法,例如 创建如下方法

          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
              } 
          

          【讨论】:

            【解决方案7】:
            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);
            }
            

            每当您调用examplePayment() 时,将CallingMethodName 作为参数传递给此方法。

            这可能是一个解决方案。

            【讨论】:

              猜你喜欢
              • 2011-06-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-01-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-02-24
              相关资源
              最近更新 更多