{"id":3252,"date":"2025-05-29T15:39:49","date_gmt":"2025-06-04T08:27:49","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3252"},"modified":"2025-06-04T15:39:49","modified_gmt":"2025-06-04T08:27:49","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","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\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3Python\u4e2d\u7684\u62bd\u8c61\u57fa\u985e\uff08Abstract Base Class\uff09\u8207\u6700\u4f73\u5be6\u8e10\uff082025\u6700\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, abstract base class, Python \u62bd\u8c61\u57fa\u5e95\u985e\u5225, Python \u6559\u5b78, Python 2025\"><\/p>\n<h1>\u4ec0\u9ebc\u662fPython\u7684\u62bd\u8c61\u57fa\u5e95\u985e\u5225\uff08Abstract Base Class\uff09\uff1f<\/h1>\n<p>Python\u7684\u62bd\u8c61\u57fa\u5e95\u985e\u5225\uff08ABC\uff09\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u985e\u5225\uff0c\u7528\u65bc\u5b9a\u7fa9\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\u3002\u9019\u4e9b\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\u5fc5\u9808\u5728\u5b50\u985e\u5225\u4e2d\u5be6\u73fe\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5b50\u985e\u5225\u9075\u5faa\u7279\u5b9a\u7684\u63a5\u53e3\u8a2d\u8a08\u3002\u4f7f\u7528\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u7684\u4e3b\u8981\u76ee\u7684\u5728\u65bc\u63d0\u4f9b\u4e00\u500b\u53ef\u4ee5\u88ab\u5b50\u985e\u5225\u7e7c\u627f\u7684\u6846\u67b6\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5f37\u5236\u5b50\u985e\u5225\u5be6\u73fe\u7279\u5b9a\u7684\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u5f9e\u800c\u4fc3\u9032\u4ee3\u78bc\u7684\u4e00\u81f4\u6027\u548c\u91cd\u7528\u6027\u3002<\/p>\n<h2>Python\u4e2d\u7684\u62bd\u8c61\u57fa\u5e95\u985e\u5225\uff08ABC\uff09\u7684\u4f7f\u7528<\/h2>\n<p>\u8981\u5728Python\u4e2d\u4f7f\u7528\u62bd\u8c61\u57fa\u5e95\u985e\u5225\uff0c\u9996\u5148\u9700\u8981\u5c0e\u5165`abc`\u6a21\u7d44\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport abc<br \/>\n&#8220;`<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528`abc`\u6a21\u7d44\u4f86\u5b9a\u7fa9\u4e00\u500b\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u3002\u4ee5\u4e0b\u662f\u5b9a\u7fa9\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyAbstractBaseClass(abc.ABC):<br \/>\n    @abc.abstractmethod<br \/>\n    def my_abstract_method(self):<br \/>\n        &#8220;&#8221;&#8221;\u9019\u662f\u4e00\u500b\u62bd\u8c61\u65b9\u6cd5\uff0c\u5b50\u985e\u5225\u5fc5\u9808\u5be6\u73fe\u6b64\u65b9\u6cd5\u3002&#8221;&#8221;&#8221;<br \/>\n        pass<\/p>\n<p>    @property<br \/>\n    @abc.abstractmethod<br \/>\n    def my_abstract_property(self):<br \/>\n        &#8220;&#8221;&#8221;\u9019\u662f\u4e00\u500b\u62bd\u8c61\u5c6c\u6027\uff0c\u5b50\u985e\u5225\u5fc5\u9808\u5be6\u73fe\u6b64\u5c6c\u6027\u3002&#8221;&#8221;&#8221;<br \/>\n        pass<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c`my_abstract_method` \u548c `my_abstract_property` \u90fd\u662f\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\u3002\u4efb\u4f55\u7e7c\u627f`MyAbstractBaseClass`\u7684\u5b50\u985e\u5225\u5fc5\u9808\u5be6\u73fe\u9019\u4e9b\u65b9\u6cd5\uff0c\u5426\u5247\u5c07\u7121\u6cd5\u5be6\u4f8b\u5316\u8a72\u5b50\u985e\u5225\u3002<\/p>\n<h3>\u5be6\u4f5c\u7bc4\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u5be6\u73fe\u4e00\u500b\u5b50\u985e\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MySubClass(MyAbstractBaseClass):<br \/>\n    def my_abstract_method(self):<br \/>\n        return &#8220;Abstract method implemented.&#8221;<\/p>\n<p>    @property<br \/>\n    def my_abstract_property(self):<br \/>\n        return &#8220;Abstract property implemented.&#8221;<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u6aa2\u67e5`MySubClass`\u662f\u5426\u6b63\u78ba\u7e7c\u627f\u4e86`MyAbstractBaseClass`\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(issubclass(MySubClass, MyAbstractBaseClass))  # \u8f38\u51fa: True<\/p>\n<p>instance = MySubClass()<br \/>\nprint(instance.my_abstract_method())  # \u8f38\u51fa: Abstract method implemented.<br \/>\nprint(instance.my_abstract_property)    # \u8f38\u51fa: Abstract property implemented.<br \/>\n&#8220;`<\/p>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5982\u679c\u4e00\u500b\u985e\u5225\u7e7c\u627f\u81ea\u62bd\u8c61\u57fa\u5e95\u985e\u5225\uff0c\u4f46\u6c92\u6709\u5be6\u73fe\u5176\u6240\u6709\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u5247\u6703\u5f15\u767c`TypeError`\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass IncompleteSubClass(MyAbstractBaseClass):<br \/>\n    pass<\/p>\n<p># \u9019\u5c07\u5c0e\u81f4\u932f\u8aa4<br \/>\n# instance = IncompleteSubClass()  # TypeError: Can&#8217;t instantiate abstract class IncompleteSubClass with abstract methods my_abstract_method, my_abstract_property<br \/>\n&#8220;`<\/p>\n<p>\u78ba\u4fdd\u5728\u5b50\u985e\u4e2d\u5be6\u73fe\u6240\u6709\u7684\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u4ee5\u907f\u514d\u6b64\u985e\u932f\u8aa4\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u4f7f\u7528\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u7684\u8a2d\u8a08\u6a21\u5f0f\uff0c\u53ef\u4ee5\u8b93\u60a8\u5728\u5927\u578b\u9805\u76ee\u4e2d\u4fdd\u6301\u4ee3\u78bc\u7684\u7d50\u69cb\u6027\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5275\u5efa\u4e00\u7d44\u76f8\u95dc\u7684\u6578\u64da\u8655\u7406\u985e\u5225\uff0c\u9019\u4e9b\u985e\u5225\u90fd\u7e7c\u627f\u81ea\u540c\u4e00\u500b\u62bd\u8c61\u57fa\u5e95\u985e\u5225\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5b83\u5011\u90fd\u5be6\u73fe\u4e86\u76f8\u540c\u7684\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u5f9e\u800c\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>\u6b32\u4e86\u89e3\u66f4\u591aPython\u7de8\u7a0b\u6280\u5de7\uff0c\u8acb\u53c3\u8003[\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python\u7684\u62bd\u8c61\u57fa\u5e95\u985e\u5225\uff08ABC\uff09\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u5b9a\u7fa9\u6e05\u6670\u7684\u63a5\u53e3\uff0c\u4e26\u4fc3\u9032\u4ee3\u78bc\u7684\u91cd\u7528\u8207\u4e00\u81f4\u6027\u3002\u5728\u4f7f\u7528\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u6642\uff0c\u8acb\u78ba\u4fdd\u5b50\u985e\u5be6\u73fe\u6240\u6709\u7684\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u4ee5\u907f\u514d\u904b\u884c\u6642\u932f\u8aa4\u3002\u5be6\u73fe\u6b63\u78ba\u7684\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u8a2d\u8a08\uff0c\u53ef\u4ee5\u4f7f\u60a8\u7684\u4ee3\u78bc\u66f4\u52a0\u5065\u58ef\u548c\u6613\u65bc\u7dad\u8b77\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u4ec0\u9ebc\u662f\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u7684\u4e3b\u8981\u512a\u52e2\uff1f**<br \/>\nA1: \u62bd\u8c61\u57fa\u5e95\u985e\u5225\u63d0\u4f9b\u4e86\u4e00\u7a2e\u5f37\u5236\u5b50\u985e\u5be6\u73fe\u7279\u5b9a\u65b9\u6cd5\u548c\u5c6c\u6027\u7684\u65b9\u5f0f\uff0c\u5f9e\u800c\u78ba\u4fdd\u4ee3\u78bc\u7684\u4e00\u81f4\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406\u5b50\u985e\u4e2d\u672a\u5be6\u73fe\u62bd\u8c61\u65b9\u6cd5\u7684\u60c5\u6cc1\uff1f**<br \/>\nA2: \u82e5\u5b50\u985e\u672a\u5be6\u73fe\u62bd\u8c61\u65b9\u6cd5\uff0c\u5247\u5728\u5be6\u4f8b\u5316\u8a72\u5b50\u985e\u6642\u6703\u5f15\u767c`TypeError`\uff0c\u958b\u767c\u8005\u9700\u8981\u78ba\u4fdd\u6240\u6709\u5fc5\u8981\u7684\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\u5747\u5df2\u5be6\u73fe\u3002<\/p>\n<p>**Q3: \u62bd\u8c61\u57fa\u5e95\u985e\u5225\u53ef\u4ee5\u8207\u5176\u4ed6\u985e\u578b\u7684\u985e\u5225\u5171\u5b58\u55ce\uff1f**<br \/>\nA3: \u662f\u7684\uff0c\u62bd\u8c61\u57fa\u5e95\u985e\u5225\u53ef\u4ee5\u8207\u5176\u4ed6\u666e\u901a\u985e\u5225\u5171\u5b58\uff0c\u5b83\u5011\u53ef\u4ee5\u4e00\u8d77\u7528\u65bc\u69cb\u5efa\u8907\u96dc\u7684\u985e\u5225\u7d50\u69cb\u548c\u8a2d\u8a08\u6a21\u5f0f\u3002<br \/>\n&#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\u88ab\u7528\u4f86\u5b9a\u7fa9\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\uff0c\u4e26\u4e14\u53ef\u4ee5\u88ab\u7528\u4f86\u6aa2\u67e5\u5b50\u985e\u5225\u662f\u5426\u6b63\u78ba\u5be6\u73fe\u4e86\u62bd\u8c61\u65b9\u6cd5\u548c\u5c6c\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39Python\u4e2d\u62bd\u8c61\u57fa\u985e\u7684\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u69cb\u5efa\u66f4\u5065\u58ef\u7684\u4ee3\u78bc\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-3252","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-Qs","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3252","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=3252"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3252\/revisions"}],"predecessor-version":[{"id":3253,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3252\/revisions\/3253"}],"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=3252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}