{"id":166,"date":"2019-03-14T15:06:13","date_gmt":"2019-03-14T07:06:13","guid":{"rendered":"http:\/\/132.145.126.25\/?p=166"},"modified":"2020-05-25T15:38:52","modified_gmt":"2020-05-25T07:38:52","slug":"class-var-zh","status":"publish","type":"post","link":"https:\/\/www.tsihyoung.com\/index.php\/2019\/03\/14\/class-var-zh\/","title":{"rendered":"\u985e\u8b8a\u6578"},"content":{"rendered":"\n<p>\u5b9a\u7fa9\u5728\u985e\u4e2d\u7684\u8b8a\u6578\u7a31\u70ba\u985e\u8b8a\u6578\u3002\u985e\u8b8a\u6578\u7684\u503c\u53ef\u5728\u5b9a\u7fa9\u6642\u5373\u7d66\u5b9a\uff0c\u4e5f\u53ef\u7528init(_ input: Type)\u521d\u59cb\u5316\u5668\u5728\u751f\u6210\u985e\u5be6\u4f8b\u6642\u5f9e\u8f38\u5165\u4e2d\u7d66\u5b9a\u3002\u524d\u7a2e\u65b9\u6cd5\u6703\u5148\u65bcinit()\u57f7\u884c\uff0c\u5373\u5982\u679cinit()\u4e2d\u5305\u542b\u8a72\u8b8a\u6578\uff0c\u5247\u521d\u59cb\u5316\u5f8c\u8a72\u8b8a\u6578\u70bainit()\u4e2d\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">class test {\n    var a = 0\n    \n    init(_ a: Int) {\n        print(self.a)\n        self.a = a\n    }\n}\n\nprint(test(1).a) \/\/\u8f38\u51fa0 1<\/code><\/pre>\n\n\n\n<p>\u9664\u4e86\u4f7f\u7528init()\uff0c\u4ea6\u53ef\u5148\u5728\u5b9a\u7fa9\u8b8a\u6578\u6642\u5373\u8ce6\u503c\uff0c\u518d\u5728\u51fd\u6578\u4e2d\u7528\u985e\u4f3c\u7684\u65b9\u6cd5\u6539\u8b8a\u985e\u8b8a\u6578\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">class test {\n    var a = 0\n    \n    func assignment() {\n        self.a = 1\n    }\n}\n\nlet c = test()\nc.assignment()\nprint(c.a) \/\/\u8f38\u51fa1<\/code><\/pre>\n\n\n\n<p>\u985e\u4f3c\u65bcinit()\uff0c\u51fd\u6578\u4e5f\u53ef\u4ee5\u628a\u8f38\u5165\u7684\u53c3\u6578\u8ce6\u503c\u7d66\u985e\u8b8a\u6578\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">class test {\n    var a = 0\n    \n    func assignment(_ a: Int) {\n        self.a = a\n        change()\n        print(a)\n    }\n    \n    func change() {\n        a = 3\n    }\n}\n\nlet c = test()\nc.assignment(1) \/\/\u8f38\u51fa1\nprint(c.a) \/\/\u8f38\u51fa3<\/code><\/pre>\n\n\n\n<p>\u4f46\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u51fd\u6578\u7684\u53c3\u6578\u8207\u985e\u8b8a\u6578\u540c\u540d\uff0c\u90a3\u9ebc\u5728\u51fd\u6578\u4e2d\uff0c\u8a72\u8b8a\u6578\u540d\u70ba\u5c40\u57df\u8b8a\u6578\u800c\u975e\u985e\u8b8a\u6578\uff0c\u56e0\u6b64\u60f3\u5728\u8a72\u51fd\u6578\u4e2d\u4f7f\u7528\u985e\u8b8a\u6578\u6642\u9808\u7528self.var\u4f86\u5f15\u7528\u3002\u5373\u5728\u5c40\u57df\u8b8a\u6578\u4f5c\u7528\u57df\u5167\uff0c\u5c40\u57df\u8b8a\u6578\u6703\u4ee3\u66ff\u540c\u540d\u5168\u5c40\u8b8a\u6578\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u7fa9\u5728\u985e\u4e2d\u7684\u8b8a\u6578\u7a31\u70ba\u985e\u8b8a\u6578\u3002\u985e\u8b8a\u6578\u7684\u503c\u53ef\u5728\u5b9a\u7fa9\u6642\u5373\u7d66\u5b9a\uff0c\u4e5f\u53ef\u7528init(_ input: Type)\u521d\u59cb\u5316\u5668\u5728\u751f\u6210\u985e\u5be6\u4f8b\u6642\u5f9e\u8f38\u5165\u4e2d\u7d66\u5b9a\u3002\u524d\u7a2e\u65b9\u6cd5\u6703\u5148\u65bcinit()\u57f7\u884c\uff0c\u5373\u5982\u679cinit()\u4e2d\u5305\u542b\u8a72\u8b8a\u6578\uff0c\u5247\u521d\u59cb\u5316\u5f8c\u8a72\u8b8a\u6578\u70bainit()\u4e2d\u7684\u503c\u3002 \u9664\u4e86\u4f7f\u7528init()\uff0c\u4ea6\u53ef\u5148\u5728\u5b9a\u7fa9\u8b8a\u6578\u6642\u5373\u8ce6\u503c\uff0c\u518d\u5728\u51fd\u6578\u4e2d\u7528\u985e\u4f3c\u7684\u65b9\u6cd5\u6539\u8b8a\u985e\u8b8a\u6578\u7684\u503c\u3002 \u985e\u4f3c\u65bcinit()\uff0c\u51fd\u6578\u4e5f\u53ef\u4ee5\u628a\u8f38\u5165\u7684\u53c3\u6578\u8ce6\u503c\u7d66\u985e\u8b8a\u6578\u3002 \u4f46\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u51fd\u6578\u7684\u53c3\u6578\u8207\u985e\u8b8a\u6578\u540c\u540d\uff0c\u90a3\u9ebc\u5728\u51fd\u6578\u4e2d\uff0c\u8a72\u8b8a\u6578\u540d\u70ba\u5c40\u57df\u8b8a\u6578\u800c\u975e\u985e\u8b8a\u6578\uff0c\u56e0\u6b64\u60f3\u5728\u8a72\u51fd\u6578\u4e2d\u4f7f\u7528\u985e\u8b8a\u6578\u6642\u9808\u7528self.var\u4f86\u5f15\u7528\u3002\u5373\u5728\u5c40\u57df\u8b8a\u6578\u4f5c\u7528\u57df\u5167\uff0c\u5c40\u57df\u8b8a\u6578\u6703\u4ee3\u66ff\u540c\u540d\u5168\u5c40\u8b8a\u6578\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"post_formats":[],"languages":[6],"post_translationss":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-programme"],"guten_post_layout_featured_media_urls":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"guten_post_layout_landscape_large":false,"guten_post_layout_portrait_large":false,"guten_post_layout_square_large":false,"guten_post_layout_landscape":false,"guten_post_layout_portrait":false,"guten_post_layout_square":false,"sciencexlite-blog-thumb":false},"_links":{"self":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":0,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/tags?post=166"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/post_formats?post=166"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/languages?post=166"},{"taxonomy":"post_translations","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/post_translationss?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}