如何使用Java中的输出参数? [英] How to use an output parameter in Java?

查看:91
本文介绍了如何使用Java中的输出参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请给我一个使用输出参数在功能上的一些示例code?我试图谷歌,但只是发现它只是功能。我想在其他功能使用该输出值。

Could someone please give me some sample code that uses an output parameter in function? I've tried to Google it but just found it just in functions. I'd like to use this output value in another function.

在code我正在开发打算在Android上运行。

The code I am developing intended to be run in Android.

推荐答案

Java的推移价值;有没有退出参数就像在C#。

Java passes by value; there's no out parameter like in C#.

您可以使用返回,或(通过值)变异为引用传递的对象。

You can either use return, or mutate an object passed as a reference (by value).

  • <一个href="http://stackoverflow.com/questions/2806545/does-java-have-something-like-cs-ref-and-out-keywords">Does Java的拥有类似C#的ref和out关键字? ?(NO!)
  • 就是Java通过引用传递?(NO!)
public class FunctionSample {
    static String fReturn() {
        return "Hello!";
    }
    static void fArgNoWorkie(String s) {
        s = "What am I doing???"; // Doesn't "work"! Java passes by value!
    }
    static void fMutate(StringBuilder sb) {
        sb.append("Here you go!");
    }
    public static void main(String[] args) {
        String s = null;

        s = fReturn();
        System.out.println(s); // prints "Hello!"

        fArgNoWorkie(s);
        System.out.println(s); // prints "Hello!"

        StringBuilder sb = new StringBuilder();
        fMutate(sb);
        s = sb.toString();
        System.out.println(s); // prints "Here you go!"
    }

}

另请参见

  • 什么是一成不变的意思?
  • <一个href="http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-java">StringBuilder和StringBuffer在Java中
  • See also

    • What is meant by immutable?
    • StringBuilder and StringBuffer in Java
    • 对于 code 的OP需要帮助,下面是使用一个特殊的值(通常对于引用类型)来表示成功/失败的一个典型的解决方案:

      As for the code that OP needs help with, here's a typical solution of using a special value (usually null for reference types) to indicate success/failure:

      相反的:

      String oPerson= null;
      if (CheckAddress("5556", oPerson)) {
         print(oPerson); // DOESN'T "WORK"! Java passes by value; String is immutable!
      }
      
      private boolean CheckAddress(String iAddress, String oPerson) {
         // on search succeeded:
         oPerson = something; // DOESN'T "WORK"!
         return true;
         :
         // on search failed:
         return false;
      }
      

      使用一个字符串返回类型,而不是与来表示失败。

      Use a String return type instead, with null to indicate failure.

      String person = checkAddress("5556");
      if (person != null) {
         print(person);
      }
      
      private String checkAddress(String address) {
         // on search succeeded:
         return something;
         :
         // on search failed:
         return null;
      }
      

      这是怎么<一个href="http://java.sun.com/javase/6/docs/api/java/io/BufferedReader.html#readLine%28%29"><$c$c>java.io.BufferedReader.readLine()工作,例如:它返回的instanceof字符串(可能是一个空字符串!),直到它返回来表示结束 - 搜索。

      This is how java.io.BufferedReader.readLine() works, for example: it returns instanceof String (perhaps an empty string!), until it returns null to indicate end of "search".

      这是当然不限于引用类型返回值,。关键是,必须有一些特殊的值(s)表示,从来都不是一个有效的值,并且使用该值用于特殊用途。

      This is not limited to a reference type return value, of course. The key is that there has to be some special value(s) that is never a valid value, and you use that value for special purposes.

      另外一个典型的例子是<一个href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#indexOf%28int%29"><$c$c>String.indexOf:它返回 1 来表示搜索失败。

      Another classic example is String.indexOf: it returns -1 to indicate search failure.

      注意的:因为​​Java没有输入和输出的参数,使用我的概念 - Ø - preFIX(如 iAddress oPerson )是不必要的,unidiomatic。

      Note: because Java doesn't have a concept of "input" and "output" parameters, using the i- and o- prefix (e.g. iAddress, oPerson) is unnecessary and unidiomatic.

      如果您需要返回几个值,通常他们以某种方式有关(如 X 一个点的坐标)。最好的解决办法是封装这些值相加。人们已经使用了对象[] 名单,其中,对象&gt; ,或一般对&LT ; T1,T2&GT; ,不过说真的,你自己的类型将是最好的。


      A more general solution

      If you need to return several values, usually they're related in some way (e.g. x and y coordinates of a single Point). The best solution would be to encapsulate these values together. People have used an Object[] or a List<Object>, or a generic Pair<T1,T2>, but really, your own type would be best.

      对于这个问题,我推荐一个不变的信息搜索结果键入类似这样的封装布尔字符串搜索结果:

      For this problem, I recommend an immutable SearchResult type like this to encapsulate the boolean and String search results:

      public class SearchResult {
         public final String name;
         public final boolean isFound;
      
         public SearchResult(String name, boolean isFound) {
            this.name = name;
            this.isFound = isFound;
         }
      }
      

      然后在你的搜索功能,请执行以下操作:

      Then in your search function, you do the following:

      private SearchResult checkAddress(String address) {
        // on address search succeed
        return new SearchResult(foundName, true);
        :
        // on address search failed
        return new SearchResult(null, false);
      }
      

      然后你使用这样的:

      And then you use it like this:

      SearchResult sr = checkAddress("5556");
      if (sr.isFound) {
        String name = sr.name;
        //...
      }
      

      如果你愿意,你可以(并且可能应该)做最后不可改变领域的非 - 公开,和使用公开干将吧。

      If you want, you can (and probably should) make the final immutable fields non-public, and use public getters instead.

      这篇关于如何使用Java中的输出参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆