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

发布于 2024-10-07 11:54:13 字数 645 浏览 7 评论 0原文

在 PL/SQL(或许多其他语言)中,我可以具有从过程返回的 IN OUT 或 OUT 参数。我怎样才能在Java中实现类似的事情?

我知道这个技巧:

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

其中 in 参数代表 IN 参数,而 inOut 参数可以保存返回值。约定是 String[] inOutinOut.length == 1 的数组。

这有点笨拙。

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

  • 持有者/包装类,但我不想引入任何新类型、回调等。
  • 返回值:我想要一个通用< /strong> 解决方案。即涉及多个 IN OUT 参数。
  • IN OUT 参数的包装器作为返回值:这是一个可行的选择,但仍然不是很好,因为必须以某种方式生成该包装器

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

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?

I know this trick:

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

Where the in parameter represents an IN parameter and the inOut parameter can hold a return value. The convention would be that String[] inOut is an array of 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 general solution. 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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狂之美人 2024-10-14 11:54:13

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

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

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

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

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

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

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

But assuming you absolutely, positively must have 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 value public.) See? I said it wasn't less clumsy.

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

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 calling the function. So C# makes you make it clear, by specifying the keyword both at the declaration of the function and when calling it. In the absense of that kind of syntactic help, I'd avoid "simulating" in/out arguments.

终止放荡 2024-10-14 11:54:13

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

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?

一指流沙 2024-10-14 11:54:13

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

  • 传递一个持有者对象(有点像一元数组)
  • 使用例如 AtomicInteger
  • 从业务角度传递一个更有用的对象,该对象恰好是可变的
  • 回调到自定义接口以接收结果

如果您想一想,数组技巧与 C/C++ 中传递 T* 没有什么不同

There's no direct way. Other techniques 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文