{"id":216838,"date":"2021-05-01T08:17:03","date_gmt":"2021-05-01T00:17:03","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=216838"},"modified":"2021-04-25T09:18:56","modified_gmt":"2021-04-25T01:18:56","slug":"understand-the-use","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/understand-the-use.html","title":{"rendered":"\u4e86\u89e3\u4e0bScala \u4f7f\u7528 Option\u3001Some\u3001None\uff0c\u907f\u514d null \u4f7f\u7528"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u5927\u591a\u6570\u8bed\u8a00\u90fd\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5173\u952e\u5b57\u6216\u8005\u5bf9\u8c61\u6765\u8868\u793a\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\u7684\u662f\"\u65e0\"\uff0c\u5728 Java\uff0c\u5b83\u662f null\u3002\u5728 Java \u91cc\uff0cnull \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5bf9\u5b83\u8c03\u7528\u4efb\u4f55\u65b9\u6cd5\u90fd\u662f\u975e\u6cd5\u7684\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n \u907f\u514d null \u4f7f\u7528<\/strong><\/div>\n \u5927\u591a\u6570\u8bed\u8a00\u90fd\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5173\u952e\u5b57\u6216\u8005\u5bf9\u8c61\u6765\u8868\u793a\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\u7684\u662f\"\u65e0\"\uff0c\u5728 Java\uff0c\u5b83\u662f null\u3002\u5728 Java \u91cc\uff0cnull \u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u4e0d\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6240\u4ee5\u5bf9\u5b83\u8c03\u7528\u4efb\u4f55\u65b9\u6cd5\u90fd\u662f\u975e\u6cd5\u7684\u3002\u4f46\u662f\u8fd9\u5bf9\u8bed\u8a00\u8bbe\u8ba1\u8005\u6765\u8bf4\u662f\u4e00\u4ef6\u4ee4\u4eba\u7591\u60d1\u7684\u9009\u62e9\u3002\u4e3a\u4ec0\u4e48\u8981\u5728\u7a0b\u5e8f\u5458\u5e0c\u671b\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\u8fd4\u56de\u4e00\u4e2a\u5173\u952e\u5b57\u5462\uff1f<\/p>\n Scala \u7684 Option\u7c7b\u578b<\/strong><\/div>\n \u4e3a\u4e86\u8ba9\u6240\u6709\u4e1c\u897f\u90fd\u662f\u5bf9\u8c61\u7684\u76ee\u6807\u66f4\u52a0\u4e00\u81f4\uff0c\u4e5f\u4e3a\u4e86\u9075\u5faa\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e60\u60ef\uff0cScala \u9f13\u52b1\u4f60\u5728\u53d8\u91cf\u548c\u51fd\u6570\u8fd4\u56de\u503c\u53ef\u80fd\u4e0d\u4f1a\u5f15\u7528\u4efb\u4f55\u503c\u7684\u65f6\u5019\u4f7f\u7528 Option \u7c7b\u578b\u3002\u5728\u6ca1\u6709\u503c\u7684\u65f6\u5019\uff0c\u4f7f\u7528 None\uff0c\u8fd9\u662f Option \u7684\u4e00\u4e2a\u5b50\u7c7b\u3002\u5982\u679c\u6709\u503c\u53ef\u4ee5\u5f15\u7528\uff0c\u5c31\u4f7f\u7528 Some \u6765\u5305\u542b\u8fd9\u4e2a\u503c\u3002Some \u4e5f\u662f Option \u7684\u5b50\u7c7b\u3002 None \u88ab\u58f0\u660e\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u7c7b\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u5b83\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u6837\uff0c\u5b83\u591a\u5c11\u6709\u70b9\u50cf null \u5173\u952e\u5b57\uff0c\u4f46\u5b83\u5374\u662f\u4e00\u4e2a\u5b9e\u5b9e\u5728\u5728\u7684\uff0c\u6709\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/p>\n \u5e94\u7528\u4f8b\u5b50<\/strong><\/div>\n Option \u7c7b\u578b\u7684\u503c\u901a\u5e38\u4f5c\u4e3a Scala \u96c6\u5408\u7c7b\u578b\uff08List, Map \u7b49\uff09\u64cd\u4f5c\u7684\u8fd4\u56de\u7c7b\u578b\u3002\u6bd4\u5982 Map \u7684 get \u65b9\u6cd5\uff1a<\/p>\n scala> val capitals = Map(\"France\"->\"Paris\", \"Japan\"->\"Tokyo\", \"China\"->\"Beijing\")\r\ncapitals: scala.collection.immutable.Map[String,String] = Map(France -> Paris, Japan -> Tokyo, China -> Beijing)\r\n\r\nscala> capitals get \"France\"\r\nres0: Option[String] = Some(Paris)\r\n\r\nscala> capitals get \"North Pole\"\r\nres1: Option[String] = None<\/pre>\n |