{"id":231119,"date":"2021-12-12T09:31:31","date_gmt":"2021-12-12T01:31:31","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=231119"},"modified":"2021-11-28T17:32:35","modified_gmt":"2021-11-28T09:32:35","slug":"python-polymorphic-protocol","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-polymorphic-protocol.html","title":{"rendered":"python \u591a\u6001 \u534f\u8bae\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86python \u591a\u6001 \u534f\u8bae \u9e2d\u5b50\u7c7b\u578b\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u4f60\u5e26\u6765\u5e2e\u52a9<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u63a5\u53e3(python \u4e2d\u7684\u534f\u8bae)\u7684\u591a\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5373\u4e3a\u591a\u6001\u3002\u591a\u6001\u7684\u4f5c\u7528\uff0c\u5c31\u662f\u4e3a\u4e86\u7c7b\u5728\u7ee7\u627f\u548c\u6d3e\u751f\u7684\u65f6\u5019\uff0c\u4fdd\u8bc1\u4f7f\u7528\u201c\u5bb6\u8c31\u201d\u4e2d\u4efb\u4e00\u7c7b\u7684\u5b9e\u4f8b\u7684\u67d0\u4e00\u5c5e\u6027\u65f6\u7684\u6b63\u786e\u8c03\u7528\u3002<\/p>\n

\r\nfrom abc import ABCMeta, abstractmethod\r\n# \u9e2d\u5b50\u7c7b\r\nclass Dock(metaclass=ABCMeta):\r\n    @abstractmethod\r\n    def Swimming(self):  # \u6e38\u6cf3\u65b9\u6cd5\u534f\u8bae\uff08\u63a5\u53e3\uff09\r\n        pass\r\n    @abstractmethod  # \u8d70\u8def\u534f\u8bae\uff08\u63a5\u53e3\uff09\r\n    def Walk(self):\r\n        pass\r\n    @classmethod\r\n    def __subclasshook__(cls, C):\r\n        # \u5224\u65ad\u662f\u5426\u53e6\u4e00\u4e2a\u6bd4\u8f83\u7c7b\u662f\u5426\u5b9e\u73b0\u4e86 Swimming Walk \u534f\u8bae\uff0c \u5982\u679c\u5b9e\u73b0\u4e86\u9e2d\u5b50\u7c7b\u7684\u8fd9\u4e24\u4e2a\u534f\u8bae\uff0c\r\n        # \u90a3\u4e48\u6bd4\u8f83\u7c7b\u7684\u7c7b\u578b\u5c31\u662f\u4e00\u4e2a\u9e2d\u5b50\u7c7b\u578b\r\n        # \u5f53\u4ee3\u7801\u6267\u884c\u4e2d\u5982\u679c\u6267\u884c\u5230\u5bf9\u8c61\u548c\u8fd9\u4e2a\u7c7b\u8fdb\u884c isinstance \u7c7b\u578b\u5224\u65ad\u65f6\u4f1a\u8d70\u5230\u8fd9\u4e2a\u51fd\u6570\u8fdb\u884c\u5224\u65ad\r\n        for method in ('Swimming', 'Walk'):\r\n            for B in C.__mro__:\r\n                if method in B.__dict__:\r\n                    if B.__dict__[method] is None:\r\n                        return NotImplemented\r\n                    break\r\n            else:\r\n                return NotImplemented\r\n        return True\r\n# \u72d7\u7c7b\r\nclass Dog(object):\r\n    # \u5b9e\u73b0swimming \u534f\u8bae\r\n    def Swimming(self):\r\n        print(\"\u72d7\u4f1a\u72d7\u5228\")\r\n    # \u5b9e\u73b0walk \u534f\u8bae\r\n    def Walk(self):\r\n        print(\"\u72d7\u4f1a\u8d70\u8def\")\r\n    def Eat(self):\r\n        print(\"\u72d7\u559c\u6b22\u5403\u9aa8\u5934\")\r\n# \u4e4c\u9f9f\u7c7b\r\nclass Tortoise(object):\r\n    # \u5b9e\u73b0swimming \u534f\u8bae\r\n    def Swimming(self):\r\n        print(\"\u4e4c\u9f9f\u4f1a\u6f5c\u6c34\")\r\n    # \u5b9e\u73b0walk \u534f\u8bae\r\n    def Walk(self):\r\n        print(\"\u4e4c\u9f9f\u4f1a\u8d70\u8def\")\r\n    def Eat(self):\r\n        print(\"\u4e4c\u9f9f\u559c\u6b22\u5403\u9c7c\")\r\n \r\ndog = Dog()\r\ntortoise = Tortoise()\r\nprint(isinstance(dog, Dock))         # True\r\nprint(isinstance(tortoise, Dock))    # True\r\n<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u53ea\u8981\u5b9e\u73b0\u4e86 Dock \u7c7b\u4e2d\u7684 swimming \u548c Walk \u65b9\u6cd5\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7c7b\u5c31\u53ef\u4ee5\u88ab\u53eb\u505a Dock \u7c7b<\/p>\n

\u5e94\u7528\u573a\u666f \u5982\uff1a for \u5faa\u73af\uff0c \u5728python \u4e2d for \u5faa\u73af\u53ea\u80fd\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c \u90a3\u4e48\uff0c \u6211\u81ea\u5df1\u5b9a\u4e49\u7684\u7c7b\u5b9e\u73b0\u4e86 __iter__\u534f\u8bae\uff08\u63a5\u53e3\uff09\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u7c7b\u5c31\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u53ef\u4ee5\u88abfor \u5faa\u73af\u4f7f\u7528<\/p>\n

python \u4e2d\u5b9a\u4e49\u534f\u8bae\u7c7b\u534f\u8bae\u4f7f\u7528 @abstractmethod \u88c5\u9970\u5668\uff0c@abstractmethod \u88c5\u9970\u8fc7\u7684\u7c7b\u662f\u4e0d\u80fd\u8fdb\u884c\u521d\u59cb\u5316\u7684\uff0c\u76f8\u5bf9\u4e8ec++\u4e2d\u7684\u7eaf\u865a\u51fd\u6570\u7c7b<\/p>\n

\u8fd9\u4e2a\u7c7b\u53ea\u80fd\u5f53\u505a\u534f\u8bae\uff08\u63a5\u53e3\uff09\u7c7b<\/p>\n","protected":false},"excerpt":{"rendered":"

\u63a5\u53e3(python \u4e2d\u7684\u534f\u8bae)\u7684\u591a\u79cd\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\u5373\u4e3a\u591a\u6001\u3002\u591a\u6001\u7684\u4f5c\u7528\uff0c\u5c31\u662f\u4e3a\u4e86\u7c7b\u5728\u7ee7\u627f\u548c\u6d3e\u751f\u7684\u65f6\u5019\uff0c\u4fdd\u8bc1\u4f7f\u7528 […]<\/p>\n","protected":false},"author":362,"featured_media":118960,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-231119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=231119"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231119\/revisions"}],"predecessor-version":[{"id":231121,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231119\/revisions\/231121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/118960"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=231119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=231119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=231119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}