zoukankan      html  css  js  c++  java
  • python super()处理钻石继承

     1 class room:
     2     def __init__(self,area=120,usedfor='sleep'):
     3         self.area = area
     4         self.usedfor = usedfor
     5 
     6     def display(self):
     7         print("this is my house")
     8 
     9 class babyroom(room):
    10     def __init__(self,area=40,usedfor="son",wallcolor='green'):
    11         super().__init__(area,usedfor)
    12         self.wallcolr = wallcolor
    13 
    14     def display(self):
    15         super().display()
    16         print("babyroom area:%s wallcollor:%s"%(self.area,self.wallcolr))
    17 
    18 class rent:
    19     def __init__(self,money=1000):
    20         self.rentmoney = money
    21 
    22     def display(self):
    23         print("for rent at the price of %s"%self.rentmoney)
    24 
    25 class agent(babyroom,rent):
    26 # class agent(rent,babyroom):
    27     def display(self):
    28         super().display()
    29         print("rent house agent")
    30 
    31 agent().display()
    32 
    33 在理想中我们希望所有类的都能够输出,也就是:
    34 this is my house
    35 babyroom area:40 wallcollor:green
    36 for rent at the price of 1000
    37 rent house agent
    38 
    39 但是实际输出并不是这样的,看到上面两种写法:
    40 写法1的输出:
    41 this is my house
    42 babyroom area:40 wallcollor:green
    43 rent house agent
    44 也就是说并没有调用rent类的display方法
    45 
    46 写法二的输出:
    47 for rent at the price of 1000
    48 rent house agent
    49 也就是说babyroom以及room类的display方法都没有调用
  • 相关阅读:
    机器学习中常见的优化算法
    linux端安装Anaconda,方便远端访问jupyter
    核心③自动分号插入
    setTimeout 和 setInterval
    核心②undefined 和 null
    类型④类型转换
    核心①为什么不要使用 eval
    类型③instanceof 操作符
    类型①相等与比较
    类型②typeof 操作符
  • 原文地址:https://www.cnblogs.com/ceshixuexi/p/8870406.html
Copyright © 2011-2022 走看看