【问题标题】:Get String From Another Method?从另一个方法获取字符串?
【发布时间】:2013-05-05 23:24:27
【问题描述】:

我有两种方法,第一种方法创建一个字符串,然后我想在第二种方法中使用该字符串。

当我研究这个时,我遇到了在方法之外创建字符串的选项,但是,这在我的情况下不起作用,因为第一种方法以几种方式更改字符串,我需要最终产品第二种方法。

代码:

import java.util.Random;
import java.util.Scanner;


public class yaya {
    public static void main(String[] args) {
        System.out.println("Enter a word:");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        Random ran = new Random();
        int ranNum = ran.nextInt(10);
        input = input + ranNum;
    }

    public void change(String[] args) {
        //more string things here
    }
}

【问题讨论】:

  • 你知道关键字return的作用吗?
  • 两个方法是独立调用的,还是一个方法从另一个方法调用?
  • 向我们展示有问题的代码。
  • 您可能需要添加一些代码来解释您的问题,以便无需猜测您的意图即可回答问题。
  • @SushimMukulDutta 你的回答似乎不合适,而且 - 特别是对于初学者 - 建议使用 static 关键字而不提供任何有关其含义和限制的细节是非常危险的。

标签: java string oop methods


【解决方案1】:

在第二种方法中将修改后的字符串作为参数传递。

【讨论】:

    【解决方案2】:

    创建一个静态变量,在两种方法中使用相同的变量。

    【讨论】:

    • 为什么是静态的?这似乎不需要
    【解决方案3】:

    创建实例变量:

    public class MyClass {
    
        private String str;
    
        public void method1() {
            // change str by assigning a new value to it
        }
    
        public void method2() {
            // the changed value of str is available here
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      您需要返回第一种方法中修改后的字符串并将其传递给第二种方法。假设第一种方法将字符串中的所有实例或“r”替换为“t”(例如):

      public class Program
      {
          public static String FirstMethod(String input)
          {
              String newString = input.replace('r', 't');
              return newString;
          }
      
          public static String SecondMethod(String input)
          {
              // Do something
          }
      
          public static void main(String args[])
          {
              String test = "Replace some characters!";
              test = FirstMethod(test);
              test = SecondMethod(test);
          }
      }
      

      在这里,我们将字符串传递给第一个方法,它返回(returns)修改后的字符串。我们用这个新值更新初始字符串的值,然后将其传递给第二个方法。

      如果字符串与所讨论的对象紧密相关,并且需要在给定对象的上下文中进行大量传递和更新,那么将其作为 Bohemian 描述的实例变量更有意义。

      【讨论】:

        【解决方案5】:
        public class MyClass {
        
          public string method1(String inputStr) {
            inputStr += " AND I am sooo cool";
        
            return inputStr;
          }
        
          public void method2(String inputStr) {
            System.out.println(inputStr);
          }
        
          public static void main(String[] args){
            String firstStr = "I love return";
        
            String manipulatedStr = method1(firstStr);
        
            method2(manipulatedStr);
          }
        }
        

        【讨论】:

          【解决方案6】:

          既然你提到这两个方法应该可以独立调用,你应该试试这样:

          public class Strings {
          
          public static String firstMethod() {
              String myString = ""; //Manipulate the string however you want
              return myString;
          }
          
          public static String secondMethod() {
              String myStringWhichImGettingFromMyFirstMethod = firstMethod();
              //Run whatever operations you want here and afterwards... 
              return myStringWhichImGettingFromMyFirstMethod;
          }
          }
          

          因为这两个方法都是静态的,所以您可以在 main() 中按名称调用它们,而无需创建对象。顺便说一句,您能更具体地说明您要做什么吗?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多