{"id":996847,"date":"2024-12-27T09:18:26","date_gmt":"2024-12-27T01:18:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/996847.html"},"modified":"2024-12-27T09:18:28","modified_gmt":"2024-12-27T01:18:28","slug":"python%e4%b8%ad%e7%bb%a7%e6%89%bf%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/996847.html","title":{"rendered":"python\u4e2d\u7ee7\u627f\u5982\u4f55\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072908\/2d1209ac-1cd4-46de-910a-7b80e4b867e4.webp\" alt=\"python\u4e2d\u7ee7\u627f\u5982\u4f55\u5b9e\u73b0\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u7ee7\u627f\u662f\u901a\u8fc7<strong>\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff08\u5b50\u7c7b\uff09\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\uff08\u7236\u7c7b\uff09<\/strong>\u6765\u5b9e\u73b0\u7684\u3002\u7ee7\u627f\u7684\u5173\u952e\u5728\u4e8e<strong>\u4ee3\u7801\u91cd\u7528\u3001\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3001\u652f\u6301\u591a\u6001\u6027<\/strong>\u3002\u9996\u5148\uff0c\u901a\u8fc7\u5728\u5b50\u7c7b\u7684\u5b9a\u4e49\u4e2d\uff0c\u5c06\u7236\u7c7b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b50\u7c7b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7ee7\u627f\u3002\u7ee7\u627f\u5141\u8bb8\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002\u901a\u8fc7\u7ee7\u627f\uff0c\u5b50\u7c7b\u4e5f\u53ef\u4ee5\u91cd\u5199\u6216\u6269\u5c55\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7ee7\u627f\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u5b9a\u4e49\u7c7b\u548c\u7ee7\u627f<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u7684\u5b9a\u4e49\u662f\u901a\u8fc7<code>class<\/code>\u5173\u952e\u5b57\u5b9e\u73b0\u7684\u3002\u7ee7\u627f\u662f\u901a\u8fc7\u5728\u5b50\u7c7b\u5b9a\u4e49\u65f6\u5c06\u7236\u7c7b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<\/p>\n<p><h2>1.1 \u7c7b\u7684\u57fa\u672c\u5b9a\u4e49<\/h2>\n<\/p>\n<p><p>\u7c7b\u662fPython\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3\u6982\u5ff5\u3002\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def __init__(self, name, species):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.species = species<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        print(f&quot;{self.name} makes a sound.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>Animal<\/code>\u662f\u4e00\u4e2a\u57fa\u672c\u7c7b\uff0c\u5305\u542b<code>name<\/code>\u548c<code>species<\/code>\u4e24\u4e2a\u5c5e\u6027\u4ee5\u53ca\u4e00\u4e2a\u65b9\u6cd5<code>make_sound()<\/code>\u3002<\/p>\n<\/p>\n<p><h2>1.2 \u5b9e\u73b0\u7ee7\u627f<\/h2>\n<\/p>\n<p><p>\u7ee7\u627f\u5141\u8bb8\u4e00\u4e2a\u7c7b\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Dog(Animal):<\/p>\n<p>    def __init__(self, name, breed):<\/p>\n<p>        super().__init__(name, &quot;Dog&quot;)<\/p>\n<p>        self.breed = breed<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        print(f&quot;{self.name} barks.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Dog<\/code>\u7c7b\u7ee7\u627f\u4e86<code>Animal<\/code>\u7c7b\u3002<code>super()<\/code>\u51fd\u6570\u7528\u4e8e\u8c03\u7528\u7236\u7c7b\u7684<code>__init__<\/code>\u65b9\u6cd5\uff0c\u4ece\u800c\u521d\u59cb\u5316\u7236\u7c7b\u7684\u5c5e\u6027\u3002\u540c\u65f6\uff0c<code>Dog<\/code>\u7c7b\u91cd\u5199\u4e86<code>make_sound()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u7ee7\u627f\u7684\u4f18\u52bf<\/strong><\/h2>\n<p><p>\u7ee7\u627f\u63d0\u4f9b\u4e86\u51e0\u4e2a\u663e\u8457\u7684\u4f18\u52bf\uff0c\u5982\u4ee3\u7801\u91cd\u7528\u3001\u589e\u5f3a\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u548c\u652f\u6301\u591a\u6001\u6027\u3002<\/p>\n<\/p>\n<p><h2>2.1 \u4ee3\u7801\u91cd\u7528<\/h2>\n<\/p>\n<p><p>\u7ee7\u627f\u5141\u8bb8\u5b50\u7c7b\u91cd\u7528\u7236\u7c7b\u7684\u4ee3\u7801\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u5199\u76f8\u540c\u7684\u4ee3\u7801\u3002\u8fd9\u79cd\u91cd\u7528\u4e0d\u4ec5\u51cf\u5c11\u4e86\u4ee3\u7801\u91cf\uff0c\u4e5f\u964d\u4f4e\u4e86\u4ee3\u7801\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u6982\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u4e0a\u9762\u7684<code>Dog<\/code>\u7c7b\u4e2d\uff0c\u6211\u4eec\u4e0d\u9700\u8981\u91cd\u65b0\u5b9a\u4e49<code>name<\/code>\u548c<code>species<\/code>\u7684\u521d\u59cb\u5316\u903b\u8f91\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5df2\u7ecf\u5728\u7236\u7c7b<code>Animal<\/code>\u4e2d\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><h2>2.2 \u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u7ee7\u627f\uff0c\u4efb\u4f55\u5bf9\u7236\u7c7b\u7684\u4fee\u6539\u90fd\u4f1a\u81ea\u52a8\u53cd\u6620\u5728\u6240\u6709\u5b50\u7c7b\u4e2d\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u7684\u7ef4\u62a4\u53d8\u5f97\u66f4\u5bb9\u6613\uff0c\u56e0\u4e3a\u53ea\u9700\u8981\u5728\u4e00\u4e2a\u5730\u65b9\u8fdb\u884c\u66f4\u6539\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728<code>Animal<\/code>\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5\u6216\u4fee\u6539\u73b0\u6709\u65b9\u6cd5\uff0c\u53ea\u9700\u8981\u5728<code>Animal<\/code>\u7c7b\u4e2d\u8fdb\u884c\u66f4\u6539\uff0c\u800c\u4e0d\u5fc5\u4fee\u6539\u6240\u6709\u7684\u5b50\u7c7b\u3002<\/p>\n<\/p>\n<p><h2>2.3 \u652f\u6301\u591a\u6001\u6027<\/h2>\n<\/p>\n<p><p>\u591a\u6001\u6027\u5141\u8bb8\u5bf9\u8c61\u4ee5\u591a\u79cd\u5f62\u5f0f\u5b58\u5728\u3002\u901a\u8fc7\u7ee7\u627f\uff0c\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u5b9e\u73b0\u76f8\u540c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>Dog<\/code>\u7c7b\u548c\u5176\u4ed6\u53ef\u80fd\u7ee7\u627f<code>Animal<\/code>\u7c7b\u7684\u5b50\u7c7b\u53ef\u4ee5\u5404\u81ea\u5b9e\u73b0\u81ea\u5df1\u7684<code>make_sound()<\/code>\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4e0d\u540c\u7684\u52a8\u7269\u53ef\u4ee5\u53d1\u51fa\u4e0d\u540c\u7684\u58f0\u97f3\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e09\u3001\u5b50\u7c7b\u7684\u6269\u5c55\u548c\u91cd\u5199<\/strong><\/h2>\n<p><p>\u7ee7\u627f\u4e0d\u4ec5\u5141\u8bb8\u4ee3\u7801\u91cd\u7528\uff0c\u8fd8\u5141\u8bb8\u5b50\u7c7b\u6269\u5c55\u548c\u91cd\u5199\u7236\u7c7b\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>3.1 \u6269\u5c55\u7236\u7c7b<\/h2>\n<\/p>\n<p><p>\u5b50\u7c7b\u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u5728\u7236\u7c7b\u4e2d\u4e0d\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Cat(Animal):<\/p>\n<p>    def __init__(self, name, color):<\/p>\n<p>        super().__init__(name, &quot;Cat&quot;)<\/p>\n<p>        self.color = color<\/p>\n<p>    def purr(self):<\/p>\n<p>        print(f&quot;{self.name} is purring.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684<code>Cat<\/code>\u7c7b\u4e2d\uff0c\u9664\u4e86\u7ee7\u627f<code>Animal<\/code>\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5916\uff0c\u8fd8\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027<code>color<\/code>\u548c\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5<code>purr()<\/code>\u3002<\/p>\n<\/p>\n<p><h2>3.2 \u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u4f9b\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Bird(Animal):<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        print(f&quot;{self.name} sings.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>Bird<\/code>\u7c7b\u4e2d\uff0c<code>make_sound()<\/code>\u65b9\u6cd5\u88ab\u91cd\u5199\u4e3a\u6253\u5370\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u4f7f\u5f97<code>Bird<\/code>\u7c7b\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>super()<\/code>\u51fd\u6570<\/strong><\/h2>\n<p><p>\u5728\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u6216\u521d\u59cb\u5316\u7236\u7c7b\u7684\u5c5e\u6027\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>4.1 <code>super()<\/code>\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p><code>super()<\/code>\u51fd\u6570\u7528\u4e8e\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u907f\u514d\u76f4\u63a5\u4f7f\u7528\u7236\u7c7b\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Fish(Animal):<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        super().__init__(name, &quot;Fish&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>super().__init__(name, &quot;Fish&quot;)<\/code>\u7528\u4e8e\u8c03\u7528\u7236\u7c7b<code>Animal<\/code>\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>4.2 <code>super()<\/code>\u7684\u4f18\u52bf<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>super()<\/code>\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u5e76\u4e14\u5728\u591a\u91cd\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c\u786e\u4fdd\u6240\u6709\u7684\u7236\u7c7b\u90fd\u88ab\u6b63\u786e\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5728\u591a\u91cd\u7ee7\u627f\u4e2d\uff0c<code>super()<\/code>\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u6d89\u53ca\u7684\u7236\u7c7b\u7684<code>__init__<\/code>\u65b9\u6cd5\u90fd\u88ab\u8c03\u7528\uff0c\u800c\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u8c03\u7528\u6bcf\u4e00\u4e2a\u7236\u7c7b\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e94\u3001\u591a\u91cd\u7ee7\u627f<\/strong><\/h2>\n<p><p>Python\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><h2>5.1 \u591a\u91cd\u7ee7\u627f\u7684\u8bed\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u91cd\u7ee7\u627f\u4e2d\uff0c\u591a\u4e2a\u7236\u7c7b\u901a\u8fc7\u9017\u53f7\u5206\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Flyer:<\/p>\n<p>    def fly(self):<\/p>\n<p>        print(&quot;Flying high!&quot;)<\/p>\n<p>class Swimmer:<\/p>\n<p>    def swim(self):<\/p>\n<p>        print(&quot;Swimming fast!&quot;)<\/p>\n<p>class Duck(Flyer, Swimmer):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Duck<\/code>\u7c7b\u7ee7\u627f\u4e86<code>Flyer<\/code>\u548c<code>Swimmer<\/code>\u7c7b\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e24\u4e2a\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>5.2 \u591a\u91cd\u7ee7\u627f\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><p>\u591a\u91cd\u7ee7\u627f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u590d\u6742\u7684\u7ee7\u627f\u5173\u7cfb\uff0c\u5c24\u5176\u662f\u5f53\u591a\u4e2a\u7236\u7c7b\u6709\u540c\u540d\u65b9\u6cd5\u65f6\u3002Python\u901a\u8fc7\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c<code>Flyer<\/code>\u548c<code>Swimmer<\/code>\u90fd\u6709\u4e00\u4e2a\u540c\u540d\u7684\u65b9\u6cd5\uff0c<code>Duck<\/code>\u7c7b\u5c06\u6839\u636eMRO\u6765\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u516d\u3001\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09<\/strong><\/h2>\n<p><p>Python\u4e2d\u7684\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09\u7528\u4e8e\u786e\u5b9a\u5728\u591a\u91cd\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c\u5e94\u8be5\u8c03\u7528\u54ea\u4e2a\u7236\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>6.1 MRO\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<\/p>\n<p><p>MRO\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u7ee7\u627f\u5c42\u6b21\u7ed3\u6784\u4e2d\u67e5\u627e\u65b9\u6cd5\u7684\u7b97\u6cd5\u3002Python\u4f7f\u7528C3\u7ebf\u6027\u5316\u7b97\u6cd5\u6765\u8ba1\u7b97MRO\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(Duck.mro())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>Duck.mro()<\/code>\u5c06\u8fd4\u56de<code>[&lt;class &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__.Duck&#39;&gt;, &lt;class &#39;__main__.Flyer&#39;&gt;, &lt;class &#39;__main__.Swimmer&#39;&gt;, &lt;class &#39;object&#39;&gt;]<\/code>\uff0c\u8868\u793a\u65b9\u6cd5\u7684\u67e5\u627e\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>6.2 MRO\u7684\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u7406\u89e3MRO\u5bf9\u4e8e\u5728\u590d\u6742\u7684\u7ee7\u627f\u5c42\u6b21\u7ed3\u6784\u4e2d\u8c03\u8bd5\u4ee3\u7801\u975e\u5e38\u6709\u7528\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u4e86\u89e3\u65b9\u6cd5\u8c03\u7528\u7684\u987a\u5e8f\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u6309\u9884\u671f\u8fd0\u884c\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e03\u3001\u62bd\u8c61\u7c7b\u548c\u63a5\u53e3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u62bd\u8c61\u7c7b\u548c\u63a5\u53e3\u7528\u4e8e\u5b9a\u4e49\u7c7b\u7684\u84dd\u56fe\uff0c\u800c\u4e0d\u5b9e\u73b0\u5177\u4f53\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>7.1 \u62bd\u8c61\u7c7b\u7684\u5b9a\u4e49<\/h2>\n<\/p>\n<p><p>\u62bd\u8c61\u7c7b\u662f\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u7684\u7c7b\uff0c\u901a\u5e38\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u62bd\u8c61\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from abc import ABC, abstractmethod<\/p>\n<p>class AbstractAnimal(ABC):<\/p>\n<p>    @abstractmethod<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>AbstractAnimal<\/code>\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5<code>make_sound()<\/code>\u3002<\/p>\n<\/p>\n<p><h2>7.2 \u4f7f\u7528\u62bd\u8c61\u7c7b\u548c\u63a5\u53e3<\/h2>\n<\/p>\n<p><p>\u62bd\u8c61\u7c7b\u548c\u63a5\u53e3\u7528\u4e8e\u5b9a\u4e49\u4e00\u7ec4\u5fc5\u987b\u7531\u5b50\u7c7b\u5b9e\u73b0\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u786e\u4fdd\u5b50\u7c7b\u5177\u6709\u7279\u5b9a\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ConcreteDog(AbstractAnimal):<\/p>\n<p>    def make_sound(self):<\/p>\n<p>        print(&quot;Woof!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>ConcreteDog<\/code>\u7c7b\u4e2d\uff0c<code>make_sound()<\/code>\u65b9\u6cd5\u88ab\u5177\u4f53\u5b9e\u73b0\uff0c\u4ece\u800c\u6ee1\u8db3\u62bd\u8c61\u7c7b\u7684\u8981\u6c42\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Python\u4e2d\u7684\u7ee7\u627f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u673a\u5236\uff0c\u5141\u8bb8\u4ee3\u7801\u91cd\u7528\u3001\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u652f\u6301\u591a\u6001\u6027\u3002\u901a\u8fc7\u7ee7\u627f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u521b\u5efa\u66f4\u4e3a\u590d\u6742\u548c\u7075\u6d3b\u7684\u7c7b\u7ed3\u6784\u3002\u7406\u89e3\u7ee7\u627f\u3001<code>super()<\/code>\u51fd\u6570\u3001\u591a\u91cd\u7ee7\u627f\u3001MRO\u4ee5\u53ca\u62bd\u8c61\u7c7b\u548c\u63a5\u53e3\u7b49\u6982\u5ff5\uff0c\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u4e14\u53ef\u7ef4\u62a4\u7684Python\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u7ee7\u627f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u7ee7\u627f\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7ee7\u627f\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u4e00\u4e2a\u7c7b\uff08\u5b50\u7c7b\uff09\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\uff08\u7236\u7c7b\uff09\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u901a\u8fc7\u7ee7\u627f\uff0c\u5b50\u7c7b\u53ef\u4ee5\u91cd\u7528\u7236\u7c7b\u7684\u4ee3\u7801\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627f\u7684\u7c7b\uff1f<\/strong><br \/>\u5b9a\u4e49\u7ee7\u627f\u7684\u7c7b\u65f6\uff0c\u53ef\u4ee5\u5728\u5b50\u7c7b\u7684\u5b9a\u4e49\u4e2d\u62ec\u53f7\u5185\u6307\u5b9a\u7236\u7c7b\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff0c<code>class \u5b50\u7c7b\u540d(\u7236\u7c7b\u540d):<\/code>\u3002\u5728\u5b50\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u6216\u8005\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u4ee5\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u7ee7\u627f\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9677\u9631\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u7ee7\u627f\u65f6\uff0c\u9700\u6ce8\u610f\u7236\u7c7b\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u3002\u82e5\u7236\u7c7b\u4e2d\u5b9a\u4e49\u4e86<code>__init__<\/code>\u65b9\u6cd5\uff0c\u5b50\u7c7b\u9700\u4f7f\u7528<code>super().__init__()<\/code>\u6765\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u6b64\u5916\uff0c\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u7ee7\u627f\uff0c\u7279\u522b\u662f\u591a\u91cd\u7ee7\u627f\uff0c\u4ee5\u514d\u5bfc\u81f4\u4ee3\u7801\u590d\u6742\u6027\u589e\u52a0\u548c\u6f5c\u5728\u7684\u547d\u540d\u51b2\u7a81\u3002\u5408\u7406\u4f7f\u7528\u7ec4\u5408\u548c\u63a5\u53e3\u8bbe\u8ba1\u4e5f\u662f\u4e00\u79cd\u826f\u597d\u7684\u5b9e\u8df5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ee7\u627f\u662f\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff08\u5b50\u7c7b\uff09\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\uff08\u7236\u7c7b\uff09\u6765\u5b9e\u73b0\u7684\u3002\u7ee7\u627f\u7684\u5173\u952e\u5728\u4e8e\u4ee3\u7801\u91cd\u7528\u3001\u589e\u5f3a\u4ee3\u7801\u7684 [&hellip;]","protected":false},"author":3,"featured_media":996853,"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\/996847"}],"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=996847"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996847\/revisions"}],"predecessor-version":[{"id":996857,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/996847\/revisions\/996857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/996853"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=996847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=996847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=996847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}