面向对象编程中null的最佳替代方法是什么? [英] What is the best alternative to null in object oriented programming?
本文介绍了面向对象编程中null的最佳替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不满意避免在OOP中使用null . 有没有其他解决方案? 我不想在以此方式中避免这种情况.
I don't feel satisfied by avoiding null in OOP. Is there any alternative solution? I don't like to avoid it in this way either.
最好的处理方式是什么?
What is the best possible way to handle it?
推荐答案
Java 8 has the new Optional
type which is what I think you are asking about
可能包含也可能不包含非null值的容器对象.如果存在值,则
isPresent()
将返回true,而get()
将返回该值.
A container object which may or may not contain a non-null value. If a value is present,
isPresent()
will return true andget()
will return the value.
这篇关于面向对象编程中null的最佳替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文