{"id":3942,"date":"2020-07-16T16:34:19","date_gmt":"2020-07-16T08:34:19","guid":{"rendered":"https:\/\/www.webteach.tw\/?p=3942"},"modified":"2020-07-16T16:34:19","modified_gmt":"2020-07-16T08:34:19","slug":"%e5%b0%81%e8%a3%9d-encapsulation","status":"publish","type":"post","link":"https:\/\/www.webteach.tw\/?p=3942","title":{"rendered":"[ PHP ] &#8211; OOP 11 \u5c01\u88dd Encapsulation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u7269\u4ef6\u5c0e\u5411\u7684\u4e00\u5927\u7279\u6027\u5c31\u662f \u5c01\u88dd Encapsulation \uff0c\u4f60\u53ea\u8981\u751f\u6210\u985e\u5225 \u7136\u5f8c \u50b3\u53c3\u6578\uff0c\u4e4b\u5f8c\u5c31\u7b49\u98ef\u5403\u5c31\u597d\u4e86\uff0c\u7d30\u7bc0\u5c31\u4ea4\u7d66\u5225\u4eba\u53bb\u505a\u5427\uff0c\u7701\u4e8b\u53c8\u4e0d\u6703\u5bb9\u6613\u51fa\u932f\u3002<\/p>\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">\nclass ck {\n\tprivate $budget;\/\/\u9810\u7b97\n  \tprivate $buy_status = false;\/\/\u83dc\u8cb7\u4e86\u55ce\uff1f\n  \tprivate $preapare_status = false;\/\/\u5099\u597d\u83dc\u4e86\u55ce\uff1f\n  \tprivate $cooking_tatus = false;\/\/\u716e\u597d\u83dc\u4e86\u55ce\uff1f\n  \tprivate function buy(){ \/\/\u6700\u5c11\u8981\u6709500\u584a\u624d\u5920\u8cb7\u83dc\n\t\treturn $this->buy_status = $this->budget > 500 ? true : false;\n\t}\n\tprivate function preapare(){ \/\/\u5099\u6599 \u8981\u5148\u8cb7\u597d\u83dc\u624d\u80fd\u5920\u6d17\u83dc \u5207\u83dc \u505a\u5099\u6599\u7684\u52d5\u4f5c\n\t\treturn $this->preapare_status = $this->buy_status ? true : false;\n\t}\n\tprivate function cooking(){ \/\/\u716e\u98ef \u8981\u5148\u5099\u6599\u597d\u624d\u80fd\u5920\u716e\n\t\treturn $this->cooking_tatus = $this->preapare_status ? true : false;\n\t}\n\tpublic function serve($budget){ \/\/\u4e0a\u83dc \u4f60\u53ea\u80fd\u547c\u53eb\u9019\u500b \u56e0\u70ba\u5b83\u662f\u516c\u958b\u7684\u65b9\u6cd5 \u5176\u5b83\u7684\u90fd\u4e0d\u5141\u8a31 \u5728\u985e\u5225\u4e4b\u5916\u547c\u53eb \u9054\u5230\u4fdd\u8b77\u8cc7\u6599\u78ba\u4fdd\u6d41\u7a0b\u6b63\u78ba\n\t\t$this->budget = $budget;\n\t\techo '&lt;br>'.( $this->buy() ? '\u8cb7\u597d\u83dc\u4e86' : '\u4e0d\u5920\u9322');\n\t\techo '&lt;br>'.( $this->preapare() ? '\u5099\u597d\u83dc\u4e86' : '\u9084\u6c92\u8cb7\u83dc');\n\t\techo '&lt;br>'.( $this->cooking() ? '\u716e\u597d\u83dc\u4e86' : '\u9084\u6c92\u5099\u83dc');\n\t\techo '&lt;br>'.( $this->cooking_tatus ? '\u4e0a\u83dc\u56c9\uff01' : '\u9084\u6c92\u716e\u597d\u54e6\uff01');\n\t}\n}\n\/\/\u5c01\u88dd\u7684\u597d\u8655 \u4e0a\u83dc\u7684\u9806\u5e8f \u62ff\u9322 \u8cb7\u83dc \u5099\u83dc \u716e\u83dc \u6700\u5f8c\u624d\u80fd\u4e0a\u83dc\n$c = new ck();\n$c->serve(600);\/\/\u6211\u5011\u53ea\u8981\u547c\u53eb\u4e0a\u83dc \u8ddf \u7d66\u9322 \u524d\u7f6e\u4f5c\u696d\u7684\u7d30\u7bc0\u8ddf\u6d41\u7a0b\u4e0d\u7528\u7ba1\u4ed6\uff0c\u5361\u5728\u90a3\u500b\u968e\u6bb5\u4e5f\u6703\u8ddf\u4f60\u8aaa\u660e\necho '&lt;br>';\nclass ck1 { \/\/ \u6211\u5011\u628a\u6240\u6709\u7684\u5c6c\u6027\u65b9\u6cd5\u90fd\u8a2d\u516c\u958b\n\tpublic $budget;\/\/\u9810\u7b97\n  \tpublic $buy_status = false;\/\/\u83dc\u8cb7\u4e86\u55ce\uff1f\n  \tpublic $preapare_status = false;\/\/\u5099\u597d\u83dc\u4e86\u55ce\uff1f\n  \tpublic $cooking_tatus = false;\/\/\u716e\u597d\u83dc\u4e86\u55ce\uff1f\n  \tpublic function buy(){ \/\/\u6700\u5c11\u8981\u6709500\u584a\u624d\u5920\u8cb7\u83dc\n\t\treturn $this->buy_status = $this->budget > 500 ? true : false;\n\t}\n\tpublic function preapare(){ \/\/\u5099\u6599 \u8981\u5148\u8cb7\u597d\u83dc\u624d\u80fd\u5920\u6d17\u83dc \u5207\u83dc \u505a\u5099\u6599\u7684\u52d5\u4f5c\n\t\treturn $this->preapare_status = $this->buy_status ? true : false;\n\t}\n\tpublic function cooking(){ \/\/\u716e\u98ef \u8981\u5148\u5099\u6599\u597d\u624d\u80fd\u5920\u716e\n\t\treturn $this->cooking_tatus = $this->preapare_status ? true : false;\n\t}\n\tpublic function serve(){ \/\/\u4e0a\u83dc\n\t\techo '&lt;br>'.( $this->cooking_tatus ? '\u4e0a\u83dc\u56c9\uff01' : '\u9084\u6c92\u716e\u597d\u54e6\uff01');\n\t}\n}\n$a = new ck1();\/\/\u4f60\u5c31\u8981\u8f9b\u82e6\u7684\u81ea\u5df2 \u4e00\u500b\u4e00\u500b\u7d66\u503c \u7136\u5f8c\u7167\u9806\u5e8f\u7684\u547c\u53eb\u65b9\u6cd5 \u4e0d\u7136\u4f60\u6c38\u9060\u5403\u4e0d\u4e86\u98ef\n$a->budget=600;\n$a->buy();\n$a->preapare();\n$a->cooking();\n$a->serve();\necho '&lt;br>';\n$b = new ck1();\n$b->budget=600;\n$b->preapare();\/\/\u600e\u9ebc\u9084\u6c92\u8cb7\u83dc\u5c31\u5148\u5099\u83dc\uff1f\n$b->buy();\n$b->cooking();\n$b->serve();\/\/\u770b\u5427\uff01\u4f60\u6c38\u9060\u4e5f\u5403\u4e0d\u4e86\u98ef\u4e86\n\/\/\u986f\u793a\u7d50\u679c\n\/*\n\u8cb7\u597d\u83dc\u4e86\n\u5099\u597d\u83dc\u4e86\n\u716e\u597d\u83dc\u4e86\n\u4e0a\u83dc\u56c9\uff01\n\u4e0a\u83dc\u56c9\uff01\n\u9084\u6c92\u716e\u597d\u54e6\uff01\n*\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7269\u4ef6\u5c0e\u5411\u7684\u4e00\u5927\u7279\u6027\u5c31\u662f \u5c01\u88dd Encapsulation \uff0c\u4f60\u53ea\u8981\u751f\u6210\u985e\u5225 \u7136\u5f8c \u50b3\u53c3\u6578\uff0c\u4e4b\u5f8c\u5c31\u7b49\u98ef\u5403\u5c31\u597d\u4e86\uff0c\u7d30\u7bc0\u5c31\u4ea4\u7d66\u5225\u4eba\u53bb\u505a\u5427\uff0c\u7701\u4e8b\u53c8\u4e0d\u6703\u5bb9\u6613\u51fa\u932f\u3002<\/p>\n","protected":false},"author":1,"featured_media":2201,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,36],"tags":[238,429,448,806],"class_list":["post-3942","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop","category-php","tag-encapsulation","tag-oop","tag-php","tag-806","hvn-theme-has-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/3942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3942"}],"version-history":[{"count":0,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/3942\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/media\/2201"}],"wp:attachment":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}