{"id":565,"date":"2023-02-25T19:10:17","date_gmt":"2023-02-25T11:10:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=565"},"modified":"2023-04-29T20:35:02","modified_gmt":"2023-04-29T12:35:02","slug":"swift-computational-properties-of-class","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-computational-properties-of-class\/","title":{"rendered":"Swift &#8211; \u7c7b\u7684\u8ba1\u7b97\u5c5e\u6027"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h2>\u57fa\u672c\u4f7f\u7528<\/h2>\n<p>Swift\u4e2d\u7c7b\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u5c5e\u6027\uff0c\u5373\u4f7f\u7528get\u548cset\u6765\u95f4\u63a5\u83b7\u53d6\/\u6539\u53d8\u5176\u4ed6\u5c5e\u6027\u7684\u503c\uff0c\u4ee3\u7801\u5982\u4e0b<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">class Calcuator {\n    var a:Int = 1;\n    var b:Int = 1;\n\n    var sum:Int {\n        get{\n            return a + b\n        }\n        set(val) {\n            b = val - a\n        }\n    }\n}\nlet cal = Calcuator();\nprint(cal.sum) \/\/2\ncal.sum = 5\nprint(cal.b) \/\/4<\/code><\/pre>\n<h2>set\u7b80\u5199\u65b9\u5f0f<\/h2>\n<p>\u5bf9\u4e8eset\u6709\u7b80\u5199\u65b9\u6cd5\uff0c\u7b80\u5199\u65f6\uff0c\u65b0\u8d4b\u7684\u503c\u9ed8\u8ba4\u4e3anewValue<\/p>\n<pre><code class=\"language-swift\">class Calcuator {\n    var a:Int = 1;\n    var b:Int = 1;\n\n    var sum:Int {\n        get {\n            return a + b\n        }\n        set {\n            b = newValue - a\n        }\n    }\n}<\/code><\/pre>\n<h2>get\u7b80\u5199\u65b9\u5f0f<\/h2>\n<p>\u5982\u679c\u53ea\u8981get\uff0c\u4e0d\u8981set\u65b9\u6cd5\u65f6\u53ef\u4ee5\u7b80\u5199\u6210\u5982\u4e0b\u4ee3\u7801<\/p>\n<pre><code class=\"language-swift\">class Calcuator {\n    var a:Int = 1;\n    var b:Int = 1;\n\n    var sum:Int {\n        return a + b\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u57fa\u672c\u4f7f\u7528 Swift\u4e2d\u7c7b\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u5c5e\u6027\uff0c\u5373\u4f7f\u7528get\u548cset\u6765\u95f4\u63a5\u83b7\u53d6\/\u6539\u53d8\u5176 [&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-565","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/565","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=565"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/565\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}