{"id":201786,"date":"2020-10-05T09:49:47","date_gmt":"2020-10-05T01:49:47","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=201786"},"modified":"2020-09-24T11:50:25","modified_gmt":"2020-09-24T03:50:25","slug":"learn-scala-trait","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/learn-scala-trait.html","title":{"rendered":"\u4e00\u8d77\u5b66Scala Trait(\u7279\u5f81)"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nScala \u662f\u4e00\u95e8\u591a\u8303\u5f0f\uff08multi-paradigm\uff09\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u8bbe\u8ba1\u521d\u8877\u662f\u8981\u96c6\u6210\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5404\u79cd\u7279\u6027\u3002
\nScala \u8fd0\u884c\u5728Java\u865a\u62df\u673a\u4e0a\uff0c\u5e76\u517c\u5bb9\u73b0\u6709\u7684Java\u7a0b\u5e8f\u3002
\nScala \u6e90\u4ee3\u7801\u88ab\u7f16\u8bd1\u6210Java\u5b57\u8282\u7801\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8fd0\u884c\u4e8eJVM\u4e4b\u4e0a\uff0c\u5e76\u53ef\u4ee5\u8c03\u7528\u73b0\u6709\u7684Java\u7c7b\u5e93\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nScala Trait(\u7279\u5f81) \u76f8\u5f53\u4e8e Java \u7684\u63a5\u53e3\uff0c\u5b9e\u9645\u4e0a\u5b83\u6bd4\u63a5\u53e3\u8fd8\u529f\u80fd\u5f3a\u5927\u3002
\n\u4e0e\u63a5\u53e3\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002
\n\u4e00\u822c\u60c5\u51b5\u4e0bScala\u7684\u7c7b\u53ea\u80fd\u591f\u7ee7\u627f\u5355\u4e00\u7236\u7c7b\uff0c\u4f46\u662f\u5982\u679c\u662f Trait(\u7279\u5f81) \u7684\u8bdd\u5c31\u53ef\u4ee5\u7ee7\u627f\u591a\u4e2a\uff0c\u4ece\u7ed3\u679c\u6765\u770b\u5c31\u662f\u5b9e\u73b0\u4e86\u591a\u91cd\u7ee7\u627f\u3002
\nTrait(\u7279\u5f81) \u5b9a\u4e49\u7684\u65b9\u5f0f\u4e0e\u7c7b\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4f7f\u7528\u7684\u5173\u952e\u5b57\u662f trait\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\ntrait Equal {\r\n  def isEqual(x: Any): Boolean\r\n  def isNotEqual(x: Any): Boolean = !isEqual(x)\r\n}<\/pre>\n

\u4ee5\u4e0aTrait(\u7279\u5f81)\u7531\u4e24\u4e2a\u65b9\u6cd5\u7ec4\u6210\uff1aisEqual \u548c isNotEqual\u3002isEqual \u65b9\u6cd5\u6ca1\u6709\u5b9a\u4e49\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0cisNotEqual\u5b9a\u4e49\u4e86\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002\u5b50\u7c7b\u7ee7\u627f\u7279\u5f81\u53ef\u4ee5\u5b9e\u73b0\u672a\u88ab\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002\u6240\u4ee5\u5176\u5b9e Scala Trait(\u7279\u5f81)\u66f4\u50cf Java \u7684\u62bd\u8c61\u7c7b\u3002<\/p>\n

\u4ee5\u4e0b\u6f14\u793a\u4e86\u7279\u5f81\u7684\u5b8c\u6574\u5b9e\u4f8b\uff1a<\/p>\n

\r\ntrait Equal {\r\n  def isEqual(x: Any): Boolean\r\n  def isNotEqual(x: Any): Boolean = !isEqual(x)\r\n}\r\n\r\nclass Point(xc: Int, yc: Int) extends Equal {\r\n  var x: Int = xc\r\n  var y: Int = yc\r\n  def isEqual(obj: Any) =\r\n    obj.isInstanceOf[Point] &&\r\n    obj.asInstanceOf[Point].x == x\r\n}\r\n\r\nobject Test {\r\n   def main(args: Array[String]) {\r\n      val p1 = new Point(2, 3)\r\n      val p2 = new Point(2, 4)\r\n      val p3 = new Point(3, 3)\r\n\r\n      println(p1.isNotEqual(p2))\r\n      println(p1.isNotEqual(p3))\r\n      println(p1.isNotEqual(2))\r\n   }\r\n}<\/pre>\n

\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\n$ scalac Test.scala \r\n$ scala Test\r\nfalse\r\ntrue\r\ntrue<\/pre>\n
\u7279\u5f81\u6784\u9020\u987a\u5e8f<\/strong><\/div>\n

\u7279\u5f81\u4e5f\u53ef\u4ee5\u6709\u6784\u9020\u5668\uff0c\u7531\u5b57\u6bb5\u7684\u521d\u59cb\u5316\u548c\u5176\u4ed6\u7279\u5f81\u4f53\u4e2d\u7684\u8bed\u53e5\u6784\u6210\u3002\u8fd9\u4e9b\u8bed\u53e5\u5728\u4efb\u4f55\u6df7\u5165\u8be5\u7279\u5f81\u7684\u5bf9\u8c61\u5728\u6784\u9020\u65f6\u90fd\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n

\u6784\u9020\u5668\u7684\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n

    \n
  • 1.\u8c03\u7528\u8d85\u7c7b\u7684\u6784\u9020\u5668\uff1b<\/li>\n
  • 2.\u7279\u5f81\u6784\u9020\u5668\u5728\u8d85\u7c7b\u6784\u9020\u5668\u4e4b\u540e\u3001\u7c7b\u6784\u9020\u5668\u4e4b\u524d\u6267\u884c\uff1b<\/li>\n
  • 3.\u7279\u5f81\u7531\u5de6\u5230\u53f3\u88ab\u6784\u9020\uff1b<\/li>\n
  • 4.\u6bcf\u4e2a\u7279\u5f81\u5f53\u4e2d\uff0c\u7236\u7279\u5f81\u5148\u88ab\u6784\u9020\uff1b<\/li>\n
  • 5.\u5982\u679c\u591a\u4e2a\u7279\u5f81\u5171\u6709\u4e00\u4e2a\u7236\u7279\u5f81\uff0c\u7236\u7279\u5f81\u4e0d\u4f1a\u88ab\u91cd\u590d\u6784\u9020<\/li>\n
  • 6.\u6240\u6709\u7279\u5f81\u88ab\u6784\u9020\u5b8c\u6bd5\uff0c\u5b50\u7c7b\u88ab\u6784\u9020\u3002<\/li>\n<\/ul>\n

    \u6784\u9020\u5668\u7684\u987a\u5e8f\u662f\u7c7b\u7684\u7ebf\u6027\u5316\u7684\u53cd\u5411\u3002\u7ebf\u6027\u5316\u662f\u63cf\u8ff0\u67d0\u4e2a\u7c7b\u578b\u7684\u6240\u6709\u8d85\u7c7b\u578b\u7684\u4e00\u79cd\u6280\u672f\u89c4\u683c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    Scala Trait(\u7279\u5f81) \u76f8\u5f53\u4e8e Java \u7684\u63a5\u53e3\uff0c\u5b9e\u9645\u4e0a\u5b83\u6bd4\u63a5\u53e3\u8fd8\u529f\u80fd\u5f3a\u5927\u3002 \u4e0e\u63a5\u53e3\u4e0d\u540c\u7684\u662f\uff0c\u5b83\u8fd8\u53ef\u4ee5 […]<\/p>\n","protected":false},"author":1470,"featured_media":201759,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-201786","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\/201786","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\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=201786"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201786\/revisions"}],"predecessor-version":[{"id":201793,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201786\/revisions\/201793"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/201759"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=201786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=201786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=201786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}