Re: [分享] 原來 Void 可以這樣用
※ 引述《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
討論串 (同標題文章)