如何在Java中创建IN OUT或OUT参数 [英] How to create IN OUT or OUT parameters in Java

查看:266
本文介绍了如何在Java中创建IN OUT或OUT参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我知道这个窍门:

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

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

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.

有点笨拙.

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

  • holder/wrapper类,但我不想引入任何新类型,回调等.
  • 返回值:我想要一个常规解决方案. IE.一个包含多个IN OUT参数.
  • 将OUT参数的包装器作为返回值:这是一个可行的选择,但仍然不是很好,因为必须以某种方式生成包装器
  • 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

有人知道一般有更好的方法吗?我需要一个通用解决方案的原因是因为我想在数据库模式中从PL/SQL生成便捷的源代码.

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.

推荐答案

我的问题是:为什么method 不返回什么?而不是设置in/out参数?

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

(或者,当然,只需公开value.)看到吗?我说这还不算笨.

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

但是我会再问:method不能退还东西吗?而且,如果需要返回多个事物,是否不能返回具有这些事物属性的对象实例?

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?

离题:这是我非常喜欢C#方法的领域之一.反对输入/输出参数的参数之一是,在您调用函数时,它们不清楚.因此,C#通过在函数的声明中同时指定关键字时都指定关键字来使您更清楚.在没有这种语法帮助的情况下,我将避免模拟"输入/输出参数.

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.

这篇关于如何在Java中创建IN OUT或OUT参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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