Java 将 Optional 用作类中的属性是一种好习惯吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29033518/
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
Is it a good practice to use Optional as an attribute in a class?
提问by bashoogzaad
I have read something about the purpose of Optional
(unfortunately I don't remember where) in Java 8, and I was surprised the writer didn't mention the use of an Optional
as an attribute in a class.
我读过一些关于Optional
Java 8 中的目的(不幸的是我不记得在哪里),我很惊讶作者没有提到在类中使用 anOptional
作为属性。
Since I am using optionals pretty frequently in my classes, I was wondering if this is a good practice. Or could I better just use normal attributes, which return null
when they are not set?
由于我在课堂上经常使用可选项,我想知道这是否是一个好习惯。或者我可以更好地使用普通属性,null
当它们未设置时返回?
Note:It may look like my question is opinion based, but I get the feeling using Optional
in a class is really not the way to go (after reading the mentioned post). However, I like to use it and can't find any downside of using it.
注意:看起来我的问题似乎是基于意见的,但我觉得Optional
在课堂上使用确实不是可行的方法(阅读上述帖子后)。但是,我喜欢使用它并且找不到使用它的任何缺点。
Example
例子
I would like to give an example to clarify. I have a class Transaction
, which is built like this:
我想举一个例子来澄清。我有一个类Transaction
,它是这样构建的:
public class Transaction {
private Optional<Customer> = Optional.empty();
....
vs
对比
public class Transaction {
private Customer = null;
....
When checking on a Customer
, I think it is most logical to use transaction.getCustomer().isPresent()
than transaction.getCustomer() != null
. In my opinion the first code is cleaner than the second one.
在检查 a 时Customer
,我认为使用transaction.getCustomer().isPresent()
than最合乎逻辑transaction.getCustomer() != null
。在我看来,第一个代码比第二个代码更干净。
采纳答案by Jesper
Java 8's Optional
was mainly intended for return values from methods, and not for properties of Java classes, as described in Optional in Java SE 8:
Java 8Optional
主要用于方法的返回值,而不是 Java 类的属性,如Java SE 8中的Optional 中所述:
Of course, people will do what they want. But we did have a clear intention when adding this feature, and it was not to be a general purpose
Maybe
orSome
type, as much as many people would have liked us to do so. Our intention was to provide a limited mechanism for library method return typeswhere there needed to be a clear way to represent "no result", and usingnull
for such was overwhelmingly likely to cause errors.The key here is the focus on use as a return type. The class is definitively not intended for use as a property of a Java Bean.Witness to this is that
Optional
does not implementSerializable
, which is generally necessary for widespread use as a property of an object.
当然,人们会做他们想做的。但是我们在添加这个功能时确实有一个明确的意图,它不是一个通用的目的
Maybe
或Some
类型,就像很多人希望我们这样做的那样。我们的目的是为库方法返回类型提供一种有限的机制,在这种机制中需要有一种明确的方式来表示“无结果”,而使用null
for 则极有可能导致错误。这里的关键是专注于用作返回类型。该类绝对不打算用作 Java Bean 的属性。证明这一点的是
Optional
没有实现Serializable
,这通常是广泛使用作为对象的属性所必需的。
回答by Crazyjavahacking
I think it is a theoretical question.
我认为这是一个理论问题。
The notion of optional values was brought from functional languages world. Those languages usually also support pattern matching on language level and allow you to pattern match on the optional value.
可选值的概念来自函数式语言世界。这些语言通常还支持语言级别的模式匹配,并允许您对可选值进行模式匹配。
In functional languages function calls usually return an optional value that other code could pattern match on.
在函数式语言中,函数调用通常返回一个可选值,其他代码可以对其进行模式匹配。
I have never seen passing an optional as argument, but that does not mean it is a bad think. It looks weird though.
我从未见过将可选参数作为参数传递,但这并不意味着这是一个糟糕的想法。不过看起来很奇怪。