gpt4 book ai didi

java - 为什么describeConstable是可选的?

转载 作者:行者123 更新时间:2023-12-01 06:48:55 24 4
gpt4 key购买 nike

在什么情况下,someEnum.describeConstable()会无法返回EnumDesc

即它将返回一个空的Optional

最佳答案

describeConstable() 是从接口(interface) Constable 继承的方法,其中描述为:

Returns an Optional containing the nominal descriptor for this instance, if one can be constructed, or an empty Optional if one cannot be constructed.

当然,在某些类(class)中,例如Integer ,它永远不能为空:

Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.

但返回值是一个可选,因为并非所有接口(interface)实现都如此。

Constable 的 javadoc有这样说:

A Constable need not be able to (or may choose not to) describe all its instances in the form of a ConstantDesc; this method returns an Optional that can be empty to indicate that a nominal descriptor could not be created for an instance. (For example, MethodHandle will produce nominal descriptors for direct method handles, but not necessarily those produced by method handle combinators.)

关于java - 为什么describeConstable是可选的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57127432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com