{"id":1154242,"date":"2025-01-13T17:48:55","date_gmt":"2025-01-13T09:48:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154242.html"},"modified":"2025-01-13T17:48:57","modified_gmt":"2025-01-13T09:48:57","slug":"python%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e5%a6%82%e4%bd%95%e7%bb%a7%e6%89%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154242.html","title":{"rendered":"python\u9762\u5411\u5bf9\u8c61\u5982\u4f55\u7ee7\u627f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183843\/3d4af50f-7898-4393-b867-ab3bf037ff58.webp\" alt=\"python\u9762\u5411\u5bf9\u8c61\u5982\u4f55\u7ee7\u627f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u7684\u7ee7\u627f\u673a\u5236\u901a\u8fc7\u4f7f\u7528<code>class<\/code>\u5173\u952e\u5b57\u548c\u62ec\u53f7\u5185\u7684\u7236\u7c7b\u540d\u79f0\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u7ee7\u627f\uff0c\u5b50\u7c7b\u53ef\u4ee5\u91cd\u7528\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3001\u589e\u52a0\u65b0\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3001\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u3002\u7ee7\u627f\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\uff0c\u8fd8\u4f7f\u4ee3\u7801\u66f4\u5177\u7ed3\u6784\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\u4e00\u4e2a\u5e38\u89c1\u7684\u573a\u666f\u662f<strong>\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5<\/strong>\u3002\u91cd\u5199\u65b9\u6cd5\u5141\u8bb8\u5b50\u7c7b\u63d0\u4f9b\u7279\u5b9a\u5b9e\u73b0\uff0c\u8986\u76d6\u7236\u7c7b\u4e2d\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u591a\u6001\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u7ee7\u627f\u53ca\u76f8\u5173\u6982\u5ff5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Python\u7ee7\u627f\u57fa\u7840<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u7ee7\u627f\u901a\u8fc7\u7c7b\u5b9a\u4e49\u6765\u5b9e\u73b0\u3002\u5728\u5b9a\u4e49\u5b50\u7c7b\u65f6\uff0c\u7236\u7c7b\u540d\u79f0\u653e\u5728\u5b50\u7c7b\u540d\u79f0\u540e\u7684\u62ec\u53f7\u5185\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def display(self):<\/p>\n<p>        print(f&quot;Parent Name: {self.name}&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        super().__init__(name)<\/p>\n<p>        self.age = age<\/p>\n<p>    def display(self):<\/p>\n<p>        super().display()<\/p>\n<p>        print(f&quot;Child Age: {self.age}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child(&quot;Tom&quot;, 12)<\/p>\n<p>child.display()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Child<\/code>\u7c7b\u7ee7\u627f\u81ea<code>Parent<\/code>\u7c7b\uff0c\u5e76\u91cd\u5199\u4e86<code>display<\/code>\u65b9\u6cd5\uff0c\u540c\u65f6\u4e5f\u8c03\u7528\u4e86\u7236\u7c7b\u7684<code>display<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5355\u7ee7\u627f\u4e0e\u591a\u7ee7\u627f<\/h3>\n<\/p>\n<p><h4>1\u3001\u5355\u7ee7\u627f<\/h4>\n<\/p>\n<p><p>\u5355\u7ee7\u627f\u662f\u6307\u4e00\u4e2a\u5b50\u7c7b\u7ee7\u627f\u4e00\u4e2a\u7236\u7c7b\u3002\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u7ee7\u627f\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def __init__(self, species):<\/p>\n<p>        self.species = species<\/p>\n<p>    def sound(self):<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se NotImplementedError(&quot;This method should be overridden.&quot;)<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    def __init__(self, species, name):<\/p>\n<p>        super().__init__(species)<\/p>\n<p>        self.name = name<\/p>\n<p>    def sound(self):<\/p>\n<p>        return &quot;Bark&quot;<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>dog = Dog(&quot;Canine&quot;, &quot;Buddy&quot;)<\/p>\n<p>print(dog.sound())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u591a\u7ee7\u627f<\/h4>\n<\/p>\n<p><p>\u591a\u7ee7\u627f\u662f\u6307\u4e00\u4e2a\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u591a\u4e2a\u7236\u7c7b\u3002Python\u652f\u6301\u591a\u7ee7\u627f\uff0c\u8fd9\u4f7f\u5f97\u5b50\u7c7b\u80fd\u591f\u4ece\u591a\u4e2a\u7236\u7c7b\u4e2d\u7ee7\u627f\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Flyable:<\/p>\n<p>    def fly(self):<\/p>\n<p>        print(&quot;Flying...&quot;)<\/p>\n<p>class Swimmable:<\/p>\n<p>    def swim(self):<\/p>\n<p>        print(&quot;Swimming...&quot;)<\/p>\n<p>class Duck(Flyable, Swimmable):<\/p>\n<p>    def quack(self):<\/p>\n<p>        print(&quot;Quack...&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>duck = Duck()<\/p>\n<p>duck.fly()<\/p>\n<p>duck.swim()<\/p>\n<p>duck.quack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u91cd\u5199\u4e0e\u6269\u5c55\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1\u3001\u91cd\u5199\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u4ee5\u63d0\u4f9b\u7279\u5b9a\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Parent&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child()<\/p>\n<p>child.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6269\u5c55\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5b50\u7c7b\u53ef\u4ee5\u6269\u5c55\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u5b8c\u5168\u91cd\u5199\u5b83\u4eec\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>super()<\/code>\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Parent&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        super().greet()<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child()<\/p>\n<p>child.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>super()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>super()<\/code>\u51fd\u6570\u7528\u4e8e\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u3002\u5b83\u5728\u591a\u7ee7\u627f\u4e2d\u5c24\u5176\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u6b63\u786e\u7684\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class A:<\/p>\n<p>    def method(self):<\/p>\n<p>        print(&quot;Method from A&quot;)<\/p>\n<p>class B(A):<\/p>\n<p>    def method(self):<\/p>\n<p>        print(&quot;Method from B&quot;)<\/p>\n<p>        super().method()<\/p>\n<p>class C(B):<\/p>\n<p>    def method(self):<\/p>\n<p>        print(&quot;Method from C&quot;)<\/p>\n<p>        super().method()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>c = C()<\/p>\n<p>c.method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7c7b\u7684\u5c5e\u6027\u4e0e\u65b9\u6cd5\u7684\u7ee7\u627f<\/h3>\n<\/p>\n<p><h4>1\u3001\u7ee7\u627f\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        super().__init__(name)<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child(&quot;Alice&quot;, 10)<\/p>\n<p>print(child.name)<\/p>\n<p>print(child.age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ee7\u627f\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u5e76\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u91cd\u5199\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Parent&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child()<\/p>\n<p>child.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u62bd\u8c61\u7c7b\u4e0e\u63a5\u53e3<\/h3>\n<\/p>\n<p><h4>1\u3001\u62bd\u8c61\u7c7b<\/h4>\n<\/p>\n<p><p>\u62bd\u8c61\u7c7b\u662f\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u7684\u7c7b\uff0c\u53ea\u80fd\u88ab\u7ee7\u627f\u3002\u5b83\u901a\u5e38\u5305\u542b\u62bd\u8c61\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5b50\u7c7b\u4e2d\u5fc5\u987b\u88ab\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from abc import ABC, abstractmethod<\/p>\n<p>class AbstractParent(ABC):<\/p>\n<p>    @abstractmethod<\/p>\n<p>    def greet(self):<\/p>\n<p>        pass<\/p>\n<p>class Child(AbstractParent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child()<\/p>\n<p>child.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u63a5\u53e3<\/h4>\n<\/p>\n<p><p>Python\u6ca1\u6709\u6b63\u5f0f\u7684\u63a5\u53e3\u6982\u5ff5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u62bd\u8c61\u7c7b\u6765\u5b9e\u73b0\u63a5\u53e3\u7684\u6548\u679c\u3002\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u7ec4\u5fc5\u987b\u88ab\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Interface(ABC):<\/p>\n<p>    @abstractmethod<\/p>\n<p>    def method1(self):<\/p>\n<p>        pass<\/p>\n<p>    @abstractmethod<\/p>\n<p>    def method2(self):<\/p>\n<p>        pass<\/p>\n<p>class Implementation(Interface):<\/p>\n<p>    def method1(self):<\/p>\n<p>        print(&quot;Method1 Implementation&quot;)<\/p>\n<p>    def method2(self):<\/p>\n<p>        print(&quot;Method2 Implementation&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>impl = Implementation()<\/p>\n<p>impl.method1()<\/p>\n<p>impl.method2()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7ec4\u5408\u4e0e\u7ee7\u627f\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u7ec4\u5408\u6bd4\u7ee7\u627f\u66f4\u5408\u9002\u3002\u7ec4\u5408\u662f\u6307\u4e00\u4e2a\u7c7b\u5305\u542b\u53e6\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u7ee7\u627f\u5b83\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u7c7b\u4e4b\u95f4\u7684\u8026\u5408\u3002<\/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, engine):<\/p>\n<p>        self.engine = engine<\/p>\n<p>    def drive(self):<\/p>\n<p>        self.engine.start()<\/p>\n<p>        print(&quot;Car is driving&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>engine = Engine()<\/p>\n<p>car = Car(engine)<\/p>\n<p>car.drive()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u7ee7\u627f\u4e2d\u7684MRO\uff08\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff09<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ee7\u627f\u4e2d\uff0cPython\u4f7f\u7528C3\u7ebf\u6027\u5316\u7b97\u6cd5\u6765\u786e\u5b9a\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09\u3002\u53ef\u4ee5\u4f7f\u7528<code>__mro__<\/code>\u5c5e\u6027\u6216<code>mro()<\/code>\u65b9\u6cd5\u67e5\u770bMRO\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class A:<\/p>\n<p>    def method(self):<\/p>\n<p>        print(&quot;Method from A&quot;)<\/p>\n<p>class B(A):<\/p>\n<p>    def method(self):<\/p>\n<p>        print(&quot;Method from B&quot;)<\/p>\n<p>class C(A):<\/p>\n<p>    def method(self):<\/p>\n<p>        print(&quot;Method from C&quot;)<\/p>\n<p>class D(B, C):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>d = D()<\/p>\n<p>d.method()<\/p>\n<p>print(D.__mro__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u7ee7\u627f\u4e2d\u7684\u8bbf\u95ee\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u8bbf\u95ee\u63a7\u5236\u901a\u8fc7\u547d\u540d\u7ea6\u5b9a\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u516c\u6709\u6210\u5458<\/h4>\n<\/p>\n<p><p>\u516c\u6709\u6210\u5458\u53ef\u4ee5\u5728\u7c7b\u7684\u5185\u90e8\u548c\u5916\u90e8\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name  # \u516c\u6709\u5c5e\u6027<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>parent = Parent(&quot;John&quot;)<\/p>\n<p>print(parent.name)<\/p>\n<p>parent.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4fdd\u62a4\u6210\u5458<\/h4>\n<\/p>\n<p><p>\u4fdd\u62a4\u6210\u5458\u5728\u540d\u79f0\u524d\u52a0\u4e00\u4e2a\u4e0b\u5212\u7ebf<code>_<\/code>\uff0c\u8868\u793a\u5b83\u4eec\u4e0d\u5e94\u8be5\u5728\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\uff0c\u4f46\u5728\u5b50\u7c7b\u4e2d\u53ef\u4ee5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self._name = name  # \u4fdd\u62a4\u5c5e\u6027<\/p>\n<p>    def _greet(self):  # \u4fdd\u62a4\u65b9\u6cd5<\/p>\n<p>        print(f&quot;Hello, {self._name}&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def display(self):<\/p>\n<p>        print(f&quot;Child Name: {self._name}&quot;)<\/p>\n<p>        self._greet()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child(&quot;Alice&quot;)<\/p>\n<p>child.display()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u79c1\u6709\u6210\u5458<\/h4>\n<\/p>\n<p><p>\u79c1\u6709\u6210\u5458\u5728\u540d\u79f0\u524d\u52a0\u4e24\u4e2a\u4e0b\u5212\u7ebf<code>__<\/code>\uff0c\u8868\u793a\u5b83\u4eec\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u8bbf\u95ee\uff0c\u4e0d\u80fd\u5728\u5b50\u7c7b\u6216\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.__name = name  # \u79c1\u6709\u5c5e\u6027<\/p>\n<p>    def __greet(self):  # \u79c1\u6709\u65b9\u6cd5<\/p>\n<p>        print(f&quot;Hello, {self.__name}&quot;)<\/p>\n<p>    def display(self):<\/p>\n<p>        self.__greet()<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>parent = Parent(&quot;John&quot;)<\/p>\n<p>parent.display()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u7ee7\u627f\u4e2d\u7684\u547d\u540d\u7a7a\u95f4\u4e0e\u4f5c\u7528\u57df<\/h3>\n<\/p>\n<p><p>\u5728\u7ee7\u627f\u4e2d\uff0c\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u547d\u540d\u7a7a\u95f4\u3002\u5b50\u7c7b\u53ef\u4ee5\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        super().__init__(name)<\/p>\n<p>        self.age = age<\/p>\n<p>    def display(self):<\/p>\n<p>        print(f&quot;Name: {self.name}, Age: {self.age}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child(&quot;Alice&quot;, 10)<\/p>\n<p>child.display()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u7ee7\u627f\u4e2d\u7684\u6784\u9020\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u4ee5\u786e\u4fdd\u7236\u7c7b\u7684\u5c5e\u6027\u88ab\u6b63\u786e\u521d\u59cb\u5316\u3002\u53ef\u4ee5\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u6765\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        super().__init__(name)<\/p>\n<p>        self.age = age<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child(&quot;Alice&quot;, 10)<\/p>\n<p>print(child.name)<\/p>\n<p>print(child.age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u7ee7\u627f\u4e2d\u7684\u52a8\u6001\u65b9\u6cd5\u7ed1\u5b9a<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u65b9\u6cd5\u662f\u52a8\u6001\u7ed1\u5b9a\u7684\uff0c\u8fd9\u610f\u5473\u7740\u65b9\u6cd5\u8c03\u7528\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u3002\u8fd9\u4f7f\u5f97\u5b50\u7c7b\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u8c03\u7528\u5b50\u7c7b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Parent&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>parent = Parent()<\/p>\n<p>parent.greet()<\/p>\n<p>child = Child()<\/p>\n<p>child.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u7ee7\u627f\u4e2d\u7684\u53cd\u5c04\u673a\u5236<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u53cd\u5c04\u673a\u5236\uff0c\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u5bf9\u8c61\u7684\u7c7b\u578b\u3001\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528<code>hasattr()<\/code>\u3001<code>getattr()<\/code>\u3001<code>setattr()<\/code>\u548c<code>delattr()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u53cd\u5c04\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>parent = Parent(&quot;John&quot;)<\/p>\n<h2><strong>\u53cd\u5c04\u793a\u4f8b<\/strong><\/h2>\n<p>print(hasattr(parent, &#39;name&#39;))  # True<\/p>\n<p>print(getattr(parent, &#39;name&#39;))  # John<\/p>\n<p>setattr(parent, &#39;name&#39;, &#39;Alice&#39;)<\/p>\n<p>print(getattr(parent, &#39;name&#39;))  # Alice<\/p>\n<p>delattr(parent, &#39;name&#39;)<\/p>\n<p>print(hasattr(parent, &#39;name&#39;))  # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u7ee7\u627f\u4e2d\u7684\u591a\u6001\u6027<\/h3>\n<\/p>\n<p><p>\u591a\u6001\u6027\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u7279\u6027\uff0c\u5141\u8bb8\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\u901a\u8fc7\u76f8\u540c\u7684\u63a5\u53e3\u8c03\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u5b9e\u73b0\u3002\u7ee7\u627f\u548c\u65b9\u6cd5\u91cd\u5199\u662f\u5b9e\u73b0\u591a\u6001\u6027\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def sound(self):<\/p>\n<p>        raise NotImplementedError(&quot;This method should be overridden.&quot;)<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    def sound(self):<\/p>\n<p>        return &quot;Bark&quot;<\/p>\n<p>class Cat(Animal):<\/p>\n<p>    def sound(self):<\/p>\n<p>        return &quot;Meow&quot;<\/p>\n<p>def make_sound(animal):<\/p>\n<p>    print(animal.sound())<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>dog = Dog()<\/p>\n<p>cat = Cat()<\/p>\n<p>make_sound(dog)  # Bark<\/p>\n<p>make_sound(cat)  # Meow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u7ee7\u627f\u4e2d\u7684\u7c7b\u65b9\u6cd5\u4e0e\u9759\u6001\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\u662f\u4e0e\u7c7b\u76f8\u5173\u7684\u65b9\u6cd5\uff0c\u4f46\u5b83\u4eec\u7684\u8c03\u7528\u65b9\u5f0f\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7c7b\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u7c7b\u65b9\u6cd5\u4f7f\u7528<code>@classmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f<code>cls<\/code>\uff0c\u8868\u793a\u7c7b\u672c\u8eab\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    @classmethod<\/p>\n<p>    def greet(cls):<\/p>\n<p>        print(f&quot;Hello from {cls.__name__}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>Parent.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9759\u6001\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u9759\u6001\u65b9\u6cd5\u4f7f\u7528<code>@staticmethod<\/code>\u88c5\u9970\u5668\u5b9a\u4e49\uff0c\u5b83\u4eec\u4e0d\u9700\u8981\u7c7b\u5b9e\u4f8b\u6216\u7c7b\u672c\u8eab\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def greet():<\/p>\n<p>        print(&quot;Hello from Parent&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>Parent.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u7ee7\u627f\u4e2d\u7684\u5143\u7c7b<\/h3>\n<\/p>\n<p><p>\u5143\u7c7b\u662f\u521b\u5efa\u7c7b\u7684\u7c7b\u3002\u901a\u8fc7\u5143\u7c7b\uff0c\u53ef\u4ee5\u63a7\u5236\u7c7b\u7684\u521b\u5efa\u548c\u884c\u4e3a\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u7c7b\u5b9a\u4e49\u4e2d\u4f7f\u7528<code>__metaclass__<\/code>\u5c5e\u6027\u6216\u7ee7\u627f<code>type<\/code>\u7c7b\u6765\u5b9a\u4e49\u5143\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Meta(type):<\/p>\n<p>    def __new__(cls, name, bases, dct):<\/p>\n<p>        print(f&quot;Creating class {name}&quot;)<\/p>\n<p>        return super().__new__(cls, name, bases, dct)<\/p>\n<p>class Parent(metaclass=Meta):<\/p>\n<p>    pass<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>parent = Parent()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u7ee7\u627f\u4e2d\u7684\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u4e2a\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u7528\u6765\u4fee\u6539\u53e6\u4e00\u4e2a\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u884c\u4e3a\u3002\u5728\u7ee7\u627f\u4e2d\uff0c\u88c5\u9970\u5668\u53ef\u4ee5\u7528\u4e8e\u4fee\u6539\u5b50\u7c7b\u65b9\u6cd5\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Before function call&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;After function call&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>class Parent:<\/p>\n<p>    @decorator<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Parent&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>parent = Parent()<\/p>\n<p>parent.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u7ee7\u627f\u4e2d\u7684\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7ee7\u627f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u548c\u5904\u7406\u9519\u8bef\u3002\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u5b9a\u4e49\u7279\u5b9a\u7684\u5f02\u5e38\uff0c\u5e76\u5728\u7236\u7c7b\u6216\u5b50\u7c7b\u4e2d\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ParentException(Exception):<\/p>\n<p>    pass<\/p>\n<p>class ChildException(ParentException):<\/p>\n<p>    pass<\/p>\n<p>class Parent:<\/p>\n<p>    def greet(self):<\/p>\n<p>        raise ParentException(&quot;Error in Parent&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        try:<\/p>\n<p>            super().greet()<\/p>\n<p>        except ParentException as e:<\/p>\n<p>            print(f&quot;Caught exception: {e}&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>child = Child()<\/p>\n<p>child.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u7ee7\u627f\u4e2d\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u6765\u5b9a\u4e49\u3002\u5728\u7ee7\u627f\u4e2d\uff0c\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;Entering context&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&quot;Exiting context&quot;)<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(&quot;Hello from Child&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>with Child() as child:<\/p>\n<p>    child.greet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u7ee7\u627f\u4e2d\u7684\u8bbe\u8ba1\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u8bbe\u8ba1\u6a21\u5f0f\u662f\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u901a\u7528\u89e3\u51b3\u65b9\u6848\u3002\u5728\u7ee7\u627f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5355\u4f8b\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Singleton:<\/p>\n<p>    _instance = None<\/p>\n<p>    def __new__(cls, *args, kwargs):<\/p>\n<p>        if not cls._instance:<\/p>\n<p>            cls._instance = super().__new__(cls, *args, kwargs)<\/p>\n<p>        return cls._instance<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>singleton1 = Singleton()<\/p>\n<p>singleton2 = Singleton()<\/p>\n<p>print(singleton1 is singleton2)  # True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5de5\u5382\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5de5\u5382\u6a21\u5f0f\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\uff0c\u800c\u4e0d\u9700\u8981\u6307\u5b9a\u5177\u4f53\u7684\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def sound(self):<\/p>\n<p>        raise NotImplementedError(&quot;This method should be overridden.&quot;)<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    def sound(self):<\/p>\n<p>        return &quot;Bark&quot;<\/p>\n<p>class Cat(Animal):<\/p>\n<p>    def sound(self):<\/p>\n<p>        return &quot;Meow&quot;<\/p>\n<p>class AnimalFactory:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def create_animal(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>            raise ValueError(&quot;Unknown animal type&quot;)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>animal = AnimalFactory.create_animal(&quot;dog&quot;)<\/p>\n<p>print(animal.sound())  # Bark<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a\u901a\u8fc7\u7ee7\u627f\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u5145\u5206\u7406\u89e3\u548c\u8fd0\u7528\u7ee7\u627f\u673a\u5236\uff0c\u5bf9\u4e8e\u7f16\u5199\u9ad8\u8d28\u91cf\u7684Python\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u4e2d\u7684\u7ee7\u627f\uff1f<\/strong><br \/>\u7ee7\u627f\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\uff0c\u5141\u8bb8\u4e00\u4e2a\u7c7b\uff08\u5b50\u7c7b\uff09\u4ece\u53e6\u4e00\u4e2a\u7c7b\uff08\u7236\u7c7b\uff09\u83b7\u53d6\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97\u4ee3\u7801\u7684\u590d\u7528\u6210\u4e3a\u53ef\u80fd\uff0c\u5b50\u7c7b\u53ef\u4ee5\u6269\u5c55\u7236\u7c7b\u7684\u529f\u80fd\uff0c\u6216\u8005\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5355\u7ee7\u627f\u548c\u591a\u7ee7\u627f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5355\u7ee7\u627f\u6307\u7684\u662f\u4e00\u4e2a\u5b50\u7c7b\u53ea\u7ee7\u627f\u4e00\u4e2a\u7236\u7c7b\uff0c\u800c\u591a\u7ee7\u627f\u5219\u5141\u8bb8\u4e00\u4e2a\u5b50\u7c7b\u7ee7\u627f\u591a\u4e2a\u7236\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u5b9a\u4e49\u5b50\u7c7b\u65f6\uff0c\u5c06\u7236\u7c7b\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b50\u7c7b\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>class \u5b50\u7c7b(\u7236\u7c7b):<\/code> \u8868\u793a\u5355\u7ee7\u627f\uff0c\u800c <code>class \u5b50\u7c7b(\u7236\u7c7b1, \u7236\u7c7b2):<\/code> \u5219\u8868\u793a\u591a\u7ee7\u627f\u3002\u591a\u7ee7\u627f\u9700\u8981\u6ce8\u610f\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09\uff0c\u4ee5\u786e\u4fdd\u65b9\u6cd5\u8c03\u7528\u7684\u6b63\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u5b50\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e0e\u7236\u7c7b\u4e2d\u540c\u540d\u7684\u65b9\u6cd5\u6765\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5b50\u7c7b\u63d0\u4f9b\u4e0d\u540c\u7684\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u7236\u7c7b\u6709\u4e00\u4e2a\u65b9\u6cd5 <code>def show(self):<\/code>\uff0c\u5b50\u7c7b\u53ef\u4ee5\u5b9a\u4e49\u540c\u540d\u7684\u65b9\u6cd5 <code>def show(self):<\/code>\uff0c\u5728\u8be5\u65b9\u6cd5\u4e2d\u5b9e\u73b0\u4e0d\u540c\u7684\u903b\u8f91\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5b50\u7c7b\u53ef\u4ee5\u4f7f\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u9009\u62e9\u8986\u76d6\u5b83\u4ee5\u5b9e\u73b0\u7279\u5b9a\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u7684\u7ee7\u627f\u673a\u5236\u901a\u8fc7\u4f7f\u7528class\u5173\u952e\u5b57\u548c\u62ec\u53f7\u5185\u7684\u7236\u7c7b\u540d\u79f0\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u7ee7\u627f\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1154248,"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\/1154242"}],"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=1154242"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154242\/revisions"}],"predecessor-version":[{"id":1154249,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154242\/revisions\/1154249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154248"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}