{"id":3384,"date":"2025-06-02T15:11:58","date_gmt":"2025-06-04T08:01:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3384"},"modified":"2025-06-04T15:11:58","modified_gmt":"2025-06-04T08:01:58","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84%e6%8a%bd%e8%b1%a1%e5%9f%ba%e9%a1%9eabstractbaseclass-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84%e6%8a%bd%e8%b1%a1%e5%9f%ba%e9%a1%9eabstractbaseclass-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u62bd\u8c61\u57fa\u985e\uff08Abstract Base Class\uff09\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, abstract base class, \u62bd\u8c61\u57fa\u985e, Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>\u4ec0\u9ebc\u662f Python \u7684\u62bd\u8c61\u57fa\u985e\uff08Abstract Base Class\uff09\uff1f<\/h1>\n<p>Python \u7684\u62bd\u8c61\u57fa\u985e\uff08ABC\uff09\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u985e\u5225\uff0c\u7528\u65bc\u5b9a\u7fa9\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u9019\u4e9b\u65b9\u6cd5\u548c\u5c6c\u6027\u5fc5\u9808\u5728\u5b50\u985e\u5225\u4e2d\u5be6\u73fe\u3002\u62bd\u8c61\u57fa\u985e\u7684\u4e3b\u8981\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u500b\u6a21\u677f\uff0c\u8b93\u5b50\u985e\u5225\u53ef\u4ee5\u7e7c\u627f\u4e26\u5be6\u4f5c\u9019\u4e9b\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\u3002\u4f7f\u7528 ABC \u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5f37\u5236\u57f7\u884c\u67d0\u4e9b\u65b9\u6cd5\u7684\u5be6\u4f5c\uff0c\u5f9e\u800c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u8a2d\u8a08\u8207\u7dad\u8b77\u6027\u3002<\/p>\n<h2>Python \u4e2d\u7684\u62bd\u8c61\u57fa\u985e\uff08ABC\uff09<\/h2>\n<p>\u62bd\u8c61\u57fa\u985e\uff08ABC\uff09\u662f\u4e00\u7a2e\u900f\u904e `abc` \u6a21\u7d44\u5be6\u73fe\u7684\u8a2d\u8a08\u6a21\u5f0f\u3002\u5b83\u5141\u8a31\u958b\u767c\u8005\u5b9a\u7fa9\u4e00\u7d44\u65b9\u6cd5\uff0c\u9019\u4e9b\u65b9\u6cd5\u5728\u5b50\u985e\u5225\u4e2d\u5fc5\u9808\u88ab\u5be6\u4f5c\u3002\u9019\u7a2e\u65b9\u5f0f\u8b93\u7a0b\u5f0f\u78bc\u66f4\u5177\u53ef\u64f4\u5c55\u6027\u8207\u53ef\u8b80\u6027\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Python \u7684\u62bd\u8c61\u57fa\u985e\uff08ABC\uff09<\/h2>\n<p>\u5728 Python \u4e2d\u4f7f\u7528\u62bd\u8c61\u57fa\u985e\u975e\u5e38\u7c21\u55ae\uff0c\u60a8\u53ea\u9700\u5728\u985e\u5225\u5b9a\u7fa9\u4e2d\u4f7f\u7528 `@abstractmethod` \u88dd\u98fe\u5668\u4f86\u5b9a\u7fa9\u62bd\u8c61\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u62bd\u8c61\u57fa\u985e\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom abc import ABC, abstractmethod<\/p>\n<p>class MyAbstractBaseClass(ABC):<br \/>\n    @abstractmethod<br \/>\n    def my_abstract_method(self):<br \/>\n        &#8220;&#8221;&#8221;\u5b50\u985e\u5225\u5fc5\u9808\u5be6\u4f5c\u6b64\u65b9\u6cd5&#8221;&#8221;&#8221;<br \/>\n        pass<\/p>\n<p>    @property<br \/>\n    @abstractmethod<br \/>\n    def my_abstract_property(self):<br \/>\n        &#8220;&#8221;&#8221;\u5b50\u985e\u5225\u5fc5\u9808\u5be6\u4f5c\u6b64\u5c6c\u6027&#8221;&#8221;&#8221;<br \/>\n        pass<br \/>\n&#8220;`<\/p>\n<p>\u5728\u6b64\u7bc4\u4f8b\u4e2d\uff0c`MyAbstractBaseClass` \u662f\u4e00\u500b\u62bd\u8c61\u57fa\u985e\uff0c\u5b83\u5b9a\u7fa9\u4e86\u4e00\u500b\u62bd\u8c61\u65b9\u6cd5 `my_abstract_method` \u548c\u4e00\u500b\u62bd\u8c61\u5c6c\u6027 `my_abstract_property`\u3002\u4efb\u4f55\u7e7c\u627f\u6b64\u62bd\u8c61\u57fa\u985e\u7684\u5b50\u985e\u5225\u90fd\u5fc5\u9808\u5be6\u4f5c\u9019\u4e9b\u65b9\u6cd5\u548c\u5c6c\u6027\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5be6\u4f5c\u4e0a\u8ff0\u62bd\u8c61\u57fa\u985e\u7684\u4e00\u500b\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass ConcreteClass(MyAbstractBaseClass):<br \/>\n    def my_abstract_method(self):<br \/>\n        return &#8220;\u9019\u662f\u62bd\u8c61\u65b9\u6cd5\u7684\u5be6\u4f5c&#8221;<\/p>\n<p>    @property<br \/>\n    def my_abstract_property(self):<br \/>\n        return &#8220;\u9019\u662f\u62bd\u8c61\u5c6c\u6027\u7684\u5be6\u4f5c&#8221;<br \/>\n&#8220;`<\/p>\n<p>\u5728 `ConcreteClass` \u4e2d\uff0c\u6211\u5011\u5be6\u4f5c\u4e86 `my_abstract_method` \u548c `my_abstract_property`\uff0c\u9019\u6a23\u4fbf\u6eff\u8db3\u4e86\u62bd\u8c61\u57fa\u985e\u7684\u8981\u6c42\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u82e5\u60a8\u5728\u4f7f\u7528\u62bd\u8c61\u57fa\u985e\u6642\u9047\u5230\u932f\u8aa4\uff0c\u901a\u5e38\u662f\u56e0\u70ba\u5b50\u985e\u5225\u672a\u5be6\u4f5c\u6240\u6709\u7684\u62bd\u8c61\u65b9\u6cd5\u6216\u5c6c\u6027\u3002\u8acb\u78ba\u4fdd\u6bcf\u500b\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\u5728\u5b50\u985e\u5225\u4e2d\u90fd\u6709\u76f8\u61c9\u7684\u5be6\u4f5c\uff0c\u5426\u5247\u5c07\u6703\u5f15\u767c `TypeError`\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u62bd\u8c61\u57fa\u985e\u4e0d\u50c5\u9069\u7528\u65bc\u5f37\u5236\u57f7\u884c\u65b9\u6cd5\u7684\u5be6\u4f5c\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u5b9a\u7fa9\u63a5\u53e3\u548c\u985e\u5225\u5c64\u6b21\u7d50\u69cb\u3002\u9019\u5c0d\u65bc\u5927\u578b\u5c08\u6848\u7279\u5225\u6709\u5e6b\u52a9\uff0c\u56e0\u70ba\u5b83\u80fd\u5920\u6e05\u6670\u5730\u6307\u5b9a\u54ea\u4e9b\u65b9\u6cd5\u662f\u5fc5\u9700\u7684\uff0c\u5f9e\u800c\u4f7f\u7a0b\u5f0f\u78bc\u67b6\u69cb\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684\u62bd\u8c61\u57fa\u985e\uff0c\u5efa\u8b70\u53c3\u8003\u9019\u7bc7[\u66f4\u8a73\u7d30\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/60d9f3acfd8978000685e8c4)\uff0c\u4ee5\u7372\u53d6\u66f4\u591a\u5be6\u7528\u8cc7\u8a0a\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684\u62bd\u8c61\u57fa\u985e\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u8a2d\u8a08\u6e05\u6670\u4e14\u53ef\u7dad\u8b77\u7684\u7a0b\u5f0f\u78bc\u3002\u900f\u904e\u4f7f\u7528 `@abstractmethod` \u88dd\u98fe\u5668\uff0c\u60a8\u53ef\u4ee5\u5f37\u5236\u5b50\u985e\u5225\u5be6\u4f5c\u5fc5\u8981\u7684\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u7d50\u69cb\u8207\u53ef\u8b80\u6027\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u62bd\u8c61\u57fa\u985e\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA: \u62bd\u8c61\u57fa\u985e\u662f\u7528\u4f86\u5b9a\u7fa9\u4e00\u7d44\u5fc5\u9808\u5728\u5b50\u985e\u5225\u4e2d\u5be6\u4f5c\u7684\u65b9\u6cd5\u548c\u5c6c\u6027\u7684\u985e\u5225\u3002\u5b83\u4e3b\u8981\u7528\u65bc\u5f37\u5236\u57f7\u884c\u63a5\u53e3\u7684\u5be6\u4f5c\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406\u62bd\u8c61\u65b9\u6cd5\u672a\u5be6\u4f5c\u7684\u932f\u8aa4\uff1f**<br \/>\nA: \u78ba\u4fdd\u5728\u6240\u6709\u7e7c\u627f\u81ea\u62bd\u8c61\u57fa\u985e\u7684\u5b50\u985e\u5225\u4e2d\u5be6\u4f5c\u6240\u6709\u7684\u62bd\u8c61\u65b9\u6cd5\uff0c\u5426\u5247\u5c07\u6703\u5f15\u767c `TypeError`\u3002<\/p>\n<p>**Q3: \u62bd\u8c61\u57fa\u985e\u8207\u63a5\u53e3\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA: \u62bd\u8c61\u57fa\u985e\u53ef\u4ee5\u5305\u542b\u5be6\u4f5c\uff0c\u800c\u63a5\u53e3\u50c5\u5b9a\u7fa9\u65b9\u6cd5\u7684\u7c3d\u540d\u3002\u62bd\u8c61\u57fa\u985e\u901a\u5e38\u7528\u65bc\u8a2d\u8a08\u66f4\u5177\u9ad4\u7684\u985e\u5225\u5c64\u6b21\u7d50\u69cb\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u62bd\u8c61\u57fa\u985e(AbstractBaseClass)\u662fPython\u4e2d\u7684\u4e00\u7a2e\u7279\u6b8a\u985e\u5225\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5b9a\u7fa9\u62bd\u8c61\u65b9\u6cd5\uff0c\u4e26\u4e14\u53ef\u4ee5\u88ab\u5176\u4ed6\u985e\u5225\u7e7c\u627f\uff0c\u4ee5\u78ba\u4fdd\u5b50\u985e\u5225\u5b9a\u7fa9\u7684\u65b9\u6cd5\u7b26\u5408\u62bd\u8c61\u57fa\u985e\u7684\u8981\u6c42\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Python\u4e2d\u62bd\u8c61\u57fa\u985e\u7684\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3384","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-SA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3384"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3384\/revisions"}],"predecessor-version":[{"id":3385,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3384\/revisions\/3385"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}