{"id":570,"date":"2023-02-25T19:13:37","date_gmt":"2023-02-25T11:13:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=570"},"modified":"2023-04-29T20:33:39","modified_gmt":"2023-04-29T12:33:39","slug":"swift-protocol","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-protocol\/","title":{"rendered":"Swift &#8211; \u534f\u8bae\uff08protocol\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h2>\u534f\u8bae\u57fa\u672c\u4f7f\u7528<\/h2>\n<p><!-- more --><\/p>\n<p>Swift\u4e2d\u534f\u8bae\u7c7b\u4f3c\u4e8eJava\u4e2d\u7684\u63a5\u53e3\uff0c\u534f\u8bae\u91cc\u53ea\u505a\u65b9\u6cd5\u7684\u58f0\u660e\uff0c\u5305\u62ec\u65b9\u6cd5\u540d\u3001\u8fd4\u56de\u503c\u3001\u53c2\u6570\u7b49\u4fe1\u606f\uff0c\u800c\u6ca1\u6709\u5177\u4f53\u7684\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<pre><code class=\"language-swift\">protocol Person {\n    \/\/\u8bfb\u5199\u5c5e\u6027\n    var name:String{get set}\n\n    \/\/\u53ea\u8bfb\u5c5e\u6027\n    var age:Int{get}\n\n    \/\/\u7c7b\u578b\u65b9\u6cd5\n    static func method1()\n\n    \/\/\u5b9e\u4f8b\u65b9\u6cd5\n    func method2() -&gt; Int\n\n    \/\/\u7a81\u53d8\u65b9\u6cd5\n    mutating func method3()\n}<\/code><\/pre>\n<h2>\u534f\u8bae\u7ee7\u627f<\/h2>\n<p>\u534f\u8bae\u53ef\u4ee5\u7ee7\u627f\u53e6\u4e00\u4e2a\u534f\u8bae<\/p>\n<pre><code class=\"language-swift\">protocol Animal {\n    func move()\n}\n\nprotocol Bird:Animal {\n    func song()\n}\n\nclass Chiken:Bird {\n    func song() {\n        print(&quot;\u6bcd\u9e21\u54af\u54af&quot;)\n    }\n\n    func move() {\n        print(&quot;\u6bcd\u9e21\u8d70&quot;)\n    }\n}<\/code><\/pre>\n<h2>\u591a\u534f\u8bae\u5b9e\u73b0<\/h2>\n<p>\u5982\u679c\u67d0\u4e2a\u7c7b\u96c6\u7ee7\u627f\u4e86\u67d0\u4e2a\u7236\u7c7b\uff0c\u53c8\u9075\u5faa\u4e86\u67d0\u4e2a\u534f\u8bae\uff0c\u90a3\u4e48\u5192\u53f7\u540e\u9762\u5e94\u8be5\u5148\u5199\u7236\u7c7b\uff0c\u518d\u5199\u534f\u8bae<\/p>\n<pre><code class=\"language-swift\">class CC:\u7ee7\u627f\u7684\u7236\u7c7b, \u534f\u8bae1, \u534f\u8bae2 {\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u534f\u8bae\u57fa\u672c\u4f7f\u7528 Swift\u4e2d\u534f\u8bae\u7c7b\u4f3c\u4e8eJava\u4e2d\u7684\u63a5\u53e3\uff0c\u534f\u8bae\u91cc\u53ea\u505a\u65b9\u6cd5\u7684\u58f0\u660e\uff0c\u5305\u62ec [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}