{"id":3913,"date":"2020-07-15T16:10:49","date_gmt":"2020-07-15T08:10:49","guid":{"rendered":"https:\/\/www.webteach.tw\/?p=3913"},"modified":"2020-07-15T16:10:49","modified_gmt":"2020-07-15T08:10:49","slug":"protected-overwrite","status":"publish","type":"post","link":"https:\/\/www.webteach.tw\/?p=3913","title":{"rendered":"[ PHP ] \u2013 OOP 7 \u7269\u4ef6 \u7e7c\u627f\u5f8c \u7684 protected \u4fdd\u8b77 \u8207 overwrite \u8986\u8f09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u518d\u4f86\u4f60\u6703\u767c\u73fe\u7e7c\u627f\u6703\u767c\u751f\u4e00\u4e9b\u4f60\u6c92\u60f3\u5230\u7684\u4e8b\u60c5\uff0c\u4e0d\u662f\u8aaa\u597d\u7238\u7238\u7684\u9322\u53ef\u4ee5\u5152\u5b50\u7528\u55ce\uff1f\u4f46\u4f60\u5fd8\u8a18\u554f\u8001\u7238\u4fdd\u96aa\u7bb1\u7684\u5bc6\u78bc\u662f\u591a\u5c11\u4e86\uff1b\u600e\u9ebc\u628a\u9322\u62ff\u51fa\u4f86\u82b1\u3002\u5b50\u627f\u7236\u696d\u5f8c\u7b2c\u4e8c\u4ee3\u901a\u5e38\u6703\u6539\u8b8a\u4e00\u524d\u4e00\u4ee3\u7684\u4e00\u4e9b\u505a\u6cd5\uff0c\u7238\u7238\u5728\u5df7\u53e3\u958b\u6c34\u679c\u6524\uff0c\u5152\u5b50\u7e7c\u627f\u5f8c\u7576\u7136\u8981\u641e\u9ede\u4e0d\u4e00\u6a23\u7684\u5728\u7db2\u8def\u4e0a\u8ce3\u6c34\u679c\u624d\u53eb\u6f6e\u3002<\/p>\n\n\n<pre class=\"wp-block-syntaxhighlighter-code\">\n\/\/extends \u7e7c\u627f \u4f60\u53ef\u4ee5\u60f3\u6210\u907a\u50b3 \u4f60\u7238\u7238\u6709\u7684\u4e00\u4e9b\u7279\u5fb5 \u5728\u4f60\u7684\u8eab\u4e0a\u4e5f\u6703\u770b\u5230\uff0c\u4f46\u53c8\u6c92\u6709\u5b8c\u5168\u4e00\u6a23(\u53c8\u4e0d\u662f\u8907\u88fd\u4eba)\n\/\/protected \u4fdd\u8b77 \u751f\u6210\u985e\u5225\u5f8c \u53ea\u53ef\u4ee5\u5728 \u985e\u5225\u7684\u5167\u90e8 \u6216\u662f \u7e7c\u627f\u985e\u5225\u4e2d \u5beb\u5165\u8b80\u53d6\u5c6c\u6027\u7684\u503c\nclass People {\n\tprivate $height;\/\/\u8eab\u9ad8\n\tprivate $weight;\/\/\u9ad4\u91cd\n\tprotected $sex;\/\/\u59d3\u5225 \u8981\u6539\u6210 \u4fdd\u8b77 \u5b50\u985e\u5225\u624d\u53ef\u4ee5\u8b8a\u66f4\u5b83\u7684\u503c\n\tfunction set_height($height1) { $this->height = $height1;}\n\tfunction set_weight($weight1) {$this->weight = $weight1;}\n\tfunction set_sex($sex1) {$this->sex = $sex1;}\n\tfunction get_height() {return $this->height;}\n\tfunction get_weight() {return $this->weight;}\n\tfunction get_sex() {return $this->sex;}\n}\nclass People2 extends People { \/\/People2 \u7e7c\u627f\u4e86 people \u5167\u6240\u6709\u7684\u5c6c\u6027\u8ddf\u65b9\u6cd5\n\tprivate $action;\/\/\u52d5\u4f5c\n\tfunction set_action($action1) {$this->action = $action1;}\n\tfunction get_action() {return $this->action;}\n\t\/\/overwrite \u8986\u8f09 \u5c31\u662f\u91cd\u5beb\u7236\u985e\u5225\u5167\u5df2\u7d93\u5b58\u5728\u7684\u65b9\u6cd5\n\tfunction get_sex() {return ' \u9019\u662f\u5bc6\u79d8\u4e0d\u544a\u8a34\u4f60 ';}\n}\n$jeff = new People2();\n$jeff->set_height(170);\n$jeff->set_weight(65);\n$jeff->set_sex('male');\n$jeff->set_action('sit');\necho 'jeff \u7684\u8eab\u9ad8\u662f\uff1a';\necho $jeff->get_height();\necho '&lt;br>jeff \u7684\u9ad4\u91cd\u662f\uff1a';\necho $jeff->get_weight();\necho '&lt;br>jeff \u7684\u59d3\u5225\u662f\uff1a';\necho $jeff->get_sex();\necho '&lt;br>jeff \u7684\u52d5\u4f5c\u662f\uff1a';\necho $jeff->get_action();\n\/\/\u986f\u793a\u7d50\u679c\n\/*\njeff \u7684\u8eab\u9ad8\u662f\uff1a170\njeff \u7684\u9ad4\u91cd\u662f\uff1a65\njeff \u7684\u59d3\u5225\u662f\uff1a \u9019\u662f\u5bc6\u79d8\u4e0d\u544a\u8a34\u4f60\njeff \u7684\u52d5\u4f5c\u662f\uff1asit\n*\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u518d\u4f86\u4f60\u6703\u767c\u73fe\u7e7c\u627f\u6703\u767c\u751f\u4e00\u4e9b\u4f60\u6c92\u60f3\u5230\u7684\u4e8b\u60c5\uff0c\u4e0d\u662f\u8aaa\u597d\u7238\u7238\u7684\u9322\u53ef\u4ee5\u5152\u5b50\u7528\u55ce\uff1f\u4f46\u4f60\u5fd8\u8a18\u554f\u8001\u7238\u4fdd\u96aa\u7bb1\u7684\u5bc6\u78bc\u662f\u591a\u5c11\u4e86\uff1b\u600e\u9ebc\u628a\u9322\u62ff\u51fa\u4f86\u82b1\u3002\u5b50\u627f\u7236\u696d\u5f8c\u7b2c\u4e8c\u4ee3\u901a\u5e38\u6703\u6539\u8b8a\u4e00\u524d\u4e00\u4ee3\u7684\u4e00\u4e9b\u505a\u6cd5\uff0c\u7238\u7238\u5728\u5df7\u53e3\u958b\u6c34\u679c\u6524\uff0c\u5152\u5b50\u7e7c\u627f\u5f8c\u7576\u7136\u8981\u641e\u9ede\u4e0d\u4e00\u6a23\u7684\u5728\u7db2\u8def\u4e0a\u8ce3\u6c34\u679c\u624d\u53eb\u6f6e\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":[429,435,448,472,664,1115,1163],"class_list":["post-3913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop","category-php","tag-oop","tag-overwrite","tag-php","tag-protected","tag-664","tag-1115","tag-1163","hvn-theme-has-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/3913","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=3913"}],"version-history":[{"count":0,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=\/wp\/v2\/posts\/3913\/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=3913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webteach.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}