{"id":2092,"date":"2024-11-04T16:05:12","date_gmt":"2024-11-04T08:05:12","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=2092"},"modified":"2024-11-04T16:05:12","modified_gmt":"2024-11-04T08:05:12","slug":"python%e4%b8%ad%e5%b8%b8%e7%94%a8%e7%9a%84%e8%a3%85%e9%a5%b0%e5%99%a8classmethod%e3%80%81abstractmethod%e3%80%81property%e5%92%8cstaticmethod","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2024\/11\/04\/python%e4%b8%ad%e5%b8%b8%e7%94%a8%e7%9a%84%e8%a3%85%e9%a5%b0%e5%99%a8classmethod%e3%80%81abstractmethod%e3%80%81property%e5%92%8cstaticmethod\/","title":{"rendered":"Python\u4e2d\u5e38\u7528\u7684\u88c5\u9970\u5668@classmethod\u3001@abstractmethod\u3001@property\u548c@staticmethod"},"content":{"rendered":"\n<p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u88c5\u9970\u5668\u662f\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u51fd\u6570\u6216\u7c7b\u7684\u884c\u4e3a\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u4e2a\u5e38\u7528\u7684\u88c5\u9970\u5668\uff0c\u5305\u62ec<code>@classmethod<\/code>\u3001<code>@abstractmethod<\/code>\u3001<code>@property<\/code>\u548c<code>@staticmethod<\/code>\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\uff0c\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5b83\u4eec\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"culr5\"><strong>@classmethod<\/strong><\/h3>\n\n\n\n<p><code>@classmethod<\/code>\u88c5\u9970\u5668\u7528\u4e8e\u5b9a\u4e49\u7c7b\u65b9\u6cd5\uff08classmethods\uff09\u3002\u7c7b\u65b9\u6cd5\u4e0e\u666e\u901a\u65b9\u6cd5\u4e0d\u540c\uff0c\u5b83\u5728\u7c7b\u5c42\u7ea7\u4e0a\u64cd\u4f5c\uff0c\u800c\u4e0d\u662f\u5728\u5b9e\u4f8b\u5c42\u7ea7\u4e0a\u3002\u901a\u8fc7\u7c7b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u65b9\u6cd5\uff0c\u800c\u65e0\u9700\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>@classmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u7c7b\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MathUtils:\n    @classmethod\n    def multiply(cls, a, b):\n        return a * b\n\nresult = MathUtils.multiply(5, 3)\nprint(result)  # \u8f93\u51fa: 15\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>MathUtils<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b\u65b9\u6cd5<code>multiply<\/code>\uff0c\u901a\u8fc7<code>@classmethod<\/code>\u88c5\u9970\u5668\u6807\u8bb0\u3002\u7c7b\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u901a\u5e38\u88ab\u547d\u540d\u4e3a<code>cls<\/code>\uff0c\u5b83\u6307\u5411\u7c7b\u672c\u8eab\u3002\u901a\u8fc7\u7c7b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u7c7b\u5c42\u7ea7\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c\u65e0\u9700\u5b9e\u4f8b\u5316\u7c7b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"d914l\"><strong>@abstractmethod<\/strong><\/h3>\n\n\n\n<p><code>@abstractmethod<\/code>\u88c5\u9970\u5668\u7528\u4e8e\u5b9a\u4e49\u62bd\u8c61\u65b9\u6cd5\uff08abstract methods\uff09\u3002\u62bd\u8c61\u65b9\u6cd5\u5728\u57fa\u7c7b\u4e2d\u58f0\u660e\u4f46\u6ca1\u6709\u5177\u4f53\u5b9e\u73b0\uff0c\u9700\u8981\u5728\u6d3e\u751f\u7c7b\u4e2d\u8fdb\u884c\u5b9e\u73b0\u3002\u5982\u679c\u4e00\u4e2a\u7c7b\u4e2d\u5305\u542b\u62bd\u8c61\u65b9\u6cd5\uff0c\u90a3\u4e48\u8be5\u7c7b\u5fc5\u987b\u58f0\u660e\u4e3a\u62bd\u8c61\u7c7b\uff0c\u65e0\u6cd5\u5b9e\u4f8b\u5316\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>@abstractmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u62bd\u8c61\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from abc import ABC, abstractmethod\n\nclass Shape(ABC):\n    @abstractmethod\n    def area(self):\n        pass\n\nclass Circle(Shape):\n    def __init__(self, radius):\n        self.radius = radius\n    \n    def area(self):\n        return 3.14 * self.radius ** 2\n\n# \u521b\u5efa Circle \u5bf9\u8c61\ncircle = Circle(5)\nprint(circle.area())  # \u8f93\u51fa: 78.5\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>Shape<\/code>\u7c7b\u662f\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5<code>area<\/code>\u3002\u901a\u8fc7\u4f7f\u7528<code>@abstractmethod<\/code>\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u58f0\u660e<code>area<\/code>\u65b9\u6cd5\u4e3a\u62bd\u8c61\u65b9\u6cd5\uff0c\u65e0\u9700\u63d0\u4f9b\u5177\u4f53\u5b9e\u73b0\u3002\u6d3e\u751f\u7c7b<code>Circle<\/code>\u7ee7\u627f\u4e86<code>Shape<\/code>\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86<code>area<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u5177\u6709\u7279\u5b9a\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2b2ui\"><strong>@property<\/strong><\/h3>\n\n\n\n<p><code>@property<\/code>\u88c5\u9970\u5668\u7528\u4e8e\u5c06\u4e00\u4e2a\u7c7b\u65b9\u6cd5\u8f6c\u6362\u4e3a\u53ea\u8bfb\u5c5e\u6027\uff08read-only property\uff09\u3002\u901a\u8fc7\u4f7f\u7528<code>@property<\/code>\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u4f7f\u5176\u5728\u4f7f\u7528\u70b9\u7b26\u53f7\u8bbf\u95ee\u65f6\uff0c\u50cf\u8bbf\u95ee\u5c5e\u6027\u4e00\u6837\uff0c\u800c\u4e0d\u662f\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>@property<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u5c5e\u6027\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Person:\n    def __init__(self, name):\n        self._name = name\n    \n    @property\n    def name(self):\n        return self._name\n\n# \u521b\u5efa Person \u5bf9\u8c61\nperson = Person(\"John\")\nprint(person.name)  # \u8f93\u51fa: John\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>Person<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c5e\u6027<code>name<\/code>\uff0c\u4f7f\u7528<code>@property<\/code>\u88c5\u9970\u5668\u5c06<code>name<\/code>\u65b9\u6cd5\u8f6c\u6362\u4e3a\u53ea\u8bfb\u5c5e\u6027\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027\u65b9\u5f0f\u8bbf\u95ee<code>name<\/code>\uff0c\u800c\u65e0\u9700\u663e\u5f0f\u8c03\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5mfgj\"><strong>@staticmethod<\/strong><\/h3>\n\n\n\n<p><code>@staticmethod<\/code>\u88c5\u9970\u5668\u7528\u4e8e\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\uff08staticmethods\uff09\u3002\u9759\u6001\u65b9\u6cd5\u5728\u7c7b\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u5b9a\u4e49\uff0c\u4e0e\u7c7b\u7684\u5b9e\u4f8b\u65e0\u5173\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u901a\u8fc7\u5b9e\u4f8b\u6765\u8c03\u7528\u3002\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>@staticmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\u9759\u6001\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MathUtils:\n    @staticmethod\n    def add(a, b):\n        return a + b\n\nresult = MathUtils.add(5, 3)\nprint(result)  # \u8f93\u51fa: 8\n<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>MathUtils<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5<code>add<\/code>\uff0c\u901a\u8fc7<code>@staticmethod<\/code>\u88c5\u9970\u5668\u6807\u8bb0\u3002\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\uff0c\u65e0\u9700\u5b9e\u4f8b\u5316\u7c7b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fc4ph\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p>\u88c5\u9970\u5668\u662fPython\u4e2d\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u4f18\u5316\u4ee3\u7801\u7ed3\u6784\u3001\u63d0\u4f9b\u989d\u5916\u529f\u80fd\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u672c\u6587\u4ecb\u7ecd\u4e86<code>@classmethod<\/code>\u3001<code>@abstractmethod<\/code>\u3001<code>@property<\/code>\u548c<code>@staticmethod<\/code>\u8fd9\u51e0\u4e2a\u88c5\u9970\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n\n\n\n<p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u80fd\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u88c5\u9970\u5668\u7684\u4f5c\u7528\uff0c\u5e76\u5728\u81ea\u5df1\u7684\u4ee3\u7801\u4e2d\u7075\u6d3b\u5e94\u7528\u5b83\u4eec\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u7f16\u7a0b\u4e2d\uff0c\u88c5\u9970\u5668\u662f\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u4fee\u6539\u51fd\u6570\u6216\u7c7b\u7684\u884c\u4e3a\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-2092","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2092"}],"version-history":[{"count":1,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2092\/revisions"}],"predecessor-version":[{"id":2093,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/2092\/revisions\/2093"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2092"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}