{"id":1064743,"date":"2024-12-31T16:11:44","date_gmt":"2024-12-31T08:11:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064743.html"},"modified":"2024-12-31T16:11:46","modified_gmt":"2024-12-31T08:11:46","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%bf%e7%88%b6%e7%b1%bb%e7%9a%84%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064743.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8bbf\u7236\u7c7b\u7684\u5c5e\u6027"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1c2cc51c-ec22-4e1a-bd1b-964185dd7789.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u8bbf\u7236\u7c7b\u7684\u5c5e\u6027\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u3001\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\u3001\u5728\u5b50\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u907f\u514d\u591a\u91cd\u7ee7\u627f\u5e26\u6765\u7684\u6f5c\u5728\u95ee\u9898\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5b83\u4eec\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>super()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u662f\u8bbf\u95ee\u7236\u7c7b\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u63a8\u8350\u65b9\u6cd5\u3002<code>super()<\/code>\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u7236\u7c7b\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.parent_attribute = &quot;I am a parent attribute&quot;<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()  # \u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/p>\n<p>        self.child_attribute = &quot;I am a child attribute&quot;<\/p>\n<p>    def get_parent_attribute(self):<\/p>\n<p>        return super().parent_attribute  # \u901a\u8fc7super()\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027<\/p>\n<p>child = Child()<\/p>\n<p>print(child.get_parent_attribute())  # \u8f93\u51fa: I am a parent attribute<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b50\u7c7b<code>Child<\/code>\u901a\u8fc7<code>super().__init__()<\/code>\u8c03\u7528\u4e86\u7236\u7c7b<code>Parent<\/code>\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u4ece\u800c\u7ee7\u627f\u4e86\u7236\u7c7b\u7684\u5c5e\u6027<code>parent_attribute<\/code>\u3002\u968f\u540e\uff0c\u901a\u8fc7<code>super().parent_attribute<\/code>\u8bbf\u95ee\u4e86\u7236\u7c7b\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u4e5f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.parent_attribute = &quot;I am a parent attribute&quot;<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        Parent.__init__(self)  # \u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/p>\n<p>        self.child_attribute = &quot;I am a child attribute&quot;<\/p>\n<p>    def get_parent_attribute(self):<\/p>\n<p>        return Parent.parent_attribute<\/p>\n<p>child = Child()<\/p>\n<p>print(child.get_parent_attribute())  # \u8f93\u51fa: I am a parent attribute<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7c7b<code>Child<\/code>\u901a\u8fc7<code>Parent.__init__(self)<\/code>\u8c03\u7528\u4e86\u7236\u7c7b<code>Parent<\/code>\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7<code>Parent.parent_attribute<\/code>\u8bbf\u95ee\u4e86\u7236\u7c7b\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5728\u5b50\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5b50\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\uff0c\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u5b50\u7c7b\u5728\u521d\u59cb\u5316\u65f6\u6b63\u786e\u5730\u7ee7\u627f\u7236\u7c7b\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):<\/p>\n<p>        self.parent_attribute = &quot;I am a parent attribute&quot;<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        Parent.__init__(self)  # \u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/p>\n<p>        self.child_attribute = &quot;I am a child attribute&quot;<\/p>\n<p>    def get_parent_attribute(self):<\/p>\n<p>        return self.parent_attribute  # \u76f4\u63a5\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027<\/p>\n<p>child = Child()<\/p>\n<p>print(child.get_parent_attribute())  # \u8f93\u51fa: I am a parent attribute<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7c7b<code>Child<\/code>\u901a\u8fc7<code>Parent.__init__(self)<\/code>\u8c03\u7528\u4e86\u7236\u7c7b<code>Parent<\/code>\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u4ece\u800c\u7ee7\u627f\u4e86\u7236\u7c7b\u7684\u5c5e\u6027<code>parent_attribute<\/code>\u3002\u968f\u540e\uff0c\u901a\u8fc7<code>self.parent_attribute<\/code>\u76f4\u63a5\u8bbf\u95ee\u4e86\u7236\u7c7b\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u6790<\/h3>\n<\/p>\n<p><h4>\u4e00\u3001\u4f7f\u7528<code>super()<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><strong>1. \u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b50\u7c7b\u4e2d\u4f7f\u7528<code>super().__init__()<\/code>\uff0c\u53ef\u4ee5\u786e\u4fdd\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u88ab\u6b63\u786e\u8c03\u7528\uff0c\u4ece\u800c\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u5728\u591a\u91cd\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\u5c24\u5176\u91cd\u8981\uff0c\u56e0\u4e3a<code>super()<\/code>\u51fd\u6570\u53ef\u4ee5\u6309\u7167\u7ee7\u627f\u94fe\u987a\u5e8f\u8c03\u7528\u6240\u6709\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent1:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.parent1_attribute = &quot;I am Parent1 attribute&quot;<\/p>\n<p>class Parent2:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.parent2_attribute = &quot;I am Parent2 attribute&quot;<\/p>\n<p>class Child(Parent1, Parent2):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.child_attribute = &quot;I am a child attribute&quot;<\/p>\n<p>child = Child()<\/p>\n<p>print(child.parent1_attribute)  # \u8f93\u51fa: I am Parent1 attribute<\/p>\n<p>print(child.parent2_attribute)  # \u8f93\u51fa: I am Parent2 attribute<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7c7b<code>Child<\/code>\u901a\u8fc7<code>super().__init__()<\/code>\u8c03\u7528\u4e86\u7236\u7c7b<code>Parent1<\/code>\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u4ece\u800c\u7ee7\u627f\u4e86<code>Parent1<\/code>\u7684\u5c5e\u6027<code>parent1_attribute<\/code>\u3002\u7531\u4e8e<code>super()<\/code>\u51fd\u6570\u6309\u7167\u7ee7\u627f\u94fe\u987a\u5e8f\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u56e0\u6b64<code>Parent2<\/code>\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e5f\u88ab\u8c03\u7528\u4e86\uff0c\u7ee7\u627f\u4e86<code>Parent2<\/code>\u7684\u5c5e\u6027<code>parent2_attribute<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u521d\u59cb\u5316\u65b9\u6cd5\u5916\uff0c<code>super()<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u8c03\u7528\u7236\u7c7b\u7684\u5176\u4ed6\u65b9\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u5e76\u907f\u514d\u591a\u91cd\u7ee7\u627f\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.parent_attribute = &quot;I am a parent attribute&quot;<\/p>\n<p>    def parent_method(self):<\/p>\n<p>        return &quot;This is a method from parent&quot;<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super().__init__()<\/p>\n<p>        self.child_attribute = &quot;I am a child attribute&quot;<\/p>\n<p>    def child_method(self):<\/p>\n<p>        parent_method_result = super().parent_method()<\/p>\n<p>        return f&quot;Child method calls: {parent_method_result}&quot;<\/p>\n<p>child = Child()<\/p>\n<p>print(child.child_method())  # \u8f93\u51fa: Child method calls: This is a method from parent<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7c7b<code>Child<\/code>\u901a\u8fc7<code>super().parent_method()<\/code>\u8c03\u7528\u4e86\u7236\u7c7b<code>Parent<\/code>\u7684\u65b9\u6cd5<code>parent_method<\/code>\uff0c\u5e76\u5728<code>child_method<\/code>\u4e2d\u8fd4\u56de\u4e86\u7236\u7c7b\u65b9\u6cd5\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u3001\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u591a\u91cd\u7ee7\u627f\u7684\u60c5\u51b5\u4e0b\uff0c\u660e\u786e\u6307\u5b9a\u8981\u8c03\u7528\u7684\u7236\u7c7b\u65b9\u6cd5\u6216\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u8868\u8fbe\u610f\u56fe\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u8fd9\u79cd\u65b9\u5f0f\u53ef\u80fd\u5728\u591a\u91cd\u7ee7\u627f\u4e2d\u5e26\u6765\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.parent_attribute = &quot;I am a parent attribute&quot;<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        Parent.__init__(self)<\/p>\n<p>        self.child_attribute = &quot;I am a child attribute&quot;<\/p>\n<p>    def get_parent_attribute(self):<\/p>\n<p>        return Parent.parent_attribute<\/p>\n<p>child = Child()<\/p>\n<p>print(child.get_parent_attribute())  # \u8f93\u51fa: I am a parent attribute<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u591a\u91cd\u7ee7\u627f\u4e2d\u907f\u514d\u6f5c\u5728\u7684\u51b2\u7a81\u548c\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent1:<\/p>\n<p>    def parent1_method(self):<\/p>\n<p>        return &quot;This is a method from Parent1&quot;<\/p>\n<p>class Parent2:<\/p>\n<p>    def parent2_method(self):<\/p>\n<p>        return &quot;This is a method from Parent2&quot;<\/p>\n<p>class Child(Parent1, Parent2):<\/p>\n<p>    def child_method(self):<\/p>\n<p>        return Parent1.parent1_method(self)<\/p>\n<p>child = Child()<\/p>\n<p>print(child.child_method())  # \u8f93\u51fa: This is a method from Parent1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7c7b<code>Child<\/code>\u901a\u8fc7<code>Parent1.parent1_method(self)<\/code>\u660e\u786e\u6307\u5b9a\u8c03\u7528<code>Parent1<\/code>\u7684\u65b9\u6cd5\uff0c\u907f\u514d\u4e86\u591a\u91cd\u7ee7\u627f\u4e2d\u53ef\u80fd\u7684\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<p><h4>\u4e09\u3001\u5728\u5b50\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728\u5b50\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b50\u7c7b\u5728\u521d\u59cb\u5316\u65f6\u6b63\u786e\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u4e5f\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\uff0c\u5e76\u5728\u5b50\u7c7b\u4e2d\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.parent_attribute = &quot;I am a parent attribute&quot;<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        Parent.__init__(self)<\/p>\n<p>        self.child_attribute = &quot;I am a child attribute&quot;<\/p>\n<p>    def get_parent_attribute(self):<\/p>\n<p>        return self.parent_attribute<\/p>\n<p>child = Child()<\/p>\n<p>print(child.get_parent_attribute())  # \u8f93\u51fa: I am a parent attribute<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u91cd\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u903b\u8f91\uff0c\u5e76\u5728\u5b50\u7c7b\u4e2d\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Parent:<\/p>\n<p>    def parent_method(self):<\/p>\n<p>        return &quot;This is a method from parent&quot;<\/p>\n<p>class Child(Parent):<\/p>\n<p>    def child_method(self):<\/p>\n<p>        return self.parent_method()<\/p>\n<p>child = Child()<\/p>\n<p>print(child.child_method())  # \u8f93\u51fa: This is a method from parent<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5b50\u7c7b<code>Child<\/code>\u901a\u8fc7<code>self.parent_method()<\/code>\u8c03\u7528\u4e86\u7236\u7c7b<code>Parent<\/code>\u7684\u65b9\u6cd5<code>parent_method<\/code>\uff0c\u5e76\u5728\u5b50\u7c7b\u65b9\u6cd5\u4e2d\u8fd4\u56de\u4e86\u7236\u7c7b\u65b9\u6cd5\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u3002<code>super()<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u907f\u514d\u591a\u91cd\u7ee7\u627f\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\u3002\u6b64\u5916\uff0c\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\u548c\u663e\u5f0f\u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e5f\u662f\u5e38\u89c1\u7684\u505a\u6cd5\uff0c\u4f46\u9700\u8981\u6839\u636e\u5177\u4f53\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u65b9\u4fbf\u5730\u7ee7\u627f\u548c\u6269\u5c55\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8bbf\u95ee\u7236\u7c7b\u7684\u79c1\u6709\u5c5e\u6027\uff1f<\/strong><br \/>\u867d\u7136\u79c1\u6709\u5c5e\u6027\u4e0d\u80fd\u76f4\u63a5\u901a\u8fc7\u5b50\u7c7b\u8bbf\u95ee\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u7236\u7c7b\u7684\u65b9\u6cd5\u6765\u95f4\u63a5\u83b7\u53d6\u3002\u53ef\u4ee5\u5728\u7236\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u516c\u5171\u6216\u53d7\u4fdd\u62a4\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u8fd4\u56de\u79c1\u6709\u5c5e\u6027\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">class Parent:\n    def __init__(self):\n        self.__private_attr = &quot;I&#39;m private&quot;\n\n    def get_private_attr(self):\n        return self.__private_attr\n\nclass Child(Parent):\n    def show_private_attr(self):\n        return self.get_private_attr()\n\nchild = Child()\nprint(child.show_private_attr())  # \u8f93\u51fa: I&#39;m private\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5b50\u7c7b\u53ef\u4ee5\u5b89\u5168\u5730\u8bbf\u95ee\u7236\u7c7b\u7684\u79c1\u6709\u5c5e\u6027\u3002<\/p>\n<p><strong>\u5b50\u7c7b\u5982\u4f55\u91cd\u5199\u7236\u7c7b\u7684\u5c5e\u6027\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b50\u7c7b\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u91cd\u5199\u7236\u7c7b\u7684\u5c5e\u6027\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u5b50\u7c7b\u548c\u7236\u7c7b\u6709\u540c\u540d\u5c5e\u6027\uff0c\u5b50\u7c7b\u7684\u5c5e\u6027\u5c06\u4f1a\u8986\u76d6\u7236\u7c7b\u7684\u5c5e\u6027\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">class Parent:\n    attr = &quot;I am from Parent&quot;\n\nclass Child(Parent):\n    attr = &quot;I am from Child&quot;\n\nprint(Child.attr)  # \u8f93\u51fa: I am from Child\nprint(Parent.attr)  # \u8f93\u51fa: I am from Parent\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97\u5b50\u7c7b\u80fd\u591f\u6839\u636e\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u5c5e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u5c5e\u6027\uff1f<\/strong><br \/>\u5728\u5b50\u7c7b\u4e2d\u8c03\u7528\u7236\u7c7b\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>super()<\/code>\u51fd\u6570\u6216\u76f4\u63a5\u901a\u8fc7\u7236\u7c7b\u540d\u6765\u8bbf\u95ee\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">class Parent:\n    attr = &quot;I am from Parent&quot;\n\nclass Child(Parent):\n    def show_attr(self):\n        return super().attr  # \u4f7f\u7528super()\n\n# \u6216\u8005\u76f4\u63a5\u901a\u8fc7\u7236\u7c7b\u540d\u8bbf\u95ee\nclass Child2(Parent):\n    def show_attr(self):\n        return Parent.attr  # \u4f7f\u7528\u7236\u7c7b\u540d\n\nchild = Child()\nprint(child.show_attr())  # \u8f93\u51fa: I am from Parent\n\nchild2 = Child2()\nprint(child2.show_attr())  # \u8f93\u51fa: I am from Parent\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e24\u79cd\u65b9\u5f0f\uff0c\u5b50\u7c7b\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u548c\u4f7f\u7528\u7236\u7c7b\u7684\u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8bbf\u95ee\u7236\u7c7b\u7684\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528super()\u51fd\u6570\u3001\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8bbf\u95ee\u3001\u5728\u5b50\u7c7b\u7684\u521d\u59cb\u5316 [&hellip;]","protected":false},"author":3,"featured_media":1064755,"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\/1064743"}],"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=1064743"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064743\/revisions"}],"predecessor-version":[{"id":1064758,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064743\/revisions\/1064758"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064755"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}