{"id":1264,"date":"2015-07-11T08:15:58","date_gmt":"2015-07-11T00:15:58","guid":{"rendered":"http:\/\/coderbee.net\/?p=1264"},"modified":"2015-07-11T08:16:23","modified_gmt":"2015-07-11T00:16:23","slug":"%e3%80%8a%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-scala%e3%80%8b%e7%ac%ac%e4%ba%8c%e7%ab%a0-%e6%a0%b8%e5%bf%83%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/scala\/20150711\/1264","title":{"rendered":"\u300a\u6df1\u5165\u7406\u89e3 Scala\u300b\u7b2c\u4e8c\u7ae0 &#8212; \u6838\u5fc3\u89c4\u5219"},"content":{"rendered":"<h2>\u5728 REPL \u91cc\u505a\u5b9e\u9a8c<\/h2>\n<h2>\u4f18\u5148\u64e6\u7528\u9762\u5411\u8868\u8fbe\u5f0f\u7f16\u7a0b<\/h2>\n<p>\u8868\u8fbe\u5f0f\u8fd4\u56de\u503c\uff1b\u8bed\u53e5\u6267\u884c\u4ee3\u7801\uff0c\u4f46\u4e0d\u8fd4\u56de\u503c\u3002Scala \u5927\u90e8\u5206\u8bed\u53e5\u90fd\u8fd4\u56de\u5176\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u503c\u4f5c\u4e3a\u7ed3\u679c\u3002<\/p>\n<ul>\n<li>\u4e0d\u53d8\u6027\uff08immutability\uff09\uff1a\u6307\u5bf9\u8c61\u6216\u53d8\u91cf\u8d4b\u503c\u540e\u5c31\u4e0d\u518d\u6539\u53d8\u72b6\u6001\u3002<\/li>\n<li>\u53ef\u53d8\u6027\uff08mutability\uff09\uff1a\u6307\u5bf9\u8c61\u6216\u53d8\u91cf\u5728\u5176\u751f\u547d\u5468\u671f\u4e2d\u80fd\u591f\u88ab\u6539\u53d8\u6216\u64cd\u7eb5\u3002<\/li>\n<\/ul>\n<h2>\u4f18\u5148\u9009\u62e9\u4e0d\u53d8\u6027<\/h2>\n<p>Scala \u91cc\u7684\u6240\u6709\u53d8\u91cf\u90fd\u662f\u6307\u5411\u5bf9\u8c61\u7684\u5f15\u7528\u3002\u628a\u53d8\u91cf\u58f0\u660e\u4e3a val \u53c2\u6570\u610f\u5473\u7740\u5b83\u662f\u4e2a\u4e0d\u53d8\u7684\u201c\u5f15\u7528\u201d\u3002<\/p>\n<p>\u6240\u6709\u7684\u65b9\u6cd5\u53c2\u6570\u90fd\u662f\u4e0d\u53d8\u5f15\u7528\uff0c\u7c7b\u53c2\u6570\u9ed8\u8ba4\u662f\u4e0d\u53d8\u5f15\u7528\u3002<\/p>\n<p>\u521b\u5efa\u53ef\u53d8\u5f15\u7528\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u4f7f\u7528 var \u5f15\u7528\u3002<\/p>\n<p>Scala \u91cc\u4e0d\u53d8\u6027\u5f88\u91cd\u8981\uff0c\u5b83\u6709\u52a9\u4e8e\u7a0b\u5e8f\u5458\u63a8\u7406\u4ee3\u7801\uff0c\u5728\u5b9a\u4e49\u5224\u7b49\u6216\u5199\u5e76\u53d1\u7a0b\u5e8f\u65f6\u5c24\u5176\u660e\u663e\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cScala \u7528\u5bf9\u8c61\u4f4d\u7f6e\u5224\u7b49\u6cd5\u548c\u6563\u5217\u3002\u5bf9\u8c61\u4f4d\u7f6e\u5224\u7b49\u6cd5\u7528\u5bf9\u8c61\u5728\u5185\u5b58\u4e2d\u7684\u4f4d\u7f6e\u6765\u4f5c\u4e3a\u5224\u7b49\u7684\u552f\u4e00\u56e0\u7d20\u3002<\/p>\n<p>Scala \u63d0\u4f9b\u4e86 <code>##<\/code> \u65b9\u6cd5\u6765\u5bf9\u5e94 hashCode \u65b9\u6cd5\uff0c<code>==<\/code> \u65b9\u6cd5\u6765\u5bf9\u5e94 equals \u65b9\u6cd5\u3002hashCode \u548c equals \u65b9\u6cd5\u603b\u662f\u6210\u5bf9\u5b9e\u73b0\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728\u5b9e\u73b0\u5224\u7b49\u65f6\u4e00\u822c\u63a8\u8350\u786e\u4fdd\u5982\u4e0b\u7ea6\u675f\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49\uff0c\u5b83\u4eec\u7684\u6563\u5217\u503c\u5e94\u8be5\u4e5f\u76f8\u7b49\uff1b<\/li>\n<li>\u4e00\u4e2a\u5bf9\u8c61\u7684\u6563\u5217\u503c\u5728\u5bf9\u8c61\u751f\u547d\u5468\u671f\u4e2d\u4e0d\u5e94\u8be5\u53d8\u5316\uff1b<\/li>\n<li>\u5728\u628a\u5bf9\u8c61\u53d1\u9001\u5230\u53e6\u4e00\u4e2a JVM \u65f6\uff0c\u5e94\u8be5\u7528\u4e24\u4e2a JVM \u91cc\u90fd\u6709\u7684\u5c5e\u6027\u6765\u5224\u7b49\u3002<\/li>\n<\/ul>\n<p>\u6ee1\u8db3\u4e0a\u8ff0\u8981\u6c42\u7684\u552f\u4e00\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528\u4e0d\u53d8\u5bf9\u8c61\u3002<\/p>\n<p>\u4e0d\u53d8\u5bf9\u8c61\u53ef\u4ee5\u5b89\u5168\u5730\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u4f20\u9012\u800c\u4e0d\u7528\u62c5\u5fc3\u4e89\u7528\u3002\u80fd\u591f\u6d88\u9664\u9501\u4ee5\u53ca\u9501\u5e26\u6765\u7684\u5404\u79cd\u6f5c\u5728 bug\uff0c\u6781\u5927\u63d0\u4f9b\u4ee3\u7801\u5e93\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<h2>\u4f7f\u7528 Option \u4e0d\u7528 null<\/h2>\n<p>Option \u53ef\u4ee5\u89c6\u4f5c\u5bb9\u5668\uff0c\u91cc\u9762\u8981\u4e48\u6709\u4e1c\u897f\uff08Some\uff09\uff0c\u8981\u4e48\u4ec0\u4e48\u90fd\u6ca1\u6709\uff08None\uff09\u3002<\/p>\n<p>\u5728 Scala \u91cc\uff0c\u53c2\u6570\u7c7b\u578b\u4e3a Option \u8868\u793a\u53c2\u6570\u53ef\u80fd\u662f\u672a\u521d\u59cb\u5316\u7684\u3002Scala \u7684\u60ef\u4f8b\u662f\u4e0d\u8981\u4f7f\u7528 null \u6216\u672a\u521d\u59cb\u5316\u7684\u53c2\u6570\u4f20\u7ed9\u51fd\u6570\u3002<\/p>\n<h2>\u591a\u6001\u573a\u666f\u4e0b\u7684\u5224\u7b49<\/h2>\n<p>\u5bf9\u4e8e\u9700\u8981\u6bd4\u5f15\u7528\u5224\u7b49\u66f4\u5f3a\u7684\u5224\u7b49\u7684\u7c7b\uff0c\u6700\u597d\u907f\u514d\u591a\u5c42\u5b9e\u4f53\u7c7b\u5c42\u6b21\u3002<\/p>\n<p>\u5728 JVM \u91cc\u5b9e\u73b0 equals \u65f6\uff0c\u4e00\u822c\u6765\u8bf4\u5728\u505a\u6df1\u5ea6\u5224\u7b49\u524d\u5148\u5224\u65ad\u5f15\u7528\u662f\u5426\u76f8\u7b49\u7684\u6027\u80fd\u66f4\u9ad8\u3002\u8bbe\u8ba1\u597d\u7684 equals \u65b9\u6cd5\u7684\u53e6\u4e00\u4e2a\u5e38\u7528\u8303\u5f0f\u662f\uff08\u5728\u6df1\u5ea6\u5224\u7b49\u4e4b\u524d\uff09\u7528 hashCode \u505a\u4e2a\u65e9\u671f\u5224\u65ad\u3002<\/p>\n<p>\u591a\u6001\u5224\u7b49\u5b9e\u73b0\u4e00\u822c\u7ee7\u627f <code>scala.Equals<\/code> \u7279\u8d28\uff0c\u8986\u5199 canEqual \u65b9\u6cd5\uff0c\u548c equals \u65b9\u6cd5\u4e32\u8054\u8d77\u6765\u4f7f\u7528\uff0c\u4f7f\u5b50\u7c7b\u53ef\u4ee5\u8df3\u51fa\uff08opt-out\uff09\u5176\u7236\u7c7b\u7684\u5224\u7b49\u5b9e\u73b0\u3002\uff08\u6ce8\uff1a\u5176\u5b9e\u5c31\u662f\u4e2a\u56de\u8c03\u7684\u94a9\u5b50\uff09<\/p>\n<pre><code class=\"scala\">trait InstantaneousTime extends Equals {\n    val repr: Int\n    override def canEqual(other: Any) = other.isInstanceOf[InstantaneousTime]\n\n    override def equals(other: Any): Boolean =\n        other match {\n            case that: InstantaneousTime =&gt;         \/\/  \u901a\u8fc7 \u6a21\u5f0f\u5339\u914d \u505a\u4e86  this -&gt; that  \u7684\u7c7b\u578b\u5224\u7b49\n                if (this eq that) true else {\n                    (that.## == this.##) &amp;&amp; (that canEqual this) &amp;&amp; (repr == that.repr)      \/\/  \u901a\u8fc7 canEqual \u505a\u4e86  that -&gt; this  \u7684\u7c7b\u578b\u5224\u7b49\n                }\n            case _ =&gt; false\n        }\n\n    override def hashCode(): Int = repr.hashCode\n}\n\ntrait Event extends InstantaneousTime {\n    val name: String\n    override def canEqual(other: Any) = other.isInstanceOf[Event]\n\n    override def equals(other: Any): Boolean = other match {\n        case that: Event =&gt;      \/\/  \u901a\u8fc7 \u6a21\u5f0f\u5339\u914d \u505a\u4e86  this -&gt; that  \u7684\u7c7b\u578b\u5224\u7b49\n            if (this eq that) {\n                true\n            } else {\n                (that canEqual this) &amp;&amp; (repr == that.repr) &amp;&amp; (name == that.name)    \/\/  \u901a\u8fc7 canEqual \u505a\u4e86  that -&gt; this  \u7684\u7c7b\u578b\u5224\u7b49\n            }   \n        case _ =&gt; false   \n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u793a\u4f8b\u90fd\u662f\u505a\u4e86\u53cc\u5411\u7684\u7c7b\u578b\u5224\u7b49\uff0c\u4f7f\u6bd4\u8f83\u7684\u5b9e\u4f8b\u5fc5\u987b\u662f\u540c\u4e00\u7c7b\u578b\u7684\uff0c\u5c4f\u853d\u4e86\u7236\u7c7b\u548c\u5b50\u7c7b\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 REPL \u91cc\u505a\u5b9e\u9a8c \u4f18\u5148\u64e6\u7528\u9762\u5411\u8868\u8fbe\u5f0f\u7f16\u7a0b \u8868\u8fbe\u5f0f\u8fd4\u56de\u503c\uff1b\u8bed\u53e5\u6267\u884c\u4ee3\u7801\uff0c\u4f46\u4e0d &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/scala\/20150711\/1264\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181],"tags":[234,227],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1264"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1264"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1264\/revisions"}],"predecessor-version":[{"id":1265,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1264\/revisions\/1265"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}