\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}]}}