為什么我不建議你用去 “ ! = null " 做判空?
往期熱門文章:
3、線上 4 臺機(jī)器同一時間全部 OOM,到底發(fā)生了什么?
4、爽??!Intellij IDEA 神器居然還藏著這些實用小技巧 !
5、這樣調(diào)優(yōu):讓你的 IDEA 快到飛起來,效率真高!
作者:lizeyang 來源:blog.csdn.net/lizeyang/article/details/40040817
...if (someobject != null) {
someobject.doCalc();}...public interface Action {
void doSomething();}
public interface Parser {
Action findAction(String userInput);}public class MyParser implements Parser {
private static Action DO_NOTHING = new Action() {
public void doSomething() { /* do nothing */ }
};
public Action findAction(String userInput) {
// ...
if ( /* we can't find any actions */ ) {
return DO_NOTHING;
}
}}Parser parser = ParserFactory.getParser();
if (parser == null) {
// now what?
// this would be an example of where null isn't (or shouldn't be) a valid response
}
Action action = parser.findAction(someInput);
if (action == null) {
// do nothing} else {
action.doSomething();}ParserFactory.getParser().findAction(someInput).doSomething();
"bar".equals(foo)
foo.equals("bar")往期熱門文章:
1、《歷史文章分類導(dǎo)讀列表!精選優(yōu)秀博文都在這里了!》
2、萬億級數(shù)據(jù)應(yīng)該怎么遷移? 3、從應(yīng)用到底層 36張圖帶你進(jìn)入Redis世界 4、寫代碼有這16個好習(xí)慣,可以減少80%非業(yè)務(wù)的bug 5、順豐快遞:請簽收MySQL靈魂十連
6、一個基于SpringBoot + MyBatis + Vue的代碼生成器 7、Redis 分布式鎖使用不當(dāng),超賣了100瓶飛天茅臺?。?! 8、如何設(shè)計訂單系統(tǒng)?這篇寫得太好了! 9、如果MySQL磁盤滿了,會發(fā)生什么?還真被我遇到了! 10、阿里開源的27個項目,值得收藏!
評論
圖片
表情
