{"id":103,"date":"2026-06-14T09:46:10","date_gmt":"2026-06-14T01:46:10","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=103"},"modified":"2026-06-14T09:46:10","modified_gmt":"2026-06-14T01:46:10","slug":"python3-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/14\/python3-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1\/","title":{"rendered":"Python3 \u9762\u5411\u5bf9\u8c61"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Python 3 \u4e2d\uff0c<strong>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09<\/strong> \u662f\u4e00\u79cd\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u7ed1\u5b9a\u5728\u4e00\u8d77\u7684\u7f16\u7a0b\u8303\u5f0f\u3002Python \u7684 OOP \u8bbe\u8ba1\u54f2\u5b66\u662f <strong>\u201c\u5b9e\u7528\u4e3b\u4e49\u201d<\/strong>\uff1a\u5b83\u652f\u6301\u6240\u6709\u7ecf\u5178\u7684 OOP \u7279\u6027\uff0c\u4f46\u4e0d\u5f3a\u5236\u4f60\u8fc7\u5ea6\u8bbe\u8ba1\uff0c\u540c\u65f6\u63d0\u4f9b\u4e86\u8bb8\u591a\u72ec\u7279\u7684\u8bed\u6cd5\u7cd6\uff08\u5982 <code>@property<\/code>\u3001\u9b54\u672f\u65b9\u6cd5\uff09\u8ba9\u4ee3\u7801\u66f4\u4f18\u96c5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u6211\u5c06\u4ece<strong>\u57fa\u7840\u8bed\u6cd5<\/strong>\u3001<strong>\u56db\u5927\u6838\u5fc3\u7279\u6027<\/strong>\u3001<strong>Python \u4e13\u5c5e\u9ad8\u7ea7\u7279\u6027<\/strong>\u5230<strong>\u73b0\u4ee3\u6700\u4f73\u5b9e\u8df5<\/strong>\uff0c\u4e3a\u4f60\u7cfb\u7edf\u68b3\u7406 Python 3 \u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001 \u57fa\u7840\u8bed\u6cd5\uff1a\u7c7b\u4e0e\u5bf9\u8c61<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Python \u4e2d\uff0c\u4f7f\u7528 <code>class<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\u3002<code>__init__<\/code> \u662f\u6784\u9020\u65b9\u6cd5\uff0c<code>self<\/code> \u4ee3\u8868\u5b9e\u4f8b\u672c\u8eab\uff08\u6ce8\u610f\uff1a<code>self<\/code> \u53ea\u662f\u7ea6\u5b9a\u4fd7\u6210\u7684\u540d\u5b57\uff0c\u4e0d\u662f\u5173\u952e\u5b57\uff0c\u4f46<strong>\u7edd\u5bf9\u4e0d\u8981\u6539\u6389\u5b83<\/strong>\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">class Dog:\n    # 1. \u7c7b\u5c5e\u6027\uff1a\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\n    species = &quot;Canis familiaris&quot;\n\n    # 2. \u5b9e\u4f8b\u65b9\u6cd5\uff1a\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u662f self\n    def __init__(self, name: str, age: int):\n        # \u5b9e\u4f8b\u5c5e\u6027\uff1a\u6bcf\u4e2a\u5b9e\u4f8b\u72ec\u6709\n        self.name = name\n        self.age = age\n\n    def bark(self) -&gt; str:\n        return f&quot;{self.name} says Woof!&quot;\n\n# \u5b9e\u4f8b\u5316\u5bf9\u8c61\nmy_dog = Dog(&quot;Buddy&quot;, 3)\nprint(my_dog.name)       # \u8f93\u51fa: Buddy\nprint(my_dog.bark())     # \u8f93\u51fa: Buddy says Woof!\nprint(my_dog.species)    # \u8f93\u51fa: Canis familiaris (\u4e5f\u53ef\u901a\u8fc7\u7c7b\u540d Dog.species \u8bbf\u95ee)<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001 OOP \u56db\u5927\u6838\u5fc3\u7279\u6027\u5728 Python \u4e2d\u7684\u4f53\u73b0<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u5c01\u88c5 (Encapsulation)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u6ca1\u6709\u4e25\u683c\u7684 <code>private<\/code> \u6216 <code>public<\/code> \u5173\u952e\u5b57\uff0c\u800c\u662f\u4f9d\u9760<strong>\u547d\u540d\u7ea6\u5b9a<\/strong>\u548c<strong>\u540d\u79f0\u4fee\u9970 (Name Mangling)<\/strong> \u6765\u5b9e\u73b0\u5c01\u88c5\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>_attribute<\/code> (\u5355\u4e0b\u5212\u7ebf)\uff1a<strong>\u7ea6\u5b9a\u4fd7\u6210\u7684\u79c1\u6709<\/strong>\u3002\u544a\u8bc9\u5176\u4ed6\u5f00\u53d1\u8005\u201c\u8fd9\u662f\u5185\u90e8\u5b9e\u73b0\uff0c\u8bf7\u52ff\u76f4\u63a5\u8bbf\u95ee\u201d\uff0c\u4f46\u6280\u672f\u4e0a\u4ecd\u53ef\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><code>__attribute<\/code> (\u53cc\u4e0b\u5212\u7ebf)\uff1a<strong>\u540d\u79f0\u4fee\u9970<\/strong>\u3002Python \u4f1a\u5728\u5e95\u5c42\u5c06\u5176\u91cd\u547d\u540d\u4e3a <code>_ClassName__attribute<\/code>\uff0c\u9632\u6b62\u5b50\u7c7b\u610f\u5916\u8986\u76d6\uff0c\u63d0\u4f9b\u8f83\u5f3a\u7684\u79c1\u6709\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">class BankAccount:\n    def __init__(self, owner: str, balance: float):\n        self.owner = owner          # \u516c\u5f00\u5c5e\u6027\n        self._account_id = &quot;12345&quot;  # \u53d7\u4fdd\u62a4\u5c5e\u6027 (\u7ea6\u5b9a\u4e0d\u76f4\u63a5\u8bbf\u95ee)\n        self.__balance = balance    # \u79c1\u6709\u5c5e\u6027 (\u540d\u79f0\u4fee\u9970)\n\n    def deposit(self, amount: float):\n        if amount &gt; 0:\n            self.__balance += amount\n\n    def get_balance(self) -&gt; float:\n        return self.__balance\n\nacc = BankAccount(&quot;Alice&quot;, 1000)\n# print(acc.__balance)  # \u274c AttributeError\nprint(acc.get_balance())  # \u2705 1000\nprint(acc._BankAccount__balance) # \u26a0\ufe0f \u6280\u672f\u4e0a\u53ef\u4ee5\u8fd9\u6837\u8bbf\u95ee\uff0c\u4f46\u5f3a\u70c8\u4e0d\u63a8\u8350<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u7ee7\u627f (Inheritance)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>class Child(Parent):<\/code> \u8bed\u6cd5\u3002\u4f7f\u7528 <code>super()<\/code> \u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">class Animal:\n    def __init__(self, name: str):\n        self.name = name\n\n    def speak(self):\n        raise NotImplementedError(&quot;\u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0\u6b64\u65b9\u6cd5&quot;)\n\nclass Cat(Animal):\n    def __init__(self, name: str, color: str):\n        super().__init__(name)  # \u8c03\u7528\u7236\u7c7b\u7684 __init__\n        self.color = color\n\n    def speak(self):\n        return f&quot;{self.name} says Meow!&quot;\n\nmy_cat = Cat(&quot;Whiskers&quot;, &quot;White&quot;)\nprint(my_cat.speak())  # \u8f93\u51fa: Whiskers says Meow!<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u591a\u6001 (Polymorphism)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u7684\u591a\u6001\u57fa\u4e8e <strong>\u201c\u9e2d\u5b50\u7c7b\u578b\u201d (Duck Typing)<\/strong>\uff1a\u201c\u5982\u679c\u5b83\u8d70\u8d77\u6765\u50cf\u9e2d\u5b50\uff0c\u53eb\u8d77\u6765\u50cf\u9e2d\u5b50\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u9e2d\u5b50\u201d\u3002Python \u4e0d\u5173\u5fc3\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u53ea\u5173\u5fc3\u5b83<strong>\u662f\u5426\u6709\u5bf9\u5e94\u7684\u65b9\u6cd5<\/strong>\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">def make_it_speak(animal: Animal):\n    # \u4e0d\u5173\u5fc3 animal \u662f Dog \u8fd8\u662f Cat\uff0c\u53ea\u8981\u6709 speak() \u65b9\u6cd5\u5c31\u884c\n    print(animal.speak())\n\nmake_it_speak(my_cat)\nmake_it_speak(my_dog)<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">4. \u62bd\u8c61 (Abstraction)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528\u5185\u7f6e\u7684 <code>abc<\/code> (Abstract Base Classes) \u6a21\u5757\u6765\u5f3a\u5236\u5b50\u7c7b\u5b9e\u73b0\u7279\u5b9a\u65b9\u6cd5\uff0c\u9632\u6b62\u5b9e\u4f8b\u5316\u62bd\u8c61\u7c7b\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from abc import ABC, abstractmethod\n\nclass Shape(ABC):\n    @abstractmethod\n    def area(self) -&gt; float:\n        pass\n\n# shape = Shape()  # \u274c TypeError: \u65e0\u6cd5\u5b9e\u4f8b\u5316\u62bd\u8c61\u7c7b\n\nclass Circle(Shape):\n    def __init__(self, radius: float):\n        self.radius = radius\n\n    def area(self) -&gt; float:\n        return 3.14159 * self.radius ** 2<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001 Python \u4e13\u5c5e\u9ad8\u7ea7\u7279\u6027 (\u6781\u5176\u91cd\u8981 \ud83c\udf1f)<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <code>@property<\/code>\uff1a\u4f18\u96c5\u7684 Getter\/Setter<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Java \u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5199 <code>get_balance()<\/code> \u548c <code>set_balance()<\/code>\u3002\u5728 Python \u4e2d\uff0c\u4f7f\u7528 <code>@property<\/code> \u53ef\u4ee5\u5c06\u65b9\u6cd5\u4f2a\u88c5\u6210\u5c5e\u6027\uff0c\u65e2\u4fdd\u6301\u4e86\u5c01\u88c5\u6027\uff0c\u53c8\u8ba9\u8c03\u7528\u8005\u50cf\u8bbf\u95ee\u666e\u901a\u5c5e\u6027\u4e00\u6837\u81ea\u7136\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">class Temperature:\n    def __init__(self, celsius: float):\n        self._celsius = celsius\n\n    @property\n    def celsius(self) -&gt; float:\n        &quot;&quot;&quot;Getter: \u5141\u8bb8\u50cf\u5c5e\u6027\u4e00\u6837\u8bfb\u53d6&quot;&quot;&quot;\n        return self._celsius\n\n    @celsius.setter\n    def celsius(self, value: float):\n        &quot;&quot;&quot;Setter: \u5141\u8bb8\u50cf\u5c5e\u6027\u4e00\u6837\u8d4b\u503c\uff0c\u5e76\u52a0\u5165\u6821\u9a8c\u903b\u8f91&quot;&quot;&quot;\n        if value &lt; -273.15:\n            raise ValueError(&quot;\u6e29\u5ea6\u4e0d\u80fd\u4f4e\u4e8e\u7edd\u5bf9\u96f6\u5ea6&quot;)\n        self._celsius = value\n\ntemp = Temperature(25)\nprint(temp.celsius)  # \u8c03\u7528 getter: 25\ntemp.celsius = 30    # \u8c03\u7528 setter\n# temp.celsius = -300 # \u274c \u4f1a\u629b\u51fa ValueError<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2. <code>@classmethod<\/code> \u4e0e <code>@staticmethod<\/code><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>@classmethod<\/code>\uff1a\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f <code>cls<\/code> (\u7c7b\u672c\u8eab)\u3002\u5e38\u7528\u4e8e<strong>\u66ff\u4ee3\u6784\u9020\u51fd\u6570<\/strong>\uff08\u5de5\u5382\u6a21\u5f0f\uff09\u3002<\/li>\n\n\n\n<li><code>@staticmethod<\/code>\uff1a\u6ca1\u6709 <code>self<\/code> \u6216 <code>cls<\/code> \u53c2\u6570\u3002\u53ea\u662f\u4e00\u4e2a\u653e\u5728\u7c7b\u547d\u540d\u7a7a\u95f4\u91cc\u7684\u666e\u901a\u51fd\u6570\uff0c\u4e0e\u7c7b\u6216\u5b9e\u4f8b\u7684\u72b6\u6001\u65e0\u5173\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">class Date:\n    def __init__(self, year: int, month: int, day: int):\n        self.year = year\n        self.month = month\n        self.day = day\n\n    @classmethod\n    def from_string(cls, date_str: str):\n        &quot;&quot;&quot;\u66ff\u4ee3\u6784\u9020\u51fd\u6570\uff1a\u4ece\u5b57\u7b26\u4e32 &#039;YYYY-MM-DD&#039; \u521b\u5efa\u5bf9\u8c61&quot;&quot;&quot;\n        year, month, day = map(int, date_str.split(&#039;-&#039;))\n        return cls(year, month, day)\n\n    @staticmethod\n    def is_valid_date(year: int, month: int, day: int) -&gt; bool:\n        &quot;&quot;&quot;\u9759\u6001\u65b9\u6cd5\uff1a\u7eaf\u7cb9\u7684\u903b\u8f91\u6821\u9a8c\uff0c\u4e0d\u4f9d\u8d56\u5b9e\u4f8b\u6216\u7c7b\u72b6\u6001&quot;&quot;&quot;\n        return 1 &lt;= month &lt;= 12 and 1 &lt;= day &lt;= 31\n\n# \u4f7f\u7528\u7c7b\u65b9\u6cd5\u521b\u5efa\u5b9e\u4f8b\nmy_date = Date.from_string(&quot;2023-10-25&quot;)\nprint(Date.is_valid_date(2023, 13, 1))  # False<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u9b54\u672f\u65b9\u6cd5 (Dunder Methods)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u65b9\u6cd5\u3002\u5b83\u4eec\u5141\u8bb8\u4f60\u7684\u5bf9\u8c61\u4e0e Python \u7684\u5185\u7f6e\u64cd\u4f5c\u7b26\u65e0\u7f1d\u96c6\u6210\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><span>25<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">class Vector2D:\n    def __init__(self, x: float, y: float):\n        self.x = x\n        self.y = y\n\n    def __str__(self) -&gt; str:\n        return f&quot;Vector({self.x}, {self.y})&quot;  # \u7ed9 print() \u7528\n\n    def __repr__(self) -&gt; str:\n        return f&quot;Vector2D(x={self.x}, y={self.y})&quot;  # \u7ed9\u5f00\u53d1\u8005\u8c03\u8bd5\u7528\n\n    def __add__(self, other: &#039;Vector2D&#039;) -&gt; &#039;Vector2D&#039;:\n        # \u91cd\u8f7d + \u8fd0\u7b97\u7b26\n        return Vector2D(self.x + other.x, self.y + other.y)\n\n    def __eq__(self, other: object) -&gt; bool:\n        # \u91cd\u8f7d == \u8fd0\u7b97\u7b26\n        if not isinstance(other, Vector2D):\n            return False\n        return self.x == other.x and self.y == other.y\n\nv1 = Vector2D(1, 2)\nv2 = Vector2D(3, 4)\nprint(v1 + v2)       # \u8f93\u51fa: Vector(4, 6)  (\u89e6\u53d1\u4e86 __str__)\nprint(v1 == v2)      # \u8f93\u51fa: False<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001 \u73b0\u4ee3 Python 3 \u6700\u4f73\u5b9e\u8df5\uff1a<code>@dataclass<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5199\u7c7b\u7684\u76ee\u7684<strong>\u4ec5\u4ec5\u662f\u4e3a\u4e86\u5b58\u50a8\u6570\u636e<\/strong>\uff08\u6ca1\u6709\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\uff09\uff0c\u4ece Python 3.7 \u5f00\u59cb\uff0c<strong>\u5f3a\u70c8\u63a8\u8350\u4f7f\u7528 <code>@dataclass<\/code><\/strong>\u3002\u5b83\u4f1a\u81ea\u52a8\u4e3a\u4f60\u751f\u6210 <code>__init__<\/code>\u3001<code>__repr__<\/code>\u3001<code>__eq__<\/code> \u7b49\u65b9\u6cd5\uff0c\u5927\u5e45\u51cf\u5c11\u6837\u677f\u4ee3\u7801\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from dataclasses import dataclass, field\nfrom typing import List\n\n@dataclass\nclass User:\n    name: str\n    age: int\n    # \u9ed8\u8ba4\u503c\u5982\u679c\u662f\u53ef\u53d8\u5bf9\u8c61\uff0c\u5fc5\u987b\u4f7f\u7528 field(default_factory=...)\n    tags: List[str] = field(default_factory=list) \n\n    # \u4f9d\u7136\u53ef\u4ee5\u5b9a\u4e49\u666e\u901a\u65b9\u6cd5\n    def add_tag(self, tag: str):\n        self.tags.append(tag)\n\n# \u81ea\u52a8\u62e5\u6709\u4e86\u5b8c\u7f8e\u7684 __init__ \u548c __repr__\nu = User(&quot;Alice&quot;, 25)\nprint(u)  # \u8f93\u51fa: User(name=&#039;Alice&#039;, age=25, tags=[])<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e94\u3001 \u5e38\u89c1\u9677\u9631\u4e0e\u907f\u5751\u6307\u5357<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u53ef\u53d8\u9ed8\u8ba4\u53c2\u6570\u9677\u9631<\/strong>\uff08\u5728 <code>__init__<\/code> \u4e2d\uff09\uff1a<\/li>\n<\/ol>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">   # \u274c \u9519\u8bef\uff1a\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u540c\u4e00\u4e2a\u5217\u8868\n   class Team:\n       def __init__(self, members=[]):\n           self.members = members\n\n   # \u2705 \u6b63\u786e\uff1a\u4f7f\u7528 None \u5e76\u5728\u5185\u90e8\u521d\u59cb\u5316\n   class Team:\n       def __init__(self, members=None):\n           self.members = members if members is not None else []<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5fd8\u8bb0\u8c03\u7528 <code>super().__init__()<\/code><\/strong>\uff1a\u5728\u591a\u91cd\u7ee7\u627f\u6216\u590d\u6742\u7ee7\u627f\u94fe\u4e2d\uff0c\u5b50\u7c7b <code>__init__<\/code> \u5fc5\u987b\u663e\u5f0f\u8c03\u7528 <code>super().__init__()<\/code>\uff0c\u5426\u5219\u7236\u7c7b\u7684\u521d\u59cb\u5316\u903b\u8f91\u4e0d\u4f1a\u6267\u884c\u3002<\/li>\n\n\n\n<li><strong>\u8fc7\u5ea6\u4f7f\u7528 OOP<\/strong>\uff1aPython \u652f\u6301\u591a\u8303\u5f0f\u3002\u5982\u679c\u4e00\u4e2a\u529f\u80fd\u7528\u51e0\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5c31\u80fd\u89e3\u51b3\uff0c<strong>\u4e0d\u8981\u5f3a\u884c\u628a\u5b83\u585e\u8fdb\u4e00\u4e2a\u7c7b\u91cc<\/strong>\u3002Python \u4e4b\u7985\uff1a\u201c\u7b80\u5355\u4f18\u4e8e\u590d\u6742\u201d\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u7684\u9762\u5411\u5bf9\u8c61\u65e2\u4fdd\u7559\u4e86\u7ecf\u5178 OOP \u7684\u4e25\u8c28\uff08\u901a\u8fc7 <code>abc<\/code> \u548c\u7ee7\u627f\uff09\uff0c\u53c8\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\uff08\u9e2d\u5b50\u7c7b\u578b\u3001<code>@property<\/code>\u3001\u9b54\u672f\u65b9\u6cd5\uff09\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b58\u50a8\u7eaf\u6570\u636e \ud83d\udc49 \u4f7f\u7528 <strong><code>@dataclass<\/code><\/strong><\/li>\n\n\n\n<li>\u9700\u8981\u63a7\u5236\u5c5e\u6027\u8bbf\u95ee \ud83d\udc49 \u4f7f\u7528 <strong><code>@property<\/code><\/strong><\/li>\n\n\n\n<li>\u9700\u8981\u4e0e\u5185\u7f6e\u64cd\u4f5c\u7b26\u4ea4\u4e92 \ud83d\udc49 \u5b9e\u73b0 <strong>\u9b54\u672f\u65b9\u6cd5 (<code>__xx__<\/code>)<\/strong><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u60f3\u6df1\u5165\u63a2\u8ba8\u54ea\u4e2a\u5177\u4f53\u573a\u666f\uff1f\u6bd4\u5982\uff1a<strong>\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u5355\u4f8b\u6a21\u5f0f (Singleton)<\/strong>\uff0c\u6216\u8005 <strong><code>@dataclass<\/code> \u7684\u9ad8\u7ea7\u7528\u6cd5\uff08\u5982 <code>__post_init__<\/code>\uff09<\/strong>\uff1f\u968f\u65f6\u544a\u8bc9\u6211\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python 3 \u4e2d\uff0c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/103\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}