{"id":1174892,"date":"2025-01-15T17:24:23","date_gmt":"2025-01-15T09:24:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174892.html"},"modified":"2025-01-15T17:24:27","modified_gmt":"2025-01-15T09:24:27","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%88%9b%e5%bb%ba%e5%af%b9%e8%b1%a1%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1174892.html","title":{"rendered":"\u5982\u4f55\u7528python\u521b\u5efa\u5bf9\u8c61\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110509\/a79e045f-1020-4c9a-bfaa-65b6f65b11bd.webp\" alt=\"\u5982\u4f55\u7528python\u521b\u5efa\u5bf9\u8c61\u548c\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u521b\u5efa\u5bf9\u8c61\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u7c7b\u5b9a\u4e49\u3001\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u3002<\/strong> \u5728\u8fd9\u56db\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u7c7b\u5b9a\u4e49\u662f\u6700\u5e38\u89c1\u548c\u57fa\u7840\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u7c7b\u5b9a\u4e49\u6765\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7c7b\u5b9a\u4e49\u521b\u5efa\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u7c7b\u662f\u7528\u6765\u5b9a\u4e49\u5bf9\u8c61\u7684\u84dd\u56fe\u3002\u7c7b\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u5316\u6765\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9a\u4e49\u7c7b<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5173\u952e\u5b57<code>class<\/code>\u6765\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u3002\u7c7b\u7684\u540d\u79f0\u901a\u5e38\u4f7f\u7528\u9996\u5b57\u6bcd\u5927\u5199\u7684\u9a7c\u5cf0\u547d\u540d\u6cd5\u3002\u7c7b\u7684\u4e3b\u4f53\u5305\u542b\u5c5e\u6027\u548c\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} and I am {self.age} years old.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Person<\/code>\u7c7b\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a<code>name<\/code>\u548c<code>age<\/code>\u3002<code>__init__<\/code>\u65b9\u6cd5\u662f\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u88ab\u81ea\u52a8\u8c03\u7528\u3002<code>greet<\/code>\u65b9\u6cd5\u662f\u4e00\u4e2a\u666e\u901a\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u6253\u5370\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efa\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u7528\u7c7b\u540d\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u6765\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person2 = Person(&quot;Bob&quot;, 25)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a<code>Person<\/code>\u5bf9\u8c61\uff1a<code>person1<\/code>\u548c<code>person2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u8bbf\u95ee\u5176\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(person1.name)  # \u8f93\u51fa: Alice<\/p>\n<p>print(person2.age)   # \u8f93\u51fa: 25<\/p>\n<p>person1.greet()      # \u8f93\u51fa: Hello, my name is Alice and I am 30 years old.<\/p>\n<p>person2.greet()      # \u8f93\u51fa: Hello, my name is Bob and I am 25 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u6784\u9020\u51fd\u6570\u521b\u5efa\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u6784\u9020\u51fd\u6570\u662f\u7c7b\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u521d\u59cb\u5316\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u5728Python\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u662f\u901a\u8fc7<code>__init__<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u7684\u3002<\/p>\n<\/p>\n<p><h4>1. \u6784\u9020\u51fd\u6570\u7684\u5b9a\u4e49<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class Car:<\/p>\n<p>    def __init__(self, make, model, year):<\/p>\n<p>        self.make = make<\/p>\n<p>        self.model = model<\/p>\n<p>        self.year = year<\/p>\n<p>    def description(self):<\/p>\n<p>        return f&quot;{self.year} {self.make} {self.model}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Car<\/code>\u7c7b\u6709\u4e09\u4e2a\u5c5e\u6027\uff1a<code>make<\/code>\u3001<code>model<\/code>\u548c<code>year<\/code>\u3002<code>__init__<\/code>\u65b9\u6cd5\u7528\u4e8e\u521d\u59cb\u5316\u8fd9\u4e9b\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efa\u5bf9\u8c61\u5e76\u521d\u59cb\u5316\u5c5e\u6027<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">car1 = Car(&quot;Toyota&quot;, &quot;Camry&quot;, 2020)<\/p>\n<p>car2 = Car(&quot;Honda&quot;, &quot;Civic&quot;, 2019)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a<code>Car<\/code>\u5bf9\u8c61\uff1a<code>car1<\/code>\u548c<code>car2<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">print(car1.description())  # \u8f93\u51fa: 2020 Toyota Camry<\/p>\n<p>print(car2.description())  # \u8f93\u51fa: 2019 Honda Civic<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u521b\u5efa\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c<code>dict()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u5bf9\u8c61\uff0c<code>list()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u5217\u8868\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5b57\u5178\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = dict(name=&quot;Alice&quot;, age=30)<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u5217\u8868\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = list([1, 2, 3, 4])<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u521b\u5efa\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5de5\u5382\u51fd\u6570\u662f\u4e00\u79cd\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u6839\u636e\u9700\u8981\u521b\u5efa\u548c\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9a\u4e49\u5de5\u5382\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def animal_factory(animal_type):<\/p>\n<p>    if animal_type == &quot;dog&quot;:<\/p>\n<p>        return Dog()<\/p>\n<p>    elif animal_type == &quot;cat&quot;:<\/p>\n<p>        return Cat()<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>class Dog:<\/p>\n<p>    def speak(self):<\/p>\n<p>        return &quot;Woof!&quot;<\/p>\n<p>class Cat:<\/p>\n<p>    def speak(self):<\/p>\n<p>        return &quot;Meow!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>animal_factory<\/code>\u51fd\u6570\u6839\u636e\u4f20\u9012\u7684<code>animal_type<\/code>\u53c2\u6570\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5de5\u5382\u51fd\u6570\u521b\u5efa\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">dog = animal_factory(&quot;dog&quot;)<\/p>\n<p>cat = animal_factory(&quot;cat&quot;)<\/p>\n<p>print(dog.speak())  # \u8f93\u51fa: Woof!<\/p>\n<p>print(cat.speak())  # \u8f93\u51fa: Meow!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u521b\u5efa\u5404\u79cd\u5bf9\u8c61\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u5bf9\u8c61\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u7c7b\u5b9a\u4e49\u3001\u6784\u9020\u51fd\u6570\u3001\u5185\u7f6e\u51fd\u6570\u8fd8\u662f\u5de5\u5382\u51fd\u6570\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5bf9\u8c61\u7684\u7ee7\u627f\u548c\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\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u91cd\u7528\u4ee3\u7801\u548c\u5b9e\u73b0\u7075\u6d3b\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>1. \u7ee7\u627f<\/h4>\n<\/p>\n<p><p>\u7ee7\u627f\u662f\u4e00\u79cd\u521b\u5efa\u65b0\u7c7b\u7684\u65b9\u5f0f\uff0c\u65b0\u7c7b\u7ee7\u627f\u4e86\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u65b0\u7c7b\u79f0\u4e3a\u5b50\u7c7b\uff0c\u7236\u7c7b\u79f0\u4e3a\u57fa\u7c7b\u6216\u8d85\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def speak(self):<\/p>\n<p>        pass<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    def speak(self):<\/p>\n<p>        return &quot;Woof!&quot;<\/p>\n<p>class Cat(Animal):<\/p>\n<p>    def speak(self):<\/p>\n<p>        return &quot;Meow!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Dog<\/code>\u548c<code>Cat<\/code>\u7c7b\u7ee7\u627f\u4e86<code>Animal<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u4e86<code>Animal<\/code>\u7c7b\u7684<code>speak<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>2. \u591a\u6001<\/h4>\n<\/p>\n<p><p>\u591a\u6001\u662f\u6307\u76f8\u540c\u7684\u64cd\u4f5c\u53ef\u4ee5\u4f5c\u7528\u4e8e\u4e0d\u540c\u7684\u5bf9\u8c61\u4e0a\uff0c\u5e76\u4e14\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u884c\u4e3a\u3002\u901a\u8fc7\u7ee7\u627f\u548c\u65b9\u6cd5\u91cd\u5199\uff0c\u53ef\u4ee5\u5b9e\u73b0\u591a\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def animal_sound(animal):<\/p>\n<p>    print(animal.speak())<\/p>\n<p>dog = Dog(&quot;Buddy&quot;)<\/p>\n<p>cat = Cat(&quot;Whiskers&quot;)<\/p>\n<p>animal_sound(dog)  # \u8f93\u51fa: Woof!<\/p>\n<p>animal_sound(cat)  # \u8f93\u51fa: Meow!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>animal_sound<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55<code>Animal<\/code>\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528\u5176<code>speak<\/code>\u65b9\u6cd5\u3002\u8fd9\u5c31\u662f\u591a\u6001\u7684\u4f53\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5bf9\u8c61\u7684\u5c01\u88c5<\/h3>\n<\/p>\n<p><p>\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u6982\u5ff5\uff0c\u901a\u8fc7\u5c01\u88c5\u53ef\u4ee5\u9690\u85cf\u5bf9\u8c61\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u53ea\u66b4\u9732\u5fc5\u8981\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h4>1. \u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u5728\u5c5e\u6027\u548c\u65b9\u6cd5\u540d\u524d\u52a0\u53cc\u4e0b\u5212\u7ebf<code>__<\/code>\uff0c\u53ef\u4ee5\u5c06\u5176\u58f0\u660e\u4e3a\u79c1\u6709\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class BankAccount:<\/p>\n<p>    def __init__(self, balance):<\/p>\n<p>        self.__balance = balance<\/p>\n<p>    def deposit(self, amount):<\/p>\n<p>        self.__balance += amount<\/p>\n<p>    def withdraw(self, amount):<\/p>\n<p>        if amount &lt;= self.__balance:<\/p>\n<p>            self.__balance -= amount<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;Insufficient funds&quot;)<\/p>\n<p>    def get_balance(self):<\/p>\n<p>        return self.__balance<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>BankAccount<\/code>\u7c7b\u7684<code>__balance<\/code>\u5c5e\u6027\u662f\u79c1\u6709\u7684\uff0c\u53ea\u80fd\u901a\u8fc7\u7c7b\u7684\u65b9\u6cd5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h4>2. \u8bbf\u95ee\u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1\u79c1\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u662f\u5185\u90e8\u7684\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u6539\u5199\uff08name mangling\uff09\u8bbf\u95ee\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">account = BankAccount(1000)<\/p>\n<p>account.deposit(500)<\/p>\n<p>account.withdraw(200)<\/p>\n<p>print(account.get_balance())  # \u8f93\u51fa: 1300<\/p>\n<h2><strong>\u8bbf\u95ee\u79c1\u6709\u5c5e\u6027\uff08\u4e0d\u63a8\u8350\uff09<\/strong><\/h2>\n<p>print(account._BankAccount__balance)  # \u8f93\u51fa: 1300<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u53ef\u4ee5\u901a\u8fc7\u540d\u79f0\u6539\u5199\u8bbf\u95ee\u79c1\u6709\u5c5e\u6027\uff0c\u4f46\u8fd9\u8fdd\u53cd\u4e86\u5c01\u88c5\u539f\u5219\uff0c\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5bf9\u8c61\u7684\u7ec4\u5408<\/h3>\n<\/p>\n<p><p>\u7ec4\u5408\u662f\u53e6\u4e00\u79cd\u521b\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5c06\u5bf9\u8c61\u4f5c\u4e3a\u5c5e\u6027\u6765\u7ec4\u5408\u4e0d\u540c\u7684\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9a\u4e49\u7ec4\u5408\u7c7b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class Engine:<\/p>\n<p>    def __init__(self, horsepower):<\/p>\n<p>        self.horsepower = horsepower<\/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, make, model, engine):<\/p>\n<p>        self.make = make<\/p>\n<p>        self.model = model<\/p>\n<p>        self.engine = engine<\/p>\n<p>    def start(self):<\/p>\n<p>        print(f&quot;{self.make} {self.model} is starting&quot;)<\/p>\n<p>        self.engine.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Car<\/code>\u7c7b\u5305\u542b\u4e00\u4e2a<code>Engine<\/code>\u5bf9\u8c61\u4f5c\u4e3a\u5176\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efa\u7ec4\u5408\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">engine = Engine(150)<\/p>\n<p>car = Car(&quot;Toyota&quot;, &quot;Camry&quot;, engine)<\/p>\n<p>car.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Car<\/code>\u5bf9\u8c61\u5305\u542b\u4e00\u4e2a<code>Engine<\/code>\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u7ec4\u5408\u6765\u5b9e\u73b0\u590d\u6742\u7684\u5bf9\u8c61\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5bf9\u8c61\u7684\u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>Python\u4f7f\u7528\u81ea\u52a8\u5185\u5b58\u7ba1\u7406\u673a\u5236\uff0c\u901a\u8fc7\u5f15\u7528\u8ba1\u6570\u548c\u5783\u573e\u56de\u6536\u6765\u7ba1\u7406\u5bf9\u8c61\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h4>1. \u5f15\u7528\u8ba1\u6570<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5f15\u7528\u8ba1\u6570\uff0c\u5f53\u5bf9\u8c61\u88ab\u5f15\u7528\u65f6\uff0c\u8ba1\u6570\u589e\u52a0\uff1b\u5f53\u5f15\u7528\u88ab\u5220\u9664\u65f6\uff0c\u8ba1\u6570\u51cf\u5c11\u3002\u5f53\u5f15\u7528\u8ba1\u6570\u4e3a\u96f6\u65f6\uff0c\u5bf9\u8c61\u88ab\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>class MyClass:<\/p>\n<p>    pass<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(sys.getrefcount(obj))  # \u8f93\u51fa: 2\uff08\u4e00\u4e2a\u662fobj\uff0c\u53e6\u4e00\u4e2a\u662fgetrefcount\u7684\u53c2\u6570\uff09<\/p>\n<p>del obj<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5783\u573e\u56de\u6536<\/h4>\n<\/p>\n<p><p>Python\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u81ea\u52a8\u56de\u6536\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u5360\u7528\u7684\u5185\u5b58\u3002\u5783\u573e\u56de\u6536\u5668\u4f1a\u5b9a\u671f\u68c0\u67e5\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u5e76\u56de\u6536\u5f15\u7528\u8ba1\u6570\u4e3a\u96f6\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gc<\/p>\n<p>gc.collect()  # \u624b\u52a8\u89e6\u53d1\u5783\u573e\u56de\u6536<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316<\/h3>\n<\/p>\n<p><p>\u5e8f\u5217\u5316\u662f\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u8fc7\u7a0b\uff0c\u53cd\u5e8f\u5217\u5316\u662f\u5c06\u5b57\u8282\u6d41\u8f6c\u6362\u56de\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002Python\u63d0\u4f9b\u4e86<code>pickle<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><h4>1. \u5e8f\u5217\u5316\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>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>person = Person(&quot;Alice&quot;, 30)<\/p>\n<p>with open(&quot;person.pkl&quot;, &quot;wb&quot;) as file:<\/p>\n<p>    pickle.dump(person, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e00\u4e2a<code>Person<\/code>\u5bf9\u8c61\u5e8f\u5217\u5316\u5e76\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u53cd\u5e8f\u5217\u5316\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;person.pkl&quot;, &quot;rb&quot;) as file:<\/p>\n<p>    loaded_person = pickle.load(file)<\/p>\n<p>print(loaded_person.name)  # \u8f93\u51fa: Alice<\/p>\n<p>print(loaded_person.age)   # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ece\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316\u4e00\u4e2a<code>Person<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5bf9\u8c61\u7684\u6bd4\u8f83\u548c\u6392\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u8f7d\u7279\u6b8a\u65b9\u6cd5\u6765\u5b9e\u73b0\u5bf9\u8c61\u7684\u6bd4\u8f83\u548c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b9e\u73b0\u5bf9\u8c61\u7684\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u91cd\u8f7d<code>__eq__<\/code>\u3001<code>__ne__<\/code>\u3001<code>__lt__<\/code>\u3001<code>__le__<\/code>\u3001<code>__gt__<\/code>\u548c<code>__ge__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u8c61\u7684\u6bd4\u8f83\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 __eq__(self, other):<\/p>\n<p>        return self.age == other.age<\/p>\n<p>    def __lt__(self, other):<\/p>\n<p>        return self.age &lt; other.age<\/p>\n<p>person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person2 = Person(&quot;Bob&quot;, 25)<\/p>\n<p>person3 = Person(&quot;Charlie&quot;, 30)<\/p>\n<p>print(person1 == person2)  # \u8f93\u51fa: False<\/p>\n<p>print(person1 == person3)  # \u8f93\u51fa: True<\/p>\n<p>print(person1 &lt; person2)   # \u8f93\u51fa: False<\/p>\n<p>print(person2 &lt; person1)   # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b9e\u73b0\u5bf9\u8c61\u7684\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u91cd\u8f7d<code>__lt__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6392\u5e8f\u51fd\u6570\u5bf9\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">persons = [person1, person2, person3]<\/p>\n<p>sorted_persons = sorted(persons)<\/p>\n<p>for person in sorted_persons:<\/p>\n<p>    print(person.name, person.age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9<code>Person<\/code>\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u6309\u5e74\u9f84\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5bf9\u8c61\u7684\u62f7\u8d1d<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>copy<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u5bf9\u8c61\u7684\u6d45\u62f7\u8d1d\u548c\u6df1\u62f7\u8d1d\u3002<\/p>\n<\/p>\n<p><h4>1. \u6d45\u62f7\u8d1d<\/h4>\n<\/p>\n<p><p>\u6d45\u62f7\u8d1d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u4f46\u4e0d\u590d\u5236\u5d4c\u5957\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>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>person1 = Person(&quot;Alice&quot;, 30)<\/p>\n<p>person2 = copy.copy(person1)<\/p>\n<p>print(person1 == person2)  # \u8f93\u51fa: False<\/p>\n<p>print(person1.name == person2.name)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6df1\u62f7\u8d1d<\/h4>\n<\/p>\n<p><p>\u6df1\u62f7\u8d1d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\uff0c\u5e76\u9012\u5f52\u590d\u5236\u6240\u6709\u5d4c\u5957\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Address:<\/p>\n<p>    def __init__(self, city, street):<\/p>\n<p>        self.city = city<\/p>\n<p>        self.street = street<\/p>\n<p>class Person:<\/p>\n<p>    def __init__(self, name, age, address):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>        self.address = address<\/p>\n<p>address = Address(&quot;New York&quot;, &quot;5th Avenue&quot;)<\/p>\n<p>person1 = Person(&quot;Alice&quot;, 30, address)<\/p>\n<p>person2 = copy.deepcopy(person1)<\/p>\n<p>print(person1 == person2)  # \u8f93\u51fa: False<\/p>\n<p>print(person1.address == person2.address)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528Python\u521b\u5efa\u5bf9\u8c61\uff0c\u5305\u62ec\u4f7f\u7528\u7c7b\u5b9a\u4e49\u3001\u6784\u9020\u51fd\u6570\u3001\u5185\u7f6e\u51fd\u6570\u548c\u5de5\u5382\u51fd\u6570\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u5bf9\u8c61\u7684\u7ee7\u627f\u3001\u591a\u6001\u3001\u5c01\u88c5\u3001\u7ec4\u5408\u3001\u5185\u5b58\u7ba1\u7406\u3001\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3001\u6bd4\u8f83\u548c\u6392\u5e8f\u4ee5\u53ca\u62f7\u8d1d\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u77e5\u8bc6\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u521b\u5efa\u548c\u7ba1\u7406\u5bf9\u8c61\uff0c\u5b9e\u73b0\u9ad8\u6548\u548c\u4f18\u96c5\u7684\u4ee3\u7801\u7f16\u5199\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u4ee5\u521b\u5efa\u5bf9\u8c61\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u5bf9\u8c61\u7684\u7b2c\u4e00\u6b65\u662f\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u3002\u7c7b\u662f\u5bf9\u8c61\u7684\u84dd\u56fe\uff0c\u53ef\u4ee5\u5305\u542b\u5c5e\u6027\uff08\u53d8\u91cf\uff09\u548c\u65b9\u6cd5\uff08\u51fd\u6570\uff09\u3002\u4f7f\u7528<code>class<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">class Dog:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n        \n    def bark(self):\n        return f&quot;{self.name} says Woof!&quot;\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5b9a\u4e49\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7c7b\u540d\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u5982<code>my_dog = Dog(&quot;Buddy&quot;, 3)<\/code>\u3002<\/p>\n<p><strong>\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539\uff1f<\/strong><br \/>\u4e00\u65e6\u5bf9\u8c61\u88ab\u521b\u5efa\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\uff08.\uff09\u64cd\u4f5c\u7b26\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u5b83\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a<code>Dog<\/code>\u5bf9\u8c61<code>my_dog<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>my_dog.name<\/code>\u6765\u83b7\u53d6\u540d\u5b57\uff0c\u901a\u8fc7<code>my_dog.age = 4<\/code>\u6765\u4fee\u6539\u5e74\u9f84\u3002\u800c\u8c03\u7528\u65b9\u6cd5\u5219\u662f\u901a\u8fc7<code>my_dog.bark()<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7ee7\u627f\u4ee5\u6269\u5c55\u5bf9\u8c61\u529f\u80fd\uff1f<\/strong><br \/>\u7ee7\u627f\u5141\u8bb8\u4f60\u521b\u5efa\u4e00\u4e2a\u65b0\u7c7b\uff0c\u4ece\u73b0\u6709\u7c7b\u4e2d\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u901a\u8fc7\u5c06\u7236\u7c7b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b50\u7c7b\uff0c\u53ef\u4ee5\u6269\u5c55\u6216\u91cd\u5199\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">class Puppy(Dog):\n    def play(self):\n        return f&quot;{self.name} is playing!&quot;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Puppy<\/code>\u7c7b\u7ee7\u627f\u4e86<code>Dog<\/code>\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8fd8\u65b0\u589e\u4e86\u4e00\u4e2a<code>play<\/code>\u65b9\u6cd5\uff0c\u5141\u8bb8\u521b\u5efa\u66f4\u7279\u5b9a\u7684\u5bf9\u8c61\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u521b\u5efa\u5bf9\u8c61\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u7c7b\u5b9a\u4e49\u3001\u4f7f\u7528\u6784\u9020\u51fd\u6570\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528\u5de5\u5382\u51fd\u6570\u3002 \u5728\u8fd9\u56db\u79cd\u65b9\u6cd5\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1174897,"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\/1174892"}],"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=1174892"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174892\/revisions"}],"predecessor-version":[{"id":1174898,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174892\/revisions\/1174898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174897"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}