如何在 Java 中创建 IN OUT 或 OUT 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4455693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:46:18  来源:igfitidea点击:

How to create IN OUT or OUT parameters in Java

javalanguage-agnosticparametersout-parameters

提问by Lukas Eder

In PL/SQL (or many other languages), I can have IN OUT or OUT parameters, which are returned from a procedure. How can I achieve a similar thing in Java?

在 PL/SQL(或许多其他语言)中,我可以拥有从过程返回的 IN OUT 或 OUT 参数。如何在 Java 中实现类似的功能?

I know this trick:

我知道这个技巧:

public void method(String in, String[] inOut, String[] inOut2) {
  inOut[0] = in;
}

Where the inparameter represents an IN parameter and the inOutparameter can hold a return value. The convention would be that String[] inOutis an array of inOut.length == 1.

其中in参数代表一个 IN 参数并且该inOut参数可以保存一个返回值。约定是它String[] inOut是一个inOut.length == 1.

That's kind of clumsy.

这有点笨拙。

EDIT Feedback to answers: Other tricks include:

编辑对答案的反馈:其他技巧包括:

  • holder/wrapper classes, but I don't want to introduce any new types, callbacks, etc.
  • return values: I'd like a generalsolution. I.e. one with several IN OUT parameters involved.
  • wrapper for IN OUT parameter as a return value: That's a viable option, but still not so nice, because that wrapper would have to be generated somehow
  • 持有者/包装类,但我不想引入任何新类型、回调等。
  • 返回值:我想要一个通用的解决方案。即一个涉及几个 IN OUT 参数。
  • IN OUT 参数的包装器作为返回值:这是一个可行的选项,但仍然不是很好,因为必须以某种方式生成该包装器

Does anyone know a better way to achieve this generally? The reason I need a general solution is because I want to generate convenience source code from PL/SQL in a database schema.

有谁知道更好的方法来实现这一目标?我需要通用解决方案的原因是我想从数据库模式中的 PL/SQL 生成方便的源代码。

采纳答案by T.J. Crowder

My question would be: Why doesn't methodreturnsomething? Rather than setting an in/out argument?

我的问题是:为什么不method返回一些东西?而不是设置输入/输出参数?

But assuming you absolutely, positively musthave an in/out argument, which is a whole different question, then the array trick is fine. Alternately, it's not less clumsy, but the other way is to pass in an object reference:

但是假设您绝对肯定必须有一个输入/输出参数,这是一个完全不同的问题,那么数组技巧就可以了。或者,它也不是那么笨拙,但另一种方法是传入一个对象引用:

public class Foo {
    private String value;

    public Foo(String v) {
        this.value = v;
    }

    public String getValue() {
        return this.value;
    }

    public void setValue(String v) {
        this.value = v;
    }
 }

 // ....
 public void method(String in, Foo inOut) {
     inOut.setValue(in);
 }

(Or, of course, just make valuepublic.) See? I said it wasn't less clumsy.

(或者,当然,只是value公开。)看到了吗?我说它不那么笨拙。

But I'd ask again: Can't methodreturn something? And if it needs to return multiple things, can't it return an object instance with properties for those things?

但我会再问一次:不能method退货吗?如果它需要返回多个东西,它不能返回一个带有这些东西属性的对象实例吗?

Off-topic:This is one of the areas where I really like the C# approach. One of the arguments against in/out arguments is that they're unclear at the point where you're callingthe function. So C# makes you make it clear, by specifying the keyword both at the declaration of the function andwhen calling it. In the absense of that kind of syntactic help, I'd avoid "simulating" in/out arguments.

题外话:这是我非常喜欢 C# 方法的领域之一。反对输入/输出参数的论据之一是,它们在您调用函数的地方不清楚。因此,C# 通过在函数声明调用函数时指定关键字,让您清楚明白。如果没有这种语法帮助,我会避免“模拟”输入/输出参数。

回答by dty

There's no direct way. Other technique include:

没有直接的办法。其他技术包括:

  • Passing a holder object (a bit like your 1-ary array)
  • Using, e.g., an AtomicInteger
  • Passing a more useful object from a business perspective that happens to be mutable
  • A callback to a custom interface for receiving the result
  • 传递一个持有者对象(有点像你的一元数组)
  • 使用,例如,一个 AtomicInteger
  • 从业务角度传递一个更有用的对象,恰好是可变的
  • 回调到自定义接口以接收结果

If you think about it, the array trick is not dissimilar to passing a T* in C/C++

如果你仔细想想,数组技巧与在 C/C++ 中传递 T* 并没有什么不同

回答by Art Licis

Java copies anything you pass as an argument. If you pass a primitive, inside method you have copy of that primitive, and no modifications will affect the actual variable outside method. If you pass object, you pass copy of reference, which actually references to the original object. This is the way how you can propagate modifications to the context of something that called the method - by modifying the state of the object that the reference is 'pointing' to. See more on this: Does Java Pass by Value or by Reference?

Java 复制您作为参数传递的任何内容。如果你传递一个原语,内部方法你有该原语的副本,并且任何修改都不会影响方法外的实际变量。如果传递对象,则传递引用的副本,它实际上是对原始对象的引用。这是您如何将修改传播到调用方法的上下文的方式 - 通过修改引用“指向”的对象的状态。查看更多相关信息:Java 是按值传递还是按引用传递?