{"id":938929,"date":"2024-12-26T20:22:12","date_gmt":"2024-12-26T12:22:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/938929.html"},"modified":"2024-12-26T20:22:14","modified_gmt":"2024-12-26T12:22:14","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e8%ae%a1%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/938929.html","title":{"rendered":"Python\u5982\u4f55\u8bbe\u8ba1\u7c7b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074206\/ed9628d6-7010-402e-a03d-32cd53f2df24.webp\" alt=\"Python\u5982\u4f55\u8bbe\u8ba1\u7c7b\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bbe\u8ba1\u7c7b\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u5b9a\u4e49\u7c7b\u7684\u540d\u79f0\u3001\u521b\u5efa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3001\u5b9a\u4e49\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3001\u5b9e\u73b0\u7ee7\u627f\u4e0e\u591a\u6001\u3001\u4ee5\u53ca\u4f7f\u7528\u7279\u6b8a\u65b9\u6cd5\u548c\u5c5e\u6027\u6765\u589e\u5f3a\u7c7b\u7684\u529f\u80fd\u3002<\/strong>\u4e3a\u4e86\u8be6\u7ec6\u8bf4\u660e\u5176\u4e2d\u7684\u4e00\u4e2a\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u6df1\u5165\u4e86\u89e3\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002\u5728Python\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u662f\u901a\u8fc7<code>__init__<\/code>\u65b9\u6cd5\u5b9e\u73b0\u7684\uff0c\u5b83\u7528\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c<code>__init__<\/code>\u65b9\u6cd5\u4f1a\u81ea\u52a8\u8c03\u7528\uff0c\u786e\u4fdd\u5bf9\u8c61\u5728\u4f7f\u7528\u524d\u88ab\u6b63\u786e\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u4f20\u9012\u53c2\u6570\uff0c\u4ece\u800c\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a<code>Person<\/code>\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u63a5\u53d7\u540d\u5b57\u548c\u5e74\u9f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u8d4b\u7ed9\u5b9e\u4f8b\u7684\u5c5e\u6027\u3002\u8fd9\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u4f7f\u5f97\u7c7b\u7684\u4f7f\u7528\u66f4\u52a0\u7075\u6d3b\u548c\u76f4\u89c2\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u7c7b\u7684\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u662f\u901a\u8fc7<code>class<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u7684\u3002\u7c7b\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u7840\u5355\u5143\uff0c\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u5177\u6709\u76f8\u4f3c\u7279\u5f81\u548c\u884c\u4e3a\u7684\u5bf9\u8c61\u3002\u4e00\u4e2a\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5bf9\u8c61\u7684\u84dd\u56fe\uff0c\u5305\u62ec\u5b83\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b9a\u4e49\u7c7b\u7684\u540d\u79f0<\/h4>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u65f6\uff0c\u9996\u5148\u9700\u8981\u4e3a\u5b83\u6307\u5b9a\u4e00\u4e2a\u540d\u79f0\u3002\u7c7b\u540d\u901a\u5e38\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u9a7c\u5cf0\u5f0f\u547d\u540d\u6cd5\uff0c\u4ee5\u4fbf\u4e8e\u533a\u5206\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684<code>Person<\/code>\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Person<\/code>\u662f\u7c7b\u7684\u540d\u79f0\uff0c<code>pass<\/code>\u662f\u4e00\u4e2a\u5360\u4f4d\u7b26\uff0c\u8868\u793a\u8fd9\u4e2a\u7c7b\u76ee\u524d\u6ca1\u6709\u4efb\u4f55\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u7c7b\u7684\u6784\u9020\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u662f\u7c7b\u7684\u4e00\u4e2a\u7279\u6b8a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u5bf9\u8c61\u521b\u5efa\u65f6\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002Python\u4e2d\u7684\u6784\u9020\u51fd\u6570\u662f\u901a\u8fc7<code>__init__<\/code>\u65b9\u6cd5\u5b9e\u73b0\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>__init__<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570<code>name<\/code>\u548c<code>age<\/code>\uff0c\u5e76\u5c06\u5b83\u4eec\u8d4b\u7ed9\u5b9e\u4f8b\u5c5e\u6027<code>self.name<\/code>\u548c<code>self.age<\/code>\u3002\u4f7f\u7528\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u786e\u4fdd\u5bf9\u8c61\u5728\u4f7f\u7528\u524d\u88ab\u6b63\u786e\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u7279\u5f81\u548c\u884c\u4e3a\u3002\u5c5e\u6027\u662f\u5b58\u50a8\u5728\u5bf9\u8c61\u4e2d\u7684\u6570\u636e\uff0c\u800c\u65b9\u6cd5\u662f\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u64cd\u4f5c\u5bf9\u8c61\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b9a\u4e49\u7c7b\u7684\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u7c7b\u7684\u5c5e\u6027\u53ef\u4ee5\u5728\u6784\u9020\u51fd\u6570\u4e2d\u5b9a\u4e49\uff0c\u4e5f\u53ef\u4ee5\u5728\u7c7b\u5b9a\u4e49\u7684\u5176\u4ed6\u90e8\u5206\u6dfb\u52a0\u3002\u5c5e\u6027\u901a\u5e38\u901a\u8fc7<code>self<\/code>\u5173\u952e\u5b57\u5f15\u7528\uff0c\u8868\u793a\u5b83\u4eec\u5c5e\u4e8e\u7c7b\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.occupation = &quot;Unemployed&quot;  # \u9ed8\u8ba4\u5c5e\u6027<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>occupation<\/code>\u662f\u4e00\u4e2a\u9ed8\u8ba4\u5c5e\u6027\uff0c\u6240\u6709<code>Person<\/code>\u5bf9\u8c61\u90fd\u5c06\u62e5\u6709\u8fd9\u4e2a\u5c5e\u6027\uff0c\u9664\u975e\u5728\u5b9e\u4f8b\u5316\u65f6\u88ab\u91cd\u65b0\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u5b9a\u4e49\u7c7b\u7684\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u7c7b\u7684\u65b9\u6cd5\u662f\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u64cd\u4f5c\u5bf9\u8c61\u7684\u6570\u636e\u3002\u65b9\u6cd5\u901a\u8fc7\u7b2c\u4e00\u4e2a\u53c2\u6570<code>self<\/code>\u8bbf\u95ee\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, my name is {self.name}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>greet<\/code>\u65b9\u6cd5\u4f7f\u7528<code>self.name<\/code>\u8bbf\u95ee\u5bf9\u8c61\u7684<code>name<\/code>\u5c5e\u6027\uff0c\u5e76\u6253\u5370\u4e00\u6761\u95ee\u5019\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ee7\u627f\u4e0e\u591a\u6001<\/h3>\n<\/p>\n<p><p>\u7ee7\u627f\u548c\u591a\u6001\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u7279\u6027\uff0c\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u66f4\u590d\u6742\u548c\u7075\u6d3b\u7684\u7c7b\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b9e\u73b0\u7ee7\u627f<\/h4>\n<\/p>\n<p><p>\u7ee7\u627f\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7c7b\uff0c\u8be5\u7c7b\u4ece\u73b0\u6709\u7c7b\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u7ee7\u627f\u901a\u8fc7\u5728\u7c7b\u5b9a\u4e49\u4e2d\u6307\u5b9a\u7236\u7c7b\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Employee(Person):<\/p>\n<p>    def __init__(self, name, age, employee_id):<\/p>\n<p>        super().__init__(name, age)<\/p>\n<p>        self.employee_id = employee_id<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Employee<\/code>\u7c7b\u7ee7\u627f\u81ea<code>Person<\/code>\u7c7b\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027<code>employee_id<\/code>\u3002<code>super()<\/code>\u51fd\u6570\u7528\u4e8e\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u786e\u4fdd<code>name<\/code>\u548c<code>age<\/code>\u5c5e\u6027\u88ab\u6b63\u786e\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u5b9e\u73b0\u591a\u6001<\/h4>\n<\/p>\n<p><p>\u591a\u6001\u662f\u6307\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u63a5\u53e3\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5728Python\u4e2d\uff0c\u591a\u6001\u901a\u5e38\u901a\u8fc7\u65b9\u6cd5\u91cd\u8f7d\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Employee(Person):<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, my name is {self.name}, and I am an employee.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Employee<\/code>\u7c7b\u91cd\u8f7d\u4e86<code>Person<\/code>\u7c7b\u7684<code>greet<\/code>\u65b9\u6cd5\uff0c\u4f7f\u5176\u6253\u5370\u4e0d\u540c\u7684\u6d88\u606f\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u76f8\u540c\u7684\u63a5\u53e3\u8c03\u7528\u4e0d\u540c\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7279\u6b8a\u65b9\u6cd5\u548c\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>Python\u7c7b\u4e2d\u6709\u8bb8\u591a\u7279\u6b8a\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u5141\u8bb8\u6211\u4eec\u81ea\u5b9a\u4e49\u7c7b\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u7279\u6b8a\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u7279\u6b8a\u65b9\u6cd5\u662f\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u548c\u7ed3\u5c3e\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u5b9e\u73b0\u7c7b\u7684\u7279\u6b8a\u884c\u4e3a\u3002\u4f8b\u5982\uff0c<code>__str__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5b9a\u4e49\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return f&quot;Person(name={self.name}, age={self.age})&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>__str__<\/code>\u65b9\u6cd5\u8fd4\u56de\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u6253\u5370\u5bf9\u8c61\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u7279\u6b8a\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u7279\u6b8a\u5c5e\u6027\u662fPython\u4e3a\u7c7b\u5b9a\u4e49\u7684\u4e00\u4e9b\u5185\u7f6e\u5c5e\u6027\uff0c\u4f8b\u5982<code>__dict__<\/code>\u5c5e\u6027\uff0c\u5b83\u8fd4\u56de\u5bf9\u8c61\u7684\u5c5e\u6027\u5b57\u5178\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = Person(&quot;Alice&quot;, 30)<\/p>\n<p>print(person.__dict__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>__dict__<\/code>\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u53ca\u5176\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7c7b\u7684\u5b9e\u4f8b\u5316\u548c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u7c7b\u7684\u5b9e\u4f8b\u5316\u662f\u521b\u5efa\u7c7b\u7684\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff0c\u800c\u4f7f\u7528\u7c7b\u662f\u901a\u8fc7\u5bf9\u8c61\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5b9e\u4f8b\u5316\u7c7b<\/h4>\n<\/p>\n<p><p>\u5b9e\u4f8b\u5316\u7c7b\u662f\u901a\u8fc7\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u5b9e\u73b0\u7684\u3002\u6784\u9020\u51fd\u6570\u63a5\u53d7\u5fc5\u8981\u7684\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = Person(&quot;Alice&quot;, 30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>Person<\/code>\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u5f15\u7528\u8d4b\u7ed9\u53d8\u91cf<code>person<\/code>\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528\u7c7b\u7684\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u7c7b\u7684\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u70b9\u53f7\uff08<code>.<\/code>\uff09\u64cd\u4f5c\u7b26\u8bbf\u95ee\u5176\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person.greet()<\/p>\n<p>print(f&quot;{person.name} is {person.age} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8c03\u7528<code>greet<\/code>\u65b9\u6cd5\u548c\u8bbf\u95ee<code>name<\/code>\u548c<code>age<\/code>\u5c5e\u6027\uff0c\u64cd\u4f5c\u5bf9\u8c61\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7c7b\u7684\u5c01\u88c5\u548c\u4fe1\u606f\u9690\u85cf<\/h3>\n<\/p>\n<p><p>\u5c01\u88c5\u548c\u4fe1\u606f\u9690\u85cf\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u5141\u8bb8\u6211\u4eec\u5c06\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u9690\u85cf\u5728\u7c7b\u7684\u5185\u90e8\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u5c01\u88c5\u7684\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5c01\u88c5\u662f\u5c06\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u4f7f\u5f97\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9\u5916\u90e8\u4e0d\u53ef\u89c1\u3002Python\u901a\u8fc7\u5728\u5c5e\u6027\u540d\u524d\u52a0\u4e0b\u5212\u7ebf\u5b9e\u73b0\u5c01\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Person:<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self._name = name<\/p>\n<p>        self._age = age<\/p>\n<p>    def get_name(self):<\/p>\n<p>        return self._name<\/p>\n<p>    def set_name(self, name):<\/p>\n<p>        self._name = name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>_name<\/code>\u548c<code>_age<\/code>\u5c5e\u6027\u662f\u53d7\u4fdd\u62a4\u7684\uff0c\u5916\u90e8\u4e0d\u5e94\u8be5\u76f4\u63a5\u8bbf\u95ee\u5b83\u4eec\u3002\u6211\u4eec\u901a\u8fc7<code>get_name<\/code>\u548c<code>set_name<\/code>\u65b9\u6cd5\u8bbf\u95ee\u548c\u4fee\u6539\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u4fe1\u606f\u9690\u85cf<\/h4>\n<\/p>\n<p><p>\u4fe1\u606f\u9690\u85cf\u662f\u6307\u5c06\u5bf9\u8c61\u7684\u5b9e\u73b0\u7ec6\u8282\u9690\u85cf\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u53ea\u66b4\u9732\u5fc5\u8981\u7684\u63a5\u53e3\u3002\u901a\u8fc7\u4fe1\u606f\u9690\u85cf\uff0c\u6211\u4eec\u53ef\u4ee5\u4fdd\u62a4\u5bf9\u8c61\u7684\u5b8c\u6574\u6027\uff0c\u9632\u6b62\u5916\u90e8\u5bf9\u5bf9\u8c61\u7684\u610f\u5916\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5b9e\u4f8b\u65b9\u6cd5\u5916\uff0cPython\u7c7b\u8fd8\u652f\u6301\u9759\u6001\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5\uff0c\u5141\u8bb8\u6211\u4eec\u5728\u7c7b\u7684\u5c42\u6b21\u4e0a\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u9759\u6001\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u9759\u6001\u65b9\u6cd5\u662f\u4e0e\u7c7b\u5173\u8054\u7684\u65b9\u6cd5\uff0c\u4e0d\u64cd\u4f5c\u5b9e\u4f8b\u7684\u5c5e\u6027\u3002\u9759\u6001\u65b9\u6cd5\u901a\u8fc7<code>@staticmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MathUtils:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def add(a, b):<\/p>\n<p>        return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>add<\/code>\u662f\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u8c03\u7528\uff0c\u800c\u4e0d\u9700\u8981\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = MathUtils.add(3, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u7c7b\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u662f\u4e0e\u7c7b\u5173\u8054\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u7c7b\u7684\u5c5e\u6027\u3002\u7c7b\u65b9\u6cd5\u901a\u8fc7<code>@classmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u901a\u5e38\u662f<code>cls<\/code>\uff0c\u8868\u793a\u7c7b\u672c\u8eab\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Counter:<\/p>\n<p>    count = 0<\/p>\n<p>    @classmethod<\/p>\n<p>    def increment(cls):<\/p>\n<p>        cls.count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>increment<\/code>\u662f\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u8c03\u7528\uff0c\u5e76\u4fee\u6539\u7c7b\u5c5e\u6027<code>count<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">Counter.increment()<\/p>\n<p>print(Counter.count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u7c7b\u7684\u7ec4\u5408\u548c\u805a\u5408<\/h3>\n<\/p>\n<p><p>\u7c7b\u7684\u7ec4\u5408\u548c\u805a\u5408\u662f\u521b\u5efa\u590d\u6742\u7c7b\u7ed3\u6784\u7684\u4e24\u79cd\u65b9\u6cd5\uff0c\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u7ec4\u5408\u7b80\u5355\u7c7b\u6784\u5efa\u590d\u6742\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u7c7b\u7684\u7ec4\u5408<\/h4>\n<\/p>\n<p><p>\u7ec4\u5408\u662f\u5c06\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u4f5c\u4e3a\u5c5e\u6027\u5305\u542b\u5728\u53e6\u4e00\u4e2a\u7c7b\u4e2d\u3002\u7ec4\u5408\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u7ec4\u5408\u7b80\u5355\u7c7b\u6784\u5efa\u590d\u6742\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Engine:<\/p>\n<p>    def start(self):<\/p>\n<p>        print(&quot;Engine started.&quot;)<\/p>\n<p>class Car:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.engine = Engine()<\/p>\n<p>    def start(self):<\/p>\n<p>        self.engine.start()<\/p>\n<p>        print(&quot;Car started.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Car<\/code>\u7c7b\u901a\u8fc7\u7ec4\u5408\u5305\u542b\u4e86\u4e00\u4e2a<code>Engine<\/code>\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7<code>start<\/code>\u65b9\u6cd5\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h4>8.2 \u7c7b\u7684\u805a\u5408<\/h4>\n<\/p>\n<p><p>\u805a\u5408\u662f\u5c06\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u7c7b\u3002\u805a\u5408\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u5171\u4eab\u5bf9\u8c61\u6784\u5efa\u590d\u6742\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Wheel:<\/p>\n<p>    def __init__(self, size):<\/p>\n<p>        self.size = size<\/p>\n<p>class Car:<\/p>\n<p>    def __init__(self, wheel):<\/p>\n<p>        self.wheel = wheel<\/p>\n<p>    def get_wheel_size(self):<\/p>\n<p>        return self.wheel.size<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Car<\/code>\u7c7b\u901a\u8fc7\u805a\u5408\u5305\u542b\u4e86\u4e00\u4e2a<code>Wheel<\/code>\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7<code>get_wheel_size<\/code>\u65b9\u6cd5\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u60a8\u5e94\u8be5\u5bf9Python\u4e2d\u5982\u4f55\u8bbe\u8ba1\u7c7b\u6709\u4e86\u5168\u9762\u7684\u4e86\u89e3\u3002\u7c7b\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3\uff0c\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u590d\u6742\u548c\u7075\u6d3b\u7684\u7a0b\u5e8f\u7ed3\u6784\u3002\u8bbe\u8ba1\u826f\u597d\u7684\u7c7b\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u7c7b\u7684\u8bbe\u8ba1\u539f\u5219\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8bbe\u8ba1Python\u7c7b\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u57fa\u672c\u539f\u5219\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u4f8b\u5982\u5355\u4e00\u804c\u8d23\u539f\u5219\uff08SRP\uff09\uff0c\u5373\u4e00\u4e2a\u7c7b\u5e94\u4ec5\u8d1f\u8d23\u4e00\u4e2a\u529f\u80fd\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u540c\u65f6\uff0c\u9075\u5faa\u5f00\u653e-\u5c01\u95ed\u539f\u5219\uff08OCP\uff09\uff0c\u5373\u7c7b\u5e94\u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5c01\u95ed\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u548c\u63a5\u53e3\u5b9e\u73b0\u7075\u6d3b\u7684\u529f\u80fd\u6269\u5c55\u3002\u6b64\u5916\uff0c\u826f\u597d\u7684\u547d\u540d\u548c\u6ce8\u91ca\u4e5f\u80fd\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u7c7b\u7684\u529f\u80fd\u548c\u7528\u9014\u3002<\/p>\n<p><strong>\u5728\u8bbe\u8ba1Python\u7c7b\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u65f6\uff0c\u9996\u5148\u8981\u660e\u786e\u7c7b\u7684\u76ee\u7684\u548c\u529f\u80fd\u3002\u5c5e\u6027\u5e94\u53cd\u6620\u7c7b\u7684\u72b6\u6001\uff0c\u800c\u65b9\u6cd5\u5219\u5e94\u5b9a\u4e49\u7c7b\u7684\u884c\u4e3a\u3002\u5728\u8003\u8651\u5c5e\u6027\u65f6\uff0c\u53ef\u4ee5\u601d\u8003\u54ea\u4e9b\u6570\u636e\u662f\u8be5\u7c7b\u5fc5\u9700\u7684\uff0c\u54ea\u4e9b\u662f\u53ef\u9009\u7684\u3002\u5728\u8bbe\u8ba1\u65b9\u6cd5\u65f6\uff0c\u786e\u4fdd\u5b83\u4eec\u4e0e\u7c7b\u7684\u804c\u8d23\u76f8\u5173\uff0c\u5e76\u80fd\u591f\u6709\u6548\u5730\u64cd\u4f5c\u548c\u7ba1\u7406\u5c5e\u6027\u3002\u4f7f\u7528\u5c01\u88c5\u539f\u5219\uff0c\u786e\u4fdd\u5c5e\u6027\u5728\u5916\u90e8\u53ea\u80fd\u901a\u8fc7\u65b9\u6cd5\u8bbf\u95ee\uff0c\u4ece\u800c\u4fdd\u6301\u7c7b\u7684\u5185\u90e8\u72b6\u6001\u5b89\u5168\u3002<\/p>\n<p><strong>\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528\u7ee7\u627f\u548c\u591a\u6001\u6765\u8bbe\u8ba1Python\u7c7b\uff1f<\/strong><br \/>\u7ee7\u627f\u548c\u591a\u6001\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u7279\u6027\u3002\u5728\u8bbe\u8ba1\u7c7b\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u6765\u5b9e\u73b0\u7ee7\u627f\u3002\u57fa\u7c7b\u5e94\u5305\u542b\u901a\u7528\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u6d3e\u751f\u7c7b\u5219\u6269\u5c55\u6216\u4fee\u6539\u8fd9\u4e9b\u529f\u80fd\u3002\u4f7f\u7528\u591a\u6001\u53ef\u4ee5\u8ba9\u4e0d\u540c\u7684\u7c7b\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u54cd\u5e94\u76f8\u540c\u7684\u6d88\u606f\uff0c\u8fd9\u901a\u5e38\u901a\u8fc7\u65b9\u6cd5\u91cd\u5199\u5b9e\u73b0\u3002\u786e\u4fdd\u5728\u8bbe\u8ba1\u65f6\u8003\u8651\u5230\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5229\u7528\u8fd9\u4e9b\u7279\u6027\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u8ba1\u7c7b\u7684\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u5b9a\u4e49\u7c7b\u7684\u540d\u79f0\u3001\u521b\u5efa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3001\u5b9a\u4e49\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3001\u5b9e\u73b0\u7ee7\u627f\u4e0e\u591a\u6001\u3001\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":938930,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938929"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=938929"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938929\/revisions"}],"predecessor-version":[{"id":938931,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/938929\/revisions\/938931"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/938930"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=938929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=938929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=938929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}