可選參數不正常[重復]

[英]Optional as parameter not working properly [duplicate]


This question already has an answer here:

這個問題在這里已有答案:

I may be having it wrong but i am getting an error with an Optional<> as method argument in Java.

我可能錯了,但我在Java中使用Optional <>作為方法參數時出錯。

I have an interface:

我有一個界面:

public interface FooInterface;

And a class that implements it

還有一個實現它的類

public class Foo implements FooInterface;

Now, suppose i have a method that receives an Optional<FooInterface>

現在,假設我有一個接收Optional 的方法

someMethod(Optional<FooInterface> arg)

I get an error if i do this:

如果我這樣做,我會收到錯誤:

Optional<Foo> foo = Optional.of(new Foo());
someMethod(foo);

It says that Optional<FooInterface> is expected instead of Optional<Foo>.

它說可選 而不是Optional

Why is that?

這是為什么?

3 个解决方案

#1


1  

  1. Use the interface type FooInterface:

    使用接口類型FooInterface:

    final FooInterface foo = new Foo();
    final Optional<FooInterface> optional = Optional.of(foo);
    
  2. Correct the argument type to Optional<? extends FooInterface>. It'll allow passing an Optional<AnyFooInterfaceSubclass>.

    將參數類型更正為Optional 。它將允許傳遞Optional

  3. Try not using the Optional for parameters and fields.

    請勿嘗試使用Optional參數和字段。

#2


1  

That is a general drawback of generic types involving extends/implements.

這是涉及擴展/實現的泛型類型的一般缺點。

interface Ifc { }
class Cls implements Ifc { }
void somemethod2(List<Ifc> arg) { }

List<Cls> list = new UnlinkedList<>();
somemethod2(list); // COMPILE ERROR

In your case a solution would be:

在您的情況下,解決方案將是:

somemethod(Optional<FooInterface>.of(foo));

Or declare

void someMethod(Optional<? extends FooInterface> arg) {

Fortunately Optional is immutable, but for List this would be problematically, as you could not set an element with Foo, as it could have been a List of an other FooInterface implementing class. So maybe better to avoid.

幸運的是,Optional是不可變的,但是對於List,這將是有問題的,因為你不能用Foo設置一個元素,因為它可能是另一個FooInterface實現類的List。所以最好避免。


There exist some proponents of avoiding Optional parameters, in favor of doing only work on non-optional objects. I do not see that as such. IMHO there are many valid uses of Optional parameters.

存在一些避免可選參數的支持者,支持僅對非可選對象進行操作。我不這么認為。恕我直言,可選參數有許多有效用途。

#3


0  

This has nothing to do with Optional itself, it applies to any generic type.

這與Optional本身無關,它適用於任何泛型類型。

Java generics are invariant, which means Collection<Foo> (or Optional, or whatever) is not a subtype of Collection<FooInterface>, even though Foo itself is a subtype of FooInterface.

Java泛型是不變的,這意味着Collection (或Optional,或其他)不是Collection 的子類型,即使Foo本身是FooInterface的子類型。

You need to make that generic type covariant using a wildcard (as @Henrik pointed out already):

您需要使用通配符來創建該泛型類型協變(如@Henrik已經指出的那樣):

/**
 * accepts an optional value of type `FooInterface` or any subtype
 */
someMethod(Optional<? extends FooInterface> arg);    

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/01/04/72f14dceb91808b33971df1f996d9d83.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com