相关疑难解决方法(0)

返回NULL的替代方法

   /**
     * Returns the foo with the matching id in this list
     * 
     * @param id the id of the foo to return
     * @return the foo with the matching id in this list
     */
    public Foo getFoo(int id)
    {
        for (Foo foo : list)
        {
            if (foo.getID() == id)
            {
                return foo;
            }
        }

        return null;
    }
Run Code Online (Sandbox Code Playgroud)

而不是返回的null时候foo没有发现,我应该throwexception?这是否重要,是否有关于这个问题的"最佳实践"成语?顺便说一下,我知道我的例子有点做作,但我希望你明白这个想法......

谢谢.

编辑

更改了Foo基于id的代码,以更好地说明真实场景.

java null design-patterns return-value

25
推荐指数
3
解决办法
4081
查看次数

标签 统计

design-patterns ×1

java ×1

null ×1

return-value ×1