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

原因:对于父类中的静态变量,子类对象只能查看,不能修改,而child1.x = 2是在外部对child1类添加了一个属性

打印一下:print(Child1.__dict__)  print(Parent.__dict__)

 

相关文章:

  • 2022-12-23
  • 2021-12-13
  • 2022-02-25
  • 2022-12-23
  • 2022-03-01
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案