1.普通嵌套
class province(object): # 省份 def __init__(self,address): self.address = address class park(object): # 公园 def __init__(self,name,start,end,address=None): self.name=name self.start=start self.end=end self.address = address class people(object): # 人数 def __init__(self,number,didian=None): self.number = number self.didian = didian p1 = province("香港") p2 = province("北京") p3 = province("深圳") pname = park("迪士尼","7:00","19:00",p1) pname2 = park("天安门","5:00","19:00",p2) pname3 = park("世界之窗","9:00","18:00",p3) num1 = people(1200,pname) num2 = people(1500,pname2) num3 = people(800,pname3) print(num1.didian.address.address) # 可查看num1的所在省份
2.主动调用其他类的成员
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class foo: def per(self): print("5个功能") class foo1(foo): def per(self): print("3个功能") foo.per(self) # 自己传入self obj = foo1() obj.per() <<< 3个功能 5个功能
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# super().实例方法 直接调用,按照继承顺序找下一个 class foo(object): def per(self): print("5个功能") class foo1(foo): def per(self): print("3个功能") super().per() obj = foo1() obj.per() <<< 3个功能 5个功能