{"id":179243,"date":"2024-05-08T20:09:01","date_gmt":"2024-05-08T12:09:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179243.html"},"modified":"2024-05-08T20:09:16","modified_gmt":"2024-05-08T12:09:16","slug":"python-classobj%e5%90%8e%e9%9d%a2%e7%9a%84%e6%8b%ac%e5%8f%b7%e4%b8%ad%e5%86%85%e5%ae%b9%e7%9a%84%e4%bd%9c%e7%94%a8%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/179243.html","title":{"rendered":"Python class(obj)\u540e\u9762\u7684\u62ec\u53f7\u4e2d\u5185\u5bb9\u7684\u4f5c\u7528\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063953\/19a927c1-9324-4c4a-bce1-966d0fc40df7.webp\" alt=\"Python class(obj)\u540e\u9762\u7684\u62ec\u53f7\u4e2d\u5185\u5bb9\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5f53\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u62ec\u53f7\u4e2d\u7684\u5185\u5bb9\u4e3b\u8981\u6307\u5b9a\u8be5\u7c7b\u8981<strong>\u7ee7\u627f\u7684\u7236\u7c7b<\/strong>\uff0c\u8fd9\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\u3002<strong>\u7c7b\u7684\u7ee7\u627f<\/strong>\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627f\u4e00\u4e2a\u6216\u591a\u4e2a\u7236\u7c7b\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u7c7b\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5b50\u7c7b\u53ef\u4ee5\u91cd\u7528\u7236\u7c7b\u4e2d\u7684\u4ee3\u7801\uff0c\u907f\u514d\u91cd\u590d\u7f16\u5199\u76f8\u540c\u4ee3\u7801\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u91cd\u7528\u6027\u548c\u6548\u7387\u3002\u6b64\u5916\uff0c\u8fd9\u79cd\u673a\u5236\u8fd8\u652f\u6301\u591a\u6001\u548c\u5c01\u88c5\uff0c\u4f7f\u5f97\u5bf9\u8c61\u6a21\u578b\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a<code>Animal<\/code>\u7c7b\uff0c\u5b83\u6709\u4e00\u4e9b\u57fa\u672c\u65b9\u6cd5\uff0c\u5982<code>eat<\/code>\u548c<code>sleep<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7ee7\u627f\u81ea<code>Animal<\/code>\u7684<code>Dog<\/code>\u7c7b\uff0c\u8fd9\u6837<code>Dog<\/code>\u7c7b\u5c31\u81ea\u52a8\u62e5\u6709\u4e86<code>eat<\/code>\u548c<code>sleep<\/code>\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u7279\u6709\u7684\u65b9\u6cd5\u4f8b\u5982<code>bark<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7c7b\u7684\u7ee7\u627f\u548c\u4f5c\u7528<\/h2>\n<\/p>\n<p><p><strong>\u7c7b\u7684\u7ee7\u627f<\/strong>\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e00\u4e2a\u6838\u5fc3\u7279\u5f81\uff0c\u5b83\u5141\u8bb8\u521b\u5efa\u4e00\u4e2a\u57fa\u4e8e\u53e6\u4e00\u4e2a\u7c7b\uff08\u7236\u7c7b\u6216\u57fa\u7c7b\uff09\u7684\u7c7b\uff08\u79f0\u4e3a\u5b50\u7c7b\u6216\u6d3e\u751f\u7c7b\uff09\u3002\u8fd9\u79cd\u673a\u5236\u7684\u4f5c\u7528\u4e3b\u8981\u4f53\u73b0\u5728\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u91cd\u7528<\/strong>\uff1a\u5b50\u7c7b\u7ee7\u627f\u4e86\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f7f\u5f97\u5171\u901a\u7684\u4ee3\u7801\u53ea\u9700\u8981\u5728\u7236\u7c7b\u4e2d\u7f16\u5199\u4e00\u6b21\u3002<\/li>\n<li><strong>\u5b9e\u73b0\u591a\u6001<\/strong>\uff1a\u901a\u8fc7\u7ee7\u627f\u548c\u91cd\u5199\u7236\u7c7b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5728\u4e0d\u540c\u7684\u5b50\u7c7b\u4e2d\u5bf9\u540c\u4e00\u4e2a\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002<\/li>\n<li><strong>\u5c01\u88c5\u6027\u589e\u5f3a<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u628a\u516c\u5171\u7684\u4ee3\u7801\u653e\u5728\u7236\u7c7b\u4e2d\uff0c\u800c\u5c06\u7279\u6b8a\u7684\u4ee3\u7801\u653e\u5728\u5b50\u7c7b\u4e2d\uff0c\u4f7f\u5f97\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u7ee7\u627f\u7684\u8bed\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7ee7\u627f\u7684\u8bed\u6cd5\u7b80\u5355\u660e\u4e86\u3002\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u5c06\u7236\u7c7b\u540d\u653e\u5728\u5b50\u7c7b\u540d\u540e\u7684\u62ec\u53f7\u4e2d\uff0c\u6765\u6307\u5b9a\u8be5\u5b50\u7c7b\u8981\u7ee7\u627f\u7684\u7236\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    pass<\/p>\n<p>class Child(Parent):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Child<\/code>\u7c7b\u7ee7\u627f\u4e86<code>Parent<\/code>\u7c7b\uff0c\u8fd9\u610f\u5473\u7740<code>Child<\/code>\u7c7b\u4f1a\u81ea\u52a8\u62e5\u6709<code>Parent<\/code>\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ee7\u627f\u7684\u6df1\u5c42\u7406\u89e3<\/h3>\n<\/p>\n<p><p>\u5f53\u6d89\u53ca\u5230\u590d\u6742\u7684\u7ee7\u627f\u5173\u7cfb\u65f6\uff0c\u7406\u89e3Python\u7684\u7ee7\u627f\u673a\u5236\u5c24\u4e3a\u91cd\u8981\u3002Python\u652f\u6301\u591a\u91cd\u7ee7\u627f\uff0c\u5373\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u7ee7\u627f\u591a\u4e2a\u7236\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Father:<\/p>\n<p>    pass<\/p>\n<p>class Mother:<\/p>\n<p>    pass<\/p>\n<p>class Child(Father, Mother):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u673a\u5236\u8ba9Python\u7684\u7c7b\u7ed3\u6784\u975e\u5e38\u7075\u6d3b\uff0c\u4f46\u4e5f\u5e26\u6765\u4e86\u4e00\u5b9a\u7684\u590d\u6742\u6027\uff0c\u6bd4\u5982\u9700\u8981\u7406\u89e3<strong>\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09<\/strong>\uff0c\u786e\u4fdd\u6b63\u786e\u7406\u89e3\u65b9\u6cd5\u8c03\u7528\u65f6\u7684\u67e5\u627e\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u65b9\u6cd5\u91cd\u5199\u548csuper\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f53\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u65f6\uff0c\u6709\u65f6\u9700\u8981\u4fee\u6539\u7ee7\u627f\u6765\u7684\u65b9\u6cd5\u4ee5\u9002\u5e94\u5b50\u7c7b\u7684\u9700\u6c42\uff0c\u8fd9\u65f6\u53ef\u4ee5\u901a\u8fc7\u91cd\u5199\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Parent&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<p>child_instance = Child()<\/p>\n<p>child_instance.greet()  # \u8f93\u51fa: Hello from Child<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u91cd\u5199\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u60f3\u8981\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        super().greet()  # \u8c03\u7528\u7236\u7c7b\u7684greet\u65b9\u6cd5<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>super().greet()<\/code>\u4f1a\u5148\u8c03\u7528\u7236\u7c7b<code>Parent<\/code>\u4e2d\u7684<code>greet<\/code>\u65b9\u6cd5\uff0c\u7136\u540e\u6267\u884c<code>Child<\/code>\u7c7b\u4e2d\u7684<code>greet<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u4e2d\u7684\u7c7b\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u4fc3\u8fdb\u4e86\u4ee3\u7801\u7684\u590d\u7528\u5e76\u7b80\u5316\u4e86\u4ee3\u7801\u7ed3\u6784\u3002\u901a\u8fc7\u6b63\u786e\u4f7f\u7528\u7c7b\u7684\u7ee7\u627f\u3001\u7406\u89e3\u591a\u91cd\u7ee7\u627f\u7684\u590d\u6742\u6027\uff0c\u5e76\u638c\u63e1\u65b9\u6cd5\u91cd\u5199\u4e0e\u4f7f\u7528<code>super<\/code>\u51fd\u6570\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5\u7684\u6280\u5de7\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u5347\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48Python\u4e2d\u7c7b\u7684\u5b9a\u4e49\u9700\u8981\u5728\u62ec\u53f7\u4e2d\u52a0\u5165obj\uff1f<\/strong><br \/>\n\u5728Python\u4e2d\uff0c\u7c7b\u5b9a\u4e49\u65f6\u62ec\u53f7\u4e2d\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u57fa\u7c7b\uff0c\u5373\u6240\u8c13\u7684\u7ee7\u627f\u3002\u4f20\u5165obj\u4f5c\u4e3a\u57fa\u7c7b\u7684\u4f5c\u7528\u662f\u4f7f\u6211\u4eec\u5b9a\u4e49\u7684\u65b0\u7c7b\u62e5\u6709\u4e00\u4e9bPython\u5185\u7f6e\u5bf9\u8c61\u7684\u9ed8\u8ba4\u884c\u4e3a\u548c\u5c5e\u6027\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ee7\u627f\u548c\u6269\u5c55\u5185\u7f6e\u5bf9\u8c61\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>2. \u5728Python\u4e2d\uff0c\u4e3a\u4ec0\u4e48\u6709\u4e9b\u7c7b\u7684\u5b9a\u4e49\u5728\u62ec\u53f7\u4e2d\u52a0\u5165obj\uff0c\u800c\u6709\u4e9b\u5219\u4e0d\u52a0\u5462\uff1f<\/strong><br \/>\n\u5728Python\u4e2d\uff0c\u4e0d\u662f\u6240\u6709\u7c7b\u7684\u5b9a\u4e49\u90fd\u9700\u8981\u5728\u62ec\u53f7\u4e2d\u52a0\u5165obj\u3002\u53ea\u6709\u5f53\u6211\u4eec\u9700\u8981\u7ee7\u627f\u5185\u7f6e\u5bf9\u8c61\u7684\u529f\u80fd\u65f6\uff0c\u624d\u9700\u8981\u5728\u62ec\u53f7\u4e2d\u52a0\u5165obj\u4f5c\u4e3a\u57fa\u7c7b\u3002\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u9700\u8981\u7ee7\u627f\u5185\u7f6e\u5bf9\u8c61\u529f\u80fd\u7684\u81ea\u5b9a\u4e49\u7c7b\uff0c\u53ef\u4ee5\u7701\u7565\u62ec\u53f7\u90e8\u5206\u3002<\/p>\n<p><strong>3. \u5728Python\u4e2d\uff0c\u5982\u679c\u4e0d\u52a0\u5165obj\u4f5c\u4e3a\u57fa\u7c7b\u4f1a\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><br \/>\n\u5982\u679c\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\u4e0d\u52a0\u5165obj\u4f5c\u4e3a\u57fa\u7c7b\uff0c\u90a3\u4e48\u65b0\u5b9a\u4e49\u7684\u7c7b\u5c06\u4e0d\u4f1a\u7ee7\u627f\u4efb\u4f55\u5185\u7f6e\u5bf9\u8c61\u7684\u529f\u80fd\u548c\u5c5e\u6027\u3002\u8fd9\u610f\u5473\u7740\u65b0\u7c7b\u5c06\u65e0\u6cd5\u4f7f\u7528\u5185\u7f6e\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u65e0\u6cd5\u50cf\u5185\u7f6e\u5bf9\u8c61\u4e00\u6837\u8fdb\u884c\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u52a0\u5165obj\u4f5c\u4e3a\u57fa\u7c7b\uff0c\u53ef\u4ee5\u8ba9\u65b0\u5b9a\u4e49\u7684\u7c7b\u5177\u5907\u4e00\u4e9b\u901a\u7528\u7684\u5185\u7f6e\u5bf9\u8c61\u884c\u4e3a\uff0c\u5e76\u4e14\u80fd\u591f\u65b9\u4fbf\u5730\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884c\u6269\u5c55\u548c\u5b9a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5f53\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u62ec\u53f7\u4e2d\u7684\u5185\u5bb9\u4e3b\u8981\u6307\u5b9a\u8be5\u7c7b\u8981\u7ee7\u627f\u7684\u7236\u7c7b\uff0c\u8fd9\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\u3002\u7c7b\u7684 [&hellip;]","protected":false},"author":3,"featured_media":179265,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179243"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=179243"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179265"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}