Re: [分享] 原來 Void 可以這樣用

看板java作者 (邀怪)時間16年前 (2009/03/12 22:11), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/8 (看更多)
※ 引述《sbrhsieh (sbr)》之銘言: : 我覺得這個理由有點牽強,如果你實作 interface 裡某個 method,而你的實作永遠 : 要 return null,你可以很簡單地(不靠較深的 engineering/tool)先宣告一個 local : variable: : public Object visit(Node f) { : final Object ret = null; : if (...) { // some shortcut : ... : return ret; : } : // normal condition : ... : ... : return ret; : } 這個方法就沒有 compiler 幫你檢查了... 如果我能記得 return ret; 我大可以 return null 就好了. public Void visit(Node f) { ... } 可以讓 compiler 幫我記得, 挺好的. : 或者是 : public Object visit(Nodef) { : visitImpl(Node f); : return null; : } : private void visitImpl(Node f) { : // your implementation here : } 我的 visitor 已經有 50 多個 method 了, 直接放個 Void 還是簡單的多 :) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.251.144.115
文章代碼(AID): #19kHW847 (java)
討論串 (同標題文章)
文章代碼(AID): #19kHW847 (java)