{"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":"
\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 |