Python类的继承问题

话不多说,直接上代码:

class Parent(object):
    x = 1


class Child1(Parent):
    pass


class Child2(Parent):
    pass


print(Parent.x, Child1.x, Child2.x)
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)

"""
运行结果:
1 1 1
1 2 1
3 2 3
"""

我对这段代码最大的疑问是为什么最后一行的输出是 3 2 3 却不是 3 2 1 ?
原来,在Python 中,类变量在内部是作为字典处理的。如果一个变量名没有在当前类的字典中发现,将会向上搜索祖先类直到被引用的变量名被找到为止。(如果这个变量名既没有在自己所在的类又没有在祖先类中找到,会引发一个AttributeError异常 )

下面我们逐行分析一下:
  • 第一行(1 1 1):父类Parent中设置了x = 1,当访问子类Child1和Child2的变量 x 时,因为子类Child1和Child2的变量 x 没有在当前类中找到,所以会向上引用父类Parent中的变量 x 。显示id代码如下:
class Parent(object):
    x = 1


class Child1(Parent):
    pass


class Child2(Parent):
    pass


print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))

"""
运行结果:
1 1 1
140709706773328 140709706773328 140709706773328
"""
  • 第二行(1 2 1):在Child1.x = 2中重写了子类Child1的变量 x ,该值仅仅在Child1类中被改变。显示id代码如下:
class Parent(object):
    x = 1


class Child1(Parent):
    pass


class Child2(Parent):
    pass


print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))

"""
运行结果:
1 1 1
140709706773328 140709706773328 140709706773328
1 2 1
140709706773328 140709706773360 140709706773328
"""
  • 第三行(3 2 3):在Parent.x = 3中更改了父类Parent中变量 x 的值,这个改变只会影响所有未重写该变量的子类(例子中的Child2)。显示id代码如下:
class Parent(object):
    x = 1


class Child1(Parent):
    pass


class Child2(Parent):
    pass


print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)
print(id(Parent.x), id(Child1.x), id(Child2.x))

"""
运行结果:
1 1 1
140709706773328 140709706773328 140709706773328
1 2 1
140709706773328 140709706773360 140709706773328
3 2 3
140709706773392 140709706773360 140709706773392
"""

总结:在继承中,所有子类中的变量 x 原本都是引用父类中变量 x 的存储空间,但是Child1中的变量 x 单独指向了另一块存储空间

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python继承是指一个子类可以继承一个或多个父类的属性和方法。这个过程可以实现代码的重用。在Python中,多继承的基本语法很简单。一个简单的例子是: ```python class A(): def test(self): print("test") class B(): def demo(self): print("demo") class C(A, B): pass c = C() c.test() c.demo() ``` 在这个例子中,类C继承了类A和类B。因此,实例化的对象c可以调用test()方法和demo()方法。 另一个例子是: ```python class Animal: def eat(self): print("吃") def drink(self): print("喝") class Dog(Animal): def bark(self): print("汪汪叫") goudan = Dog() goudan.eat() goudan.drink() ``` 在这个例子中,子类Dog继承了父类Animal。因此,实例化的对象goudan可以调用eat()方法和drink()方法。 继承还具有传递性,意味着子类不仅可以继承父类的方法和属性,还可以继承父类的父类的方法和属性。这意味着子类可以间接访问父类的父类的方法和属性。 另外,在继承中,子类也可以重写父类的方法。当子类重写了一个方法时,子类将使用自己的实现而不是从父类中继承的实现。这允许子类根据自己的需求来定制方法的行为。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python中类的继承](https://blog.csdn.net/hello_JeremyWang/article/details/122380842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值