{"id":210271,"date":"2021-02-15T09:13:56","date_gmt":"2021-02-15T01:13:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=210271"},"modified":"2021-01-29T14:18:41","modified_gmt":"2021-01-29T06:18:41","slug":"abuse-null","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/abuse-null.html","title":{"rendered":"\u4f60\u6ee5\u7528\u5224\u7a7a\u4e86\u5417\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4f5c\u4e3a\u642c\u7816\u515a\uff0c\u6211\u4eec\u5bf9\u5224\u7a7a\u4e00\u5b9a\u518d\u719f\u6089\u4e0d\u8fc7\u4e86\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u6211\u4e0d\u5efa\u8bae\u4f60\u7528\u53bb \u201c ! = null \" \u505a\u5224\u7a7a?<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u95ee\u9898<\/strong><\/div>\n

\u4e3a\u4e86\u907f\u514d\u7a7a\u6307\u9488\u8c03\u7528\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u770b\u5230\u8fd9\u6837\u7684\u8bed\u53e5\uff1a<\/p>\n

\r\n...if (someobject != null) { \r\n    someobject.doCalc();}... \r\n<\/pre>\n

\u6700\u7ec8\uff0c\u9879\u76ee\u4e2d\u4f1a\u5b58\u5728\u5927\u91cf\u5224\u7a7a\u4ee3\u7801\uff0c\u591a\u4e48\u4e11\u964b\u7e41\u5197!\u5982\u4f55\u907f\u514d\u8fd9\u79cd\u60c5\u51b5?\u6211\u4eec\u662f\u5426\u6ee5\u7528\u4e86\u5224\u7a7a\u5462?<\/p>\n

\u7cbe\u534e\u56de\u7b54<\/strong><\/div>\n

\u8fd9\u662f\u521d\u3001\u4e2d\u7ea7\u7a0b\u5e8f\u733f\u7ecf\u5e38\u4f1a\u9047\u5230\u7684\u95ee\u9898\u3002\u4ed6\u4eec\u603b\u559c\u6b22\u5728\u65b9\u6cd5\u4e2d\u8fd4\u56de null\uff0c\u56e0\u6b64\uff0c\u5728\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u65f6\uff0c\u4e5f\u4e0d\u5f97\u4e0d\u53bb\u5224\u7a7a\u3002<\/p>\n

\u53e6\u5916\uff0c\u4e5f\u8bb8\u53d7\u6b64\u4e60\u60ef\u5f71\u54cd\uff0c\u4ed6\u4eec\u603b\u6f5c\u610f\u8bc6\u5730\u8ba4\u4e3a\uff0c\u6240\u6709\u7684\u8fd4\u56de\u90fd\u662f\u4e0d\u53ef\u4fe1\u4efb\u7684\uff0c\u4e3a\u4e86\u4fdd\u62a4\u81ea\u5df1\u7a0b\u5e8f\uff0c\u5c31\u52a0\u4e86\u5927\u91cf\u7684\u5224\u7a7a\u3002<\/p>\n

\u5410\u69fd\u5b8c\u6bd5\uff0c\u56de\u5230\u8fd9\u4e2a\u9898\u76ee\u672c\u8eab\uff0c\u8fdb\u884c\u5224\u7a7a\u524d\uff0c\u8bf7\u533a\u5206\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n

  • null \u662f\u4e00\u4e2a\u6709\u6548\u6709\u610f\u4e49\u7684\u8fd4\u56de\u503c\u3002(Where null is a valid response in terms of the contract; and)<\/li>\n
  • null \u662f\u65e0\u6548\u6709\u8bef\u7684(Where it isn't a valid response.)<\/li>\n

    \u4f60\u53ef\u80fd\u8fd8\u4e0d\u660e\u767d\u8fd9\u4e24\u53e5\u8bdd\u7684\u610f\u601d\uff0c\u4e0d\u6025\uff0c\u7ee7\u7eed\u5f80\u4e0b\u770b\uff0c\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e24\u79cd\u60c5\u51b5\u3002<\/p>\n

    \u2460\u5148\u8bf4\u7b2c 2 \u79cd\u60c5\u51b5<\/strong><\/span><\/div>\n

    null \u5c31\u662f\u4e00\u4e2a\u4e0d\u5408\u7406\u7684\u53c2\u6570\uff0c\u5c31\u5e94\u8be5\u660e\u786e\u5730\u4e2d\u65ad\u7a0b\u5e8f\uff0c\u5f80\u5916\u629b\u9519\u8bef\u3002\u8fd9\u79cd\u60c5\u51b5\u5e38\u89c1\u4e8e api \u65b9\u6cd5\u3002<\/p>\n

    \u4f8b\u5982\u4f60\u5f00\u53d1\u4e86\u4e00\u4e2a\u63a5\u53e3\uff0cid \u662f\u4e00\u4e2a\u5fc5\u9009\u7684\u53c2\u6570\uff0c\u5982\u679c\u8c03\u7528\u65b9\u6ca1\u4f20\u8fd9\u4e2a\u53c2\u6570\u7ed9\u4f60\uff0c\u5f53\u7136\u4e0d\u884c\u3002<\/p>\n

    \u4f60\u8981\u611f\u77e5\u5230\u8fd9\u4e2a\u60c5\u51b5\uff0c\u544a\u8bc9\u8c03\u7528\u65b9\u201c\u563f\uff0c\u54e5\u4eec\uff0c\u4f60\u4f20\u4e2a null \u7ed9\u6211\u505a\u751a\"\u3002<\/p>\n

    \u76f8\u5bf9\u4e8e\u5224\u7a7a\u8bed\u53e5\uff0c\u66f4\u597d\u7684\u68c0\u67e5\u65b9\u5f0f\u6709\u4e24\u4e2a\uff1a<\/p>\n

  • assert \u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u628a\u9519\u8bef\u539f\u56e0\u653e\u5230 assert \u7684\u53c2\u6570\u4e2d\uff0c\u8fd9\u6837\u4e0d\u4ec5\u80fd\u4fdd\u62a4\u4f60\u7684\u7a0b\u5e8f\u4e0d\u5f80\u4e0b\u8d70\uff0c\u800c\u4e14\u8fd8\u80fd\u628a\u9519\u8bef\u539f\u56e0\u8fd4\u56de\u7ed9\u8c03\u7528\u65b9\uff0c\u5c82\u4e0d\u662f\u4e00\u4e3e\u4e24\u5f97\u3002(\u539f\u6587\u4ecb\u7ecd\u4e86 assert \u7684\u4f7f\u7528\uff0c\u8fd9\u91cc\u7701\u7565)<\/li>\n
  • \u4e5f\u53ef\u4ee5\u76f4\u63a5\u629b\u51fa\u7a7a\u6307\u9488\u5f02\u5e38\u3002\u4e0a\u9762\u8bf4\u4e86\uff0c\u6b64\u65f6 null \u662f\u4e2a\u4e0d\u5408\u7406\u7684\u53c2\u6570\uff0c\u6709\u95ee\u9898\u5c31\u662f\u6709\u95ee\u9898\uff0c\u5c31\u5e94\u8be5\u5927\u5927\u65b9\u65b9\u5f80\u5916\u629b\u3002<\/li>\n
    \u2461\u7b2c 1 \u79cd\u60c5\u51b5\u4f1a\u66f4\u590d\u6742\u4e00\u4e9b<\/strong><\/span><\/div>\n

    \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cnull \u662f\u4e2a\u201d\u770b\u4e0a\u53bb\u201c\u5408\u7406\u7684\u503c\uff0c\u4f8b\u5982\uff0c\u6211\u67e5\u8be2\u6570\u636e\u5e93\uff0c\u67d0\u4e2a\u67e5\u8be2\u6761\u4ef6\u4e0b\uff0c\u5c31\u662f\u6ca1\u6709\u5bf9\u5e94\u503c\uff0c\u6b64\u65f6 null \u7b97\u662f\u8868\u8fbe\u4e86\u201c\u7a7a\u201d\u7684\u6982\u5ff5\u3002<\/p>\n

    \u8fd9\u91cc\u7ed9\u4e00\u4e9b\u5b9e\u8df5\u5efa\u8bae<\/strong><\/div>\n
    \u2460\u5047\u5982\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u662f collections\u3002<\/strong><\/span><\/div>\n

    \u5f53\u8fd4\u56de\u7ed3\u679c\u662f\u7a7a\u65f6\uff0c\u4f60\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684 collections(empty list)\uff0c\u800c\u4e0d\u8981\u8fd4\u56de null\u3002<\/p>\n

    \u8fd9\u6837\u8c03\u7528\u4fa7\u5c31\u80fd\u5927\u80c6\u5730\u5904\u7406\u8fd9\u4e2a\u8fd4\u56de\uff0c\u4f8b\u5982\u8c03\u7528\u4fa7\u62ff\u5230\u8fd4\u56de\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5 print list.size()\uff0c\u53c8\u65e0\u9700\u62c5\u5fc3\u7a7a\u6307\u9488\u95ee\u9898\u3002<\/p>\n

    \u4ec0\u4e48?\u60f3\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\uff0c\u4e0d\u8bb0\u5f97\u4e4b\u524d\u5b9e\u73b0\u8be5\u65b9\u6cd5\u6709\u6ca1\u6309\u7167\u8fd9\u4e2a\u539f\u5219?\u6240\u4ee5\u8bf4\uff0c\u4ee3\u7801\u4e60\u60ef\u5f88\u91cd\u8981!<\/p>\n

    \u5982\u679c\u4f60\u517b\u6210\u4e60\u60ef\uff0c\u90fd\u662f\u8fd9\u6837\u5199\u4ee3\u7801(\u8fd4\u56de\u7a7a collections \u800c\u4e0d\u8fd4\u56de null)\uff0c\u4f60\u8c03\u7528\u81ea\u5df1\u5199\u7684\u65b9\u6cd5\u65f6\uff0c\u5c31\u80fd\u5927\u80c6\u5730\u5ffd\u7565\u5224\u7a7a\u3002<\/p>\n

    \u2461\u8fd4\u56de\u7c7b\u578b\u4e0d\u662f collections\uff0c\u53c8\u600e\u4e48\u529e\u5462?<\/strong><\/span><\/div>\n

    \u90a3\u5c31\u8fd4\u56de\u4e00\u4e2a\u7a7a\u5bf9\u8c61(\u800c\u975e null \u5bf9\u8c61)\uff0c\u4e0b\u9762\u4e3e\u4e2a\u201c\u6817\u5b50\u201d\uff0c\u5047\u8bbe\u6709\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n

    \r\npublic interface Action { \r\n  void doSomething();} \r\n \r\npublic interface Parser { \r\n  Action findAction(String userInput);} \r\n<\/pre>\n

    \u5176\u4e2d\uff0cParse \u6709\u4e00\u4e2a\u63a5\u53e3 FindAction\uff0c\u8fd9\u4e2a\u63a5\u53e3\u4f1a\u4f9d\u636e\u7528\u6237\u7684\u8f93\u5165\uff0c\u627e\u5230\u5e76\u6267\u884c\u5bf9\u5e94\u7684\u52a8\u4f5c\u3002<\/p>\n

    \u5047\u5982\u7528\u6237\u8f93\u5165\u4e0d\u5bf9\uff0c\u53ef\u80fd\u5c31\u627e\u4e0d\u5230\u5bf9\u5e94\u7684\u52a8\u4f5c(Action)\uff0c\u56e0\u6b64 findAction \u5c31\u4f1a\u8fd4\u56de null\uff0c\u63a5\u4e0b\u6765 action \u8c03\u7528 doSomething \u65b9\u6cd5\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u7a7a\u6307\u9488\u3002<\/p>\n

    \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u4e2a\u65b9\u5f0f\uff0c\u5c31\u662f\u4f7f\u7528 Null Object pattern(\u7a7a\u5bf9\u8c61\u6a21\u5f0f)\u3002<\/p>\n

    \u6211\u4eec\u6765\u6539\u9020\u4e00\u4e0b<\/strong><\/div>\n

    \u7c7b\u5b9a\u4e49\u5982\u4e0b\uff0c\u8fd9\u6837\u5b9a\u4e49 findAction \u65b9\u6cd5\u540e\uff0c\u786e\u4fdd\u65e0\u8bba\u7528\u6237\u8f93\u5165\u4ec0\u4e48\uff0c\u90fd\u4e0d\u4f1a\u8fd4\u56de null \u5bf9\u8c61\uff1a<\/p>\n

    \r\npublic class MyParser implements Parser { \r\n  private static Action DO_NOTHING = new Action() { \r\n    public void doSomething() { \/* do nothing *\/ } \r\n  }; \r\n \r\n  public Action findAction(String userInput) { \r\n    \/\/ ... \r\n    if ( \/* we can't find any actions *\/ ) { \r\n      return DO_NOTHING; \r\n    } \r\n  }} \r\n<\/pre>\n

    \u5bf9\u6bd4\u4e0b\u9762\u4e24\u4efd\u8c03\u7528\u5b9e\u4f8b\uff1a<\/p>\n

    \u2460\u5197\u4f59\uff1a\u6bcf\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c31\u5224\u4e00\u6b21\u7a7a<\/strong><\/span><\/div>\n
    \r\nParser parser = ParserFactory.getParser(); \r\nif (parser == null) { \r\n  \/\/ now what? \r\n  \/\/ this would be an example of where null isn't (or shouldn't be) a valid response \r\n} \r\nAction action = parser.findAction(someInput); \r\nif (action == null) { \r\n  \/\/ do nothing} else { \r\n  action.doSomething();} \r\n<\/pre>\n
    \u2461\u7cbe\u7b80<\/strong><\/span><\/div>\n
    ParserFactory.getParser().findAction(someInput).doSomething(); <\/pre>\n

    \u56e0\u4e3a\u65e0\u8bba\u4ec0\u4e48\u60c5\u51b5\uff0c\u90fd\u4e0d\u4f1a\u8fd4\u56de\u7a7a\u5bf9\u8c61\uff0c\u56e0\u6b64\u901a\u8fc7 findAction \u62ff\u5230 action \u540e\uff0c\u53ef\u4ee5\u653e\u5fc3\u5730\u8c03\u7528 action \u7684\u65b9\u6cd5\u3002<\/p>\n

    \u5176\u4ed6\u56de\u7b54\u7cbe\u9009<\/strong><\/div>\n
    \u2460\u5982\u679c\u8981\u7528 equal \u65b9\u6cd5\uff0c\u8bf7\u7528 object<\u4e0d\u53ef\u80fd\u4e3a\u7a7a>.equal(object<\u53ef\u80fd\u4e3a\u7a7a>))<\/strong><\/span><\/div>\n

    \u4f8b\u5982\u4f7f\u7528\uff1a<\/p>\n

    \r\n\"bar\".equals(foo) <\/pre>\n

    \u800c\u4e0d\u662f\uff1a<\/p>\n

    \r\nfoo.equals(\"bar\") <\/pre>\n
    \u2461Java8 \u6216\u8005 guava lib \u4e2d\uff0c\u63d0\u4f9b\u4e86 Optional \u7c7b\uff0c\u8fd9\u662f\u4e00\u4e2a\u5143\u7d20\u5bb9\u5668\uff0c\u901a\u8fc7\u5b83\u6765\u5c01\u88c5\u5bf9\u8c61\uff0c\u53ef\u4ee5\u51cf\u5c11\u5224\u7a7a\u3002\u4e0d\u8fc7\u4ee3\u7801\u91cf\u8fd8\u662f\u4e0d\u5c11\u3002\u4e0d\u723d\u3002<\/strong><\/span><\/div>\n
    \u2462\u5982\u679c\u4f60\u60f3\u8fd4\u56de null\uff0c\u8bf7\u505c\u4e0b\u6765\u60f3\u4e00\u60f3\uff0c\u8fd9\u4e2a\u5730\u65b9\u662f\u5426\u66f4\u5e94\u8be5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/strong><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"

    \u4e3a\u4e86\u907f\u514d\u7a7a\u6307\u9488\u8c03\u7528\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u770b\u5230\u8fd9\u6837\u7684\u8bed\u53e5\uff1a …if (someobject != null) { so […]<\/p>\n","protected":false},"author":1479,"featured_media":210277,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-210271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=210271"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210271\/revisions"}],"predecessor-version":[{"id":210278,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210271\/revisions\/210278"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/210277"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=210271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=210271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=210271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}