{"id":997,"date":"2014-08-13T22:42:38","date_gmt":"2014-08-13T14:42:38","guid":{"rendered":"http:\/\/coderbee.net\/?p=997"},"modified":"2014-08-26T22:57:00","modified_gmt":"2014-08-26T14:57:00","slug":"scala-trait-%e5%a0%86%e5%8f%a0%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/scala\/20140813\/997","title":{"rendered":"Scala Trait \u5806\u53e0\u7279\u6027"},"content":{"rendered":"<p>\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5c55\u793a Scala Trait \u7ebf\u6027\u5806\u53e0\u7684\u7279\u6027\uff1a<\/p>\n<pre><code class=\"scala\">package net.coderbee.scala\n\ntrait BaseTrait {\n    def action\n}\n\ntrait Trait0 extends BaseTrait {\n    abstract override def action {\n        println(\"action at Trait0\")\n        super.action\n    }\n}\n\ntrait Trait1 extends BaseTrait {\n    abstract override def action() {\n        println(\"action at trait1\")\n        super.action \/\/ \u5982\u679c\u67d0\u4e2aTrait\u4e0d\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u5219\u4f1a\u4e2d\u65adTrait\u7684\u8c03\u7528\u6808\uff0c\u8fd9\u53ef\u7528\u4ee5\u5b9e\u73b0\u8fc7\u6ee4\n        println(\"rollback at action at trait1\")\n    }\n}\n\ntrait Trait2 extends BaseTrait {\n    abstract override def action() {\n        println(\"action at trait2\")\n        super.action\n    }\n}\n\ntrait BreakTrait extends BaseTrait {\n    abstract override def action {\n        println(\"i am break trait, end here \")\n    }\n}\n\nclass ClassWithTrait extends BaseTrait {\n    def action() {\n        println(\"action at ClassWithTrait\")\n    }\n}\n\nobject StackTrait {\n\n    def main(args: Array[String]) {\n        val tr012 = new ClassWithTrait with Trait2 with Trait1 with Trait0\n        tr012.action \/\/ \u4ece\u53f3\u5f80\u5de6\u8c03\u7528 trait \u7684\u62bd\u8c61\u5b9e\u73b0\uff0c\u6700\u540e\u8c03\u7528\u672c\u7c7b\u7684\u3002\u5982\u679c\u4e2d\u95f4\u67d0\u4e2a\u62bd\u8c61\u65b9\u6cd5\u6ca1\u6709\u8c03\u7528\u8d85\u7c7b\u7684  super \u65b9\u6cd5\uff0c\u5219\u4e0d\u4f1a\u8c03\u7528\u5230\u672c\u7c7b\n\n        println\n        val tr02 = new ClassWithTrait with Trait2 with Trait0\n        tr02.action\n\n        println\n        val trBreak = new ClassWithTrait with Trait2 with BreakTrait with Trait1 with Trait0\n        trBreak.action\n    }\n}\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<p><!--more--><\/p>\n<pre><code>action at Trait0\naction at trait1\naction at trait2\naction at ClassWithTrait\nrollback at action at trait1\n\naction at Trait0\naction at trait2\naction at ClassWithTrait\n\naction at Trait0\naction at trait1\ni am break trait, end here \nrollback at action at trait1\n<\/code><\/pre>\n<p>\u4ece\u8f93\u51fa\u53ef\u4ee5\u770b\u5230\uff1a<\/p>\n<ol>\n<li>\u5bf9 trait \u7684\u62bd\u8c61\u65b9\u6cd5\u7684\u8c03\u7528\u662f \u4ece\u53f3\u5f80\u5de6\u8c03\u7528\uff0c\u5728\u4ece\u5de6\u5f80\u53f3\u8fd4\u56de\uff1b<\/li>\n<li>\u5982\u679c\u4e2d\u95f4\u67d0\u4e2a trait \u4e2d\u65ad\u4e86\u8c03\u7528\uff0c\u5219\u4ece\u8be5 trait \u5411\u5de6\u8fd4\u56de\u3002<\/li>\n<\/ol>\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>\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5c55\u793a Scala Trait \u7ebf\u6027\u5806\u53e0\u7684\u7279\u6027\uff1a package  &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/scala\/20140813\/997\">\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,182],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/997"}],"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=997"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/997\/revisions"}],"predecessor-version":[{"id":999,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/997\/revisions\/999"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}