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方法都没有调用
  • 相关阅读:
    Android px,dp,pt,sp的差别
    C 八皇后
    线性表
    android 推断手机是否支持前置摄像头
    C/C++易错难点笔记01
    Java日志记录的5条规则
    hdoj-1312-Red and Black
    oracle 11g sql developer安装后无法使用
    显示指定时间的秒数
    云端自动化测试方案
  • 原文地址:https://www.cnblogs.com/ceshixuexi/p/8870406.html
Copyright © 2011-2022 走看看