{"id":255,"date":"2018-10-09T22:40:59","date_gmt":"2018-10-09T14:40:59","guid":{"rendered":"https:\/\/develop-note.com\/blog\/?p=255"},"modified":"2022-02-16T22:18:57","modified_gmt":"2022-02-16T14:18:57","slug":"2019ironman-flask-restful-day08","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2018\/10\/09\/2019ironman-flask-restful-day08\/","title":{"rendered":"\u7b2c\u5341\u5c46\u9435\u4eba\u8cfd flask-restful DAY08-\u641e\u61c2Python\u7684OOP"},"content":{"rendered":"<h1>\u4ec0\u9ebc\u662f<a href=\"https:\/\/zh.m.wikipedia.org\/zh-tw\/\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\" rel=\"nofollow noopener\" target=\"_blank\">OOP<\/a><\/h1>\n<p>\u76f8\u4fe1\u8edf\u9ad4\u958b\u767c\u5de5\u7a0b\u5e2b\u5c0d\u65bc\u9019\u8a5e\u61c9\u8a72\u4e0d\u964c\u751f\uff0c\u6240\u4ee5\u6211\u5c31\u591a\u505a\u8d05\u8ff0\u4e86\uff0c\u6211\u53ea\u63d0\u53ca\u5176\u91cd\u8981\u7684\u56db\u500b\u89c0\u5ff5\u5728Python\u5982\u4f55\u5be6\u4f5c\uff1a<!--more--><\/p>\n<h2>\u7e7c\u627f<\/h2>\n<p>\u7e7c\u627f\u7684\u8a9e\u6cd5\u76f8\u8f03\u4e4b\u4e0b\u6bd4\u8f03\u7c21\u55ae\u8acb\u5404\u4f4d\u8b80\u8005\u770b\u4ee5\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">class Animal:\n    name = &#039;&#039;\n\n    def __init__(self, name):\n        self.name = name\n\n    def walk(self):\n        print(&#039;walking&#039;)\n\n    def eat(self):\n        print(&#039;eating&#039;)\n\nclass Dog(Animal):\n    def __init__(self, name):\n        super().__init__(name)\n\n    def walk(self):\n        print(&#039;{0} using foot walk&#039;.format(self.name))\n\n    def eat(self):\n        print(&#039;{0} eat bone&#039;.format(self.name))\n\nanimal = Dog(&#039;A&#039;)\nanimal.eat()\nanimal.walk()<\/code><\/pre>\n<p>\u7531\u4e0a\u8ff0\u7a0b\u5e8f\u8aaa\u660e\u4e86Python\u4f7f\u7528<code><code>( )<\/code>`<code>\u8868\u793a\u6240\u7e7c\u627f\u7684\u7269\u4ef6\uff0c\u7576\u8981\u4f7f\u7528\u6642\u53ea\u8981<\/code>`<code>\u985e\u5225\u540d\u7a31()<\/code>`<code>\u5373\u53ef\u7522\u751f\u4e00\u500b\u578b\u5225\u70ba\u8a72<\/code>`<code>\u985e\u5225\u540d\u7a31<\/code><\/code>\u7684\u7269\u4ef6\u3002<br \/>\n\u518d\u9032\u4e00\u6b65\u8aaa\u660e\uff0c\u9019\u88cf\u7684<code><code>self<\/code>`<code>\u6307\u7684\u662f\u7269\u4ef6\u5be6\u9ad4\uff0c\u7576\u8981\u4f7f\u7528\u7269\u4ef6\u7684\u5c6c\u6027\u6642\u90fd\u8981\u52a0\u4e0a<\/code>`<code>self.<\/code>`<code>\u524d\u7db4\u8a5e\u3002\u518d\u4f86<\/code>`<code>__init__<\/code>`<code>\u662f\u985e\u5225\u7684\u5efa\u69cb\u5b50\uff0c\u7576\u4f7f\u7528<\/code>`<code>\u985e\u5225\u540d\u7a31()<\/code>`<code>\u6703\u5148\u547c\u53eb\u5efa\u69cb\u5b50\uff0c\u4e26\u57f7\u884c\u5efa\u69cb\u5b50\u7684\u7a0b\u5e8f\uff0c\u5728Dog\u7684\u4f8b\u5b50\u4e2d\u6703\u7d66\u4e88\u8a72\u7269\u4ef6\u4e00\u500b\u540d\u5b57\uff0c\u518d\u4f86<\/code>`<code>super<\/code><\/code>\u662f\u547c\u53eb\u7236\u985e\u5225\u7684\u8a9e\u6cd5\uff0c\u4e0a\u8ff0\u4f8b\u5b50\u662f\u5efa\u69cb\u5b50\u4e2d\u547c\u53eb\u7236\u985e\u5225\u7684\u5efa\u69cb\u5b50\u4e26\u5c07\u540d\u7a31\u8ce6\u4e88\u9032\u53bb\u3002<\/p>\n<h2>\u5c01\u88dd<\/h2>\n<p>\u5728\u4e0a\u8ff0\u7e7c\u627f\u7684\u4f8b\u5b50\u4e2d<code><code>eat()<\/code>`<code>\u8207<\/code>`<code>walk() <\/code>`<code>\u5c31\u662f\u5c01\u88dd\u7684\u8868\u73fe\uff0c\u8b93\u5916\u90e8\u4f7f\u7528\u8005\u76f4\u63a5\u4f7f\u7528\u800c\u4e0d\u9700\u8003\u616e\u5167\u90e8\u5be6\u4f5c\uff0c\u82e5\u662f\u5728\u958b\u767c\u4e2d\u5982\u679c\u6709\u79c1\u6709\u8b8a\u6578\u4e0d\u5e0c\u671b\u8b93\u5916\u90e8\u4f7f\u7528\u8005\uff0c\u6216\u8005\u662f\u88ab\u7e7c\u627f\u7684\u985e\u5225\u4f7f\u7528\u7684\u53ef\u4ee5\u4f7f\u7528<\/code>`<code>__<\/code><\/code>\u524d\u7db4\u8a5e\u4f86\u547d\u540d\u8b8a\u6578\u540d\u7a31\u4f8b\u5982:<\/p>\n<pre><code class=\"language-python\">class Animal:\n    name = &#039;&#039;\n    __private = &#039;&#039;\n    def __init__(self, name):\n        self.name = name\n\n    def walk(self):\n        print(&#039;walking&#039;)\n\n    def eat(self):\n        print(&#039;eating&#039;)<\/code><\/pre>\n<h2>\u591a\u578b<\/h2>\n<p>\u63a5\u627f\u4e0a\u8ff0\u4f8b\u5b50\uff0c\u5728\u591a\u5efa\u7acb\u4e00\u500bclass\u7e7c\u627f\u4e0a\u8ff0\u7684Animal\u4e26\u5be6\u4f5c<code><code>eat()<\/code>`<code>\u8ddf<\/code>`<code>walk()<\/code>`<code>\u4e4b\u5f8c\u7576\u5be6\u9ad4\u7269\u4ef6\u4ee5Dog\u7522\u751f\u5247\u5176<\/code>`<code>eat()<\/code>`<code>\u8ddf<\/code>`<code>walk()<\/code>`<code>\u6703\u4f9d\u64daDog\u7684\u5be6\u4f5c\u4f86\u57f7\u884c\uff0c\u82e5\u4ee5\u65b0\u7684class\u4f86\u7522\u751f\u5be6\u9ad4\u5247\u6703\u4f9d\u64da\u65b0class\u7684<\/code>`<code>eat()<\/code>`<code>\u8ddf<\/code>`<code>walk()<\/code><\/code>\u5be6\u4f5c\u4f86\u57f7\u884c\uff0c\u8acb\u898b\u4ee5\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">class Duck(Animal):\n    def __init__(self, name):\n        super().__init__(name)\n\n    def walk(self):\n        print(&#039;{0} using two foot walk&#039;.format(self.name))\n\n    def eat(self):\n        print(&#039;{0} eat worm&#039;.format(self.name))\n\nanimal = Dog(&#039;A&#039;)\nanimal = Duck(&#039;A&#039;)\nanimal.eat()\nanimal.walk()<\/code><\/pre>\n<p>\u7d50\u679c\u6703\u662f\u986f\u793a\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code class=\"language-python\">A using two foot walk\nA eat worm<\/code><\/pre>\n<h2>\u62bd\u8c61<\/h2>\n<p>\u62bd\u8c61\u7684\u8868\u73fe\u662f\u70ba\u5be6\u4f5c\u7684class\u4e0d\u80fd\u7522\u751f\u5be6\u9ad4\uff0c\u800c\u8a72class\u50c5\u662f\u5b9a\u7fa9\u4e00\u500b\u6a21\u677f\u544a\u8a34\u5be6\u4f5c\u8005\u8981\u5be6\u4f5c\u4ec0\u9ebc\u5167\u5bb9\uff0c\u5118\u7ba1Python\u6839\u672c\u4e0a\u6c92\u6709\u4fee\u98fe\u8a5e\u53ef\u4ee5\u5ba3\u544a\u62bd\u50cf\u985e\u5225\uff0c\u4f46\u662f\u4ecd\u53ef\u900f\u904e\u64f4\u5145\u51fd\u5f0f\u5eab\u4f86\u505a\u5230\uff0c\u5f85\u5f8c\u7e8c\u518d\u88dc\u5145\u76f8\u95dc\u8cc7\u6599\u3002<\/p>\n<h1>\u5c0f\u7d50<\/h1>\n<p>\u900f\u904e\u672c\u7ae0\u7684\u8aaa\u660e\u5e0c\u671b\u5404\u4f4d\u8b80\u8005\u5011\u53ef\u4ee5\u5c0dPython\u7684OOP\u6709\u66f4\u9032\u4e00\u6b65\u7684\u4e86\u89e3\u3002\u5728\u8aaa\u5b8cOOP\u5df2\u7d93\u65b9\u6cd5\u5f8c\u4e0b\u4e00\u7ae0\u8981\u4ecb\u7d39\u53e6\u4e00\u500b\u53ef\u4ee5\u628a\u7a0b\u5e8f\u5beb\u7684\u66f4\u7c21\u6f54\u7684\u88dd\u98fe\u5668\uff0c\u5728\u8aaa\u660e\u88dd\u98fe\u5668\u5f8c\u6703\u518d\u91dd\u5c0d\u985e\u5225\u7684\u9032\u968e\u5167\u5bb9\u518d\u4f5c\u88dc\u5145\uff0c\u656c\u8acb\u671f\u5f85\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u9ebc\u662fOOP \u76f8\u4fe1\u8edf\u9ad4\u958b\u767c\u5de5\u7a0b\u5e2b\u5c0d\u65bc\u9019\u8a5e\u61c9\u8a72\u4e0d\u964c\u751f\uff0c\u6240\u4ee5\u6211\u5c31\u591a\u505a\u8d05\u8ff0\u4e86\uff0c\u6211\u53ea\u63d0\u53ca\u5176\u91cd\u8981\u7684\u56db\u500b\u89c0\u5ff5\u5728Python &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2018\/10\/09\/2019ironman-flask-restful-day08\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u7b2c\u5341\u5c46\u9435\u4eba\u8cfd flask-restful DAY08-\u641e\u61c2Python\u7684OOP\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[162,4,5,3],"class_list":["post-255","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2018ironman","tag-flask","tag-flask-restful","tag-python"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":28,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions"}],"predecessor-version":[{"id":2761,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/255\/revisions\/2761"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}