Chr*_*des 2 java syntax error-handling null
我需要null检查嵌套方法调用,如下所示:
if (getShopModel() != null && getShopModel().getType() != null)
Run Code Online (Sandbox Code Playgroud)
我不认为这是最好的方法,因为我两次调用getShopModel可能相当昂贵.
有没有更好的方法来检查getShopModel().getType(),这样我只需要调用一次getShopModel()?
使用变量......
Model model = getShopModel();
if (model != null && model.getType() != null)
Run Code Online (Sandbox Code Playgroud)
如果您对'getShopModel'返回的值感兴趣,这将不需要任何费用,节省额外的调用,甚至可以使调试更容易.