这个 Holder<> 在 Java 中有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25777793/
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-11-02 08:40:35  来源:igfitidea点击:

What does this Holder<> do in Java?

javagenerics

提问by Sparshith P

Can someone please explain this code?

有人可以解释一下这段代码吗?

public void getSupplierByZipCode(
    @WebParam(name = "zip", targetNamespace = "http://www.webservicex.net/")
    String zip,
    @WebParam(name = "GetSupplierByZipCodeResult", targetNamespace =  "http://www.webservicex.net/", mode = WebParam.Mode.OUT)
    Holder<Boolean> getSupplierByZipCodeResult,
    @WebParam(name = "SupplierDataLists", targetNamespace = "http://www.webservicex.net/", mode = WebParam.Mode.OUT)
    Holder<SupplierDataList> supplierDataLists);

I've never seen Holderbefore in Java. What are Holder<Boolean>and Holder<SupplierDataList>in the function? Are they like outputs?? I need the supplier data list from this function.

我以前从未Holder在 Java 中见过。什么是Holder<Boolean>Holder<SupplierDataList>在功能?他们喜欢输出吗?我需要此功能的供应商数据列表。

回答by user2864740

See Holder- The entire purpose is to "hold a value" while allowing side-effect modifications of itself(and thus change value it "holds").

参见Holder- 整个目的是“持有一个价值”,同时允许对其自身进行副作用修改(从而改变它“持有”的价值)。

The instance variable (value) representing the contained/"held" value can be reassigned; this is used to facilitate how [multiple] values are "returned" in the WS - through explicit modification of the holders supplied as parameters. (Note the usage of WebParam.Mode.OUTas well.)

value表示包含/“持有”值的实例变量 ( ) 可以重新分配;这用于促进 [多个] 值如何在 WS 中“返回” - 通过显式修改作为参数提供的持有者。(注意WebParam.Mode.OUTas的用法。)

This "extra layer" is required because Java is alwaysCall By Value; the Holder then effectively fakes a pointer-indirection (let's call it "reference-indirection"), as what might be done in C, which leads to Call By (Object) Sharingsemantics.

这个“额外的层”是必需的,因为 Java总是Call By Value;然后 Holder 有效地伪造了一个指针间接(让我们称之为“引用间接”),就像在 C 中可能做的那样,这导致调用(对象)共享语义。

Imagine:

想象:

// Outside WS function - setup parameters and invoke
String zip = "98682";
Holder<Boolean> result = new Holder<Boolean>();
getSupplierByZipCode(zip, result, ..);

// Then inside the function the Holder is modified and a new value
// is assigned to it's value member.
getSupplierByZipCodeResult.value = true;

// And outside again, the mutations are visibile still
if (result.value) {
    // Yay!
}

Since strings are immutable and the zip is notwrapped in a Holder then the zip code cannot be changed (or "returned" by) the WS call.

由于字符串是不可变的并且 zip包装在 Holder 中,因此 WS 调用无法更改(或“返回”)邮政编码。

See also:

也可以看看: