{"id":1270,"date":"2015-07-20T20:32:15","date_gmt":"2015-07-20T12:32:15","guid":{"rendered":"http:\/\/coderbee.net\/?p=1270"},"modified":"2015-07-21T21:59:31","modified_gmt":"2015-07-21T13:59:31","slug":"%e3%80%8a%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3-scala%e3%80%8b%e7%ac%ac%e4%b8%89%e7%ab%a0-%e6%9d%a5%e7%82%b9%e6%a0%b7%e5%bc%8f-%e7%bc%96%e7%a0%81%e8%a7%84%e8%8c%83","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/scala\/20150720\/1270","title":{"rendered":"\u300a\u6df1\u5165\u7406\u89e3 Scala\u300b\u7b2c\u4e09\u7ae0 &#8212; \u6765\u70b9\u6837\u5f0f &#8212; \u7f16\u7801\u89c4\u8303"},"content":{"rendered":"<p>\u7f16\u7801\u89c4\u8303\u7684\u76ee\u6807\u53ef\u4ee5\u5f52\u7eb3\u4e3a\u4e09\u7c7b\uff1a<\/p>\n<ul>\n<li>\u4ee3\u7801\u53d1\u73b0\uff1a\u4f5c\u7528\u5728\u4e8e\u4f7f\u5de5\u7a0b\u5e08\u80fd\u591f\u8f7b\u677e\u5730\u63a8\u7406\u4ee3\u7801\uff0c\u770b\u61c2\u5176\u4ed6\u5de5\u7a0b\u5e08\u7684\u610f\u56fe\u3002<\/li>\n<li>\u4ee3\u7801\u4e00\u81f4\u6027\uff1a\u4f5c\u7528\u5728\u4e8e\u4f7f\u6574\u4e2a\u9879\u76ee\u7684\u7f16\u7801\u98ce\u683c\u4fdd\u6301\u4e00\u81f4\u3002<\/li>\n<li>\u9519\u8bef\u9632\u6b62\uff1a\u6307\u90a3\u4e9b\u6709\u52a9\u4e8e\u5728\u751f\u4ea7\u4ee3\u7801\u91cc\u907f\u514d bug \u7684\u6837\u5f0f\u89c4\u5219\u3002<\/li>\n<\/ul>\n<p>\u8bbe\u8ba1\u7f16\u7801\u89c4\u8303\u5e94\u8be5\u91c7\u53d6\u7684\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u4ece\u9519\u8bef\u9632\u6b62\u89c4\u8303\u5f00\u59cb\uff1b<\/li>\n<li>\u5f00\u53d1\u4e00\u5957\u4ee3\u7801\u53d1\u73b0\u76f8\u5173\u7684\u89c4\u8303\uff0c\u5e94\u4e0e\u56e2\u961f\u4f7f\u7528\u7684\u5f00\u53d1\u73af\u5883\u5339\u914d\uff1b<\/li>\n<li>\u6700\u540e\u662f\u4ee3\u7801\u4e00\u81f4\u6027\u89c4\u8303\uff0c\u5e94\u8003\u8651\u5230\u81ea\u52a8\u5316\u5de5\u5177\u652f\u6301\u7684\u9700\u8981\u3002<\/li>\n<\/ol>\n<p>\u7a7a\u60ac\u64cd\u4f5c\u7b26\u662f\u6307\u4e00\u884c\u4ee3\u7801\u7684\u6700\u540e\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f\u4e00\u4e2a\u64cd\u4f5c\u7b26\uff0c\u6709\u52a9\u4e8e\u7f16\u8bd1\u5668\u5224\u65ad\u8bed\u53e5\u771f\u6b63\u7ed3\u675f\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u540d\u79f0\u91cd\u6574\uff08name mangling\uff09\uff1a\u662f\u6307\u7f16\u8bd1\u5668\u4fee\u6539\u6216\u4fee\u9970\u7c7b\u540d\u6216\u65b9\u6cd5\u540d\uff0c\u4ee5\u4fbf\u628a\u5b83\u7f16\u8bd1\u5230\u5e95\u5c42\u7684\u5e73\u53f0\u4e0a\u3002Scala \u91cc\u540d\u79f0\u91cd\u6574\u7528\u5728\u5d4c\u5957\u7c7b\u6216\u8f85\u52a9\u65b9\u6cd5\u4e0a\u3002<\/p>\n<p>\u5f53 Scala \u5fc5\u987b\u751f\u6210\u533f\u540d\u51fd\u6570\u6216\u533f\u540d\u7c7b\u65f6\uff0cScala \u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u5b57\uff0c\u5305\u62ec\u51fd\u6570\u6216\u7c7b\u6240\u5904\u7684\u7c7b\u540d\u3001\u5b57\u7b26\u4e32 <code>annofun<\/code> \u548c\u4e00\u4e2a\u6570\u5b57\uff0c \u7528 <code>$<\/code> \u7b26\u53f7\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<p>\u7f16\u8bd1\u5668\u4e5f\u4f1a\u5bf9\u9ed8\u8ba4\u53c2\u6570\u5e94\u7528\u540d\u79f0\u91cd\u6574\u63aa\u65bd\u3002Scala \u91cc\u9ed8\u8ba4\u53c2\u6570\u4e5f\u662f\u88ab\u7f16\u7801\u6210\u65b9\u6cd5\u7684\uff0c\u65b9\u6cd5\u540d\u4e3a <code>default<\/code> \u52a0\u4e0a\u7528\u6765\u8868\u793a\u53c2\u6570\u5728\u51fd\u6570\u91cc\u7684\u51fa\u73b0\u987a\u5e8f\u7684\u5e8f\u53f7\u3002\u8fd9\u51fa\u73b0\u5728\u65b9\u6cd5\u540d\u5b57\u7a7a\u95f4\uff08namespace\uff09\u91cc\u800c\u4e0d\u662f\u7c7b\u540d\u5b57\u7a7a\u95f4\u91cc\u3002<code>\u65b9\u6cd5\u540d$default$\u5e8f\u53f7<\/code><\/p>\n<p><!--more--><\/p>\n<p>\u53c2\u6570\u4f4d\u7f6e\u8bed\u6cd5\u662f\u6307\u5b9a\u4e49\u53c2\u6570\u7684\u6b21\u5e8f\u548c\u8c03\u7528\u65f6\u4f20\u9012\u7ed9\u53c2\u6570\u7684\u6b21\u5e8f\u4e00\u81f4\u3002\u53ef\u4ee5\u5bf9\u90e8\u5206\u53c2\u6570\u91c7\u7528\u53c2\u6570\u4f4d\u7f6e\u8bed\u6cd5\uff0c\u5bf9\u5176\u4f59\u7684\u91c7\u7528\u547d\u540d\u53c2\u6570\u8bed\u6cd5\u3002<\/p>\n<p>Scala \u4e3a\u53c2\u6570\u540d\u4f7f\u7528\u9759\u6001\u7c7b\u578b\u6216\u53d8\u91cf\uff0c\u4f46\u662f\u5b83\u4f1a\u52a8\u6001\u4fee\u6539\u9ed8\u8ba4\u503c\u3002<strong>\u540d\u5b57\u662f\u9759\u6001\u7ed1\u5b9a\u7684\uff0c\u503c\u662f\u52a8\u6001\u7ed1\u5b9a\u7684<\/strong><\/p>\n<pre><code class=\"scala\">class Parent {\n    def foo(bar: Int = 1, baz: Int = 2): Int = {println(\"super, bar:\" + bar + \", baz:\" + baz); bar + baz}\n}\n\nclass Child extends Parent {\n    override def foo(baz: Int = 3, bar: Int = 4): Int = {println(\"baz:\" + baz + \", bar:\" + bar); super.foo(baz, bar)}\n}\n\nval p = new Parent\n\nprintln(p.foo())  \/\/ 3\n\nval x = new Child\nprintln(x.foo())  \/\/ 7\n\nval y: Parent = new Child\nprintln(y.foo())  \/\/ 7\n\nprintln(x.foo(bar = 1))  \/\/ 4\n\nprintln(y.foo(bar = 5, baz = 6))  \/\/ 11\n\/\/ println(y.foo(5, bar = 6)) \/\/ error: parameter 'bar' is already specified at parameter position 1\n<\/code><\/pre>\n<p>Scala \u91cc\u7684\u547d\u540d\u53c2\u6570\u662f\u6839\u636e\u9759\u6001\u7c7b\u578b\u6765\u51b3\u5b9a\u53c2\u6570\u7684\u987a\u5e8f\u3002<\/p>\n<h2>\u603b\u662f\u6807\u8bb0\u8986\u76d6\uff08override\uff09\u65b9\u6cd5<\/h2>\n<p>override \u5173\u952e\u5b57\u7528\u4e8e\u533a\u5206\u4e00\u4e2a\u65b9\u6cd5\u662f\u5426\u8981\u8986\u76d6\u8fd8\u662f\u91cd\u8f7d\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u7eaf\u62bd\u8c61\u65b9\u6cd5\u662f\u6307\u53ea\u6709\u58f0\u660e\u6ca1\u6709\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002\u7c7b\u9996\u6b21\u5b9a\u4e49\u65f6\uff0cScala \u4e0d\u8981\u6c42\u4f7f\u7528 override \u5173\u952e\u5b57\u3002<\/p>\n<p>\u7c7b\u7ebf\u6027\u5316\u6307\u8c03\u7528\u67d0\u4e2a\u7c7b\u7684\u7236\u7c7b\u65b9\u6cd5\u65f6\u7684\u8c03\u7528\u987a\u5e8f\u3002<\/p>\n<pre><code class=\"scala\">trait Animal {\n    def talk: String\n}\n\ntrait Cat extends Animal {\n    override def talk: String = \"Meow\"\n}\n\ntrait Dog extends Animal {\n    override def talk: String = \"Wookf\"\n}\n\nval kittydoggy = new Cat with Dog\nprintln(kittydoggy.talk)\n\nval kitdog = new Dog with Cat\nprintln(kitdog.talk)\n<\/code><\/pre>\n<p>\u5982\u679c\u4e0d\u5b9e\u7528 override \u5173\u952e\u5b57\uff0c\u5c06\u7f16\u8bd1\u9519\u8bef\uff1a<code>inherits conflicting members:<\/code><\/p>\n<h2>\u5bf9\u671f\u671b\u7684\u4f18\u5316\u8fdb\u884c\u6807\u6ce8<\/h2>\n<p>\u5c3e\u9012\u5f52\u662f\u6307\u65b9\u6cd5\u5728\u6700\u540e\u4e00\u4e2a\u8bed\u53e5\u8c03\u7528\u81ea\u8eab\u3002\u7f16\u8bd1\u5668\u4f1a\u628a\u5c3e\u9012\u5f52\u4f18\u5316\u4e3a\u8fd0\u884c\u65f6\u7684\u5faa\u73af\uff0c\u800c\u4e0d\u662f\u9012\u5f52\u8c03\u7528\u3002\u4f18\u5316\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u907f\u514d\u6808\u6ea2\u51fa\u9519\u8bef\u800c\u4e0d\u662f\u63d0\u9ad8\u901f\u5ea6\u3002<\/p>\n<p>tableswitch \u5b57\u8282\u7801\u662f\u4e00\u79cd\u6bd4\u591a\u91cd\u5206\u652f\u8bed\u53e5\u66f4\u6709\u6548\u7387\u7684\u5206\u652f\u8bed\u53e5\u3002<\/p>\n<p>\u8981\u60f3 Scala \u5e94\u7528 tableswitch \u4f18\u5316\uff0c\u5fc5\u987b\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p>\n<ul>\n<li>\u5339\u914d\u7684\u503c\u5fc5\u987b\u662f\u4e2a\u5df2\u77e5\u7684\u6574\u6570\uff1b<\/li>\n<li>\u6bcf\u4e2a\u5339\u914d\u8bed\u53e5\u90fd\u5fc5\u987b\u201d\u7b80\u5355\u201c\uff0c\u4e0d\u80fd\u5305\u542b\u7c7b\u578b\u68c0\u67e5\u3001if \u8bed\u53e5\u6216\u62bd\u53d6\u5668\u3002\u8868\u8fbe\u5f0f\u7684\u503c\u5fc5\u987b\u5728\u7f16\u8bd1\u671f\u53ef\u83b7\u5f97\u3002<\/li>\n<li>\u5e94\u8be5\u6709\u591a\u4e8e\u4e24\u4e2a\u7684 case \u8bed\u53e5\u3002<\/li>\n<\/ul>\n<pre><code>object TableSwitch {\n    def func(x: Int) = x match {\n        case 1 =&gt; \"one\"\n        case 2 =&gt; \"two\"\n        case z =&gt; z + \"?\"\n    }\n}\n<\/code><\/pre>\n<p>JVM \u7528 instanceof \u5b57\u8282\u7801\u8fdb\u884c\u7c7b\u3001\u7279\u8d28\u548c\u5bf9\u8c61\u7684\u7c7b\u578b\u68c0\u67e5\u3002Scala \u5fc5\u987b\u628a\u57fa\u7840\u7684\u6574\u6570\u6253\u5305\u6210\u6574\u6570\u5bf9\u8c61\u7684\u5f62\u5f0f\u6765\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u8fd9\u662f Scala \u5728 JVM \u4e0a\u5bf9\u4efb\u4f55\u57fa\u7840\u6570\u636e\u7c7b\u578b\u505a\u7c7b\u578b\u68c0\u67e5\u7684\u57fa\u672c\u673a\u5236\u3002<\/p>\n<p>\u8981\u4f18\u5316\u5c3e\u9012\u5f52\u8c03\u7528\uff0cScala \u7f16\u8bd1\u5668\u9700\u8981\u4ee5\u4e0b\u6761\u4ef6\uff1a<\/p>\n<ul>\n<li>\u65b9\u6cd5\u5fc5\u987b\u662f final \u6216\u79c1\u6709\u7684\u3002\u4e0d\u80fd\u591a\u6001\u3002<\/li>\n<li>\u65b9\u6cd5\u5fc5\u987b\u6ce8\u660e\u8fd4\u56de\u7c7b\u578b\u3002<\/li>\n<li>\u65b9\u6cd5\u5fc5\u987b\u5728\u5176\u67d0\u4e2a\u5206\u652f\u7684\u6700\u540e\u4e00\u53e5\u8c03\u7528\u81ea\u8eab\u3002<\/li>\n<\/ul>\n<p>\u8981\u6c42\u5c3e\u9012\u5f52\u4f18\u5316\u7528 <code>@tailrec<\/code>  \u6ce8\u89e3\u5bf9\u65b9\u6cd5\u8fdb\u884c\u6807\u6ce8\u3002<\/p>\n<p>Scala \u7528\u6237\u5e94\u8be5\u786e\u4fdd\uff1a<\/p>\n<ul>\n<li>\u540c\u884c\u5de6\u5927\u62ec\u53f7\uff1b<\/li>\n<li>\u4f7f\u7528\u60ac\u7a7a\u64cd\u4f5c\u7b26\u6216\u62ec\u53f7\uff1b<\/li>\n<li>\u4f7f\u7528\u6709\u610f\u4e49\u7684\u540d\u5b57\uff1b<\/li>\n<li>\u53c2\u6570\u547d\u540d\u4fdd\u6301\u4e00\u81f4<\/li>\n<li>\u603b\u662f\u6807\u8bb0\u8986\u76d6\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\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>\u7f16\u7801\u89c4\u8303\u7684\u76ee\u6807\u53ef\u4ee5\u5f52\u7eb3\u4e3a\u4e09\u7c7b\uff1a \u4ee3\u7801\u53d1\u73b0\uff1a\u4f5c\u7528\u5728\u4e8e\u4f7f\u5de5\u7a0b\u5e08\u80fd\u591f\u8f7b\u677e\u5730\u63a8\u7406\u4ee3\u7801\uff0c\u770b &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/scala\/20150720\/1270\">\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\/1270"}],"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=1270"}],"version-history":[{"count":1,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1270\/revisions"}],"predecessor-version":[{"id":1271,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1270\/revisions\/1271"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}