python 钻石继承及参数传递

 

python 钻石继承无参数传递

#  __init__() 无参数钻石继承
class Base(object):
    def __init__(self):
        print ("Base init")
class Medium1(Base):
    def __init__(self):
        super(Medium1, self).__init__()
        print("Medium1 init")
class Medium2(Base):
    def __init__(self):
        super(Medium2, self).__init__()
        print("Medium2 init")
class Leaf(Medium1, Medium2):
    def __init__(self):
        print(Leaf.mro())
        super(Leaf, self).__init__()
        print("Leaf init")

leaf = Leaf()

python 钻石继承参数传递

# __init__() 传递参数钻石继承

class A(object):
    def __init__(self, name, age):
        print('A Begin')
        self.name = name
        self.age = age
        self.sex = "male"
        print('A End')


class B(A):
    def __init__(self, name, age, height, color):
        print('B Begin')
        self.color = color
        super(B, self).__init__(name, age, height)
        print('B End')


class C(A):
    def __init__(self, name, age, height):
        print('C Begin')
        self.height = height
        super(C, self).__init__(name, age)
        print('C End')


class D(B, C):
    def __init__(self, name, age, height, color, weight):
        print('D Begin')
        self.weight = weight
        super(D, self).__init__(name, age, height, color)
        print('D End')
    def show(self):
        print("name",self.name)
        print("age",self.age)
        print("height",self.height)
        print("colro",self.color)
        print("weight",self.weight)
        print("sex",self.sex)
if __name__ == "__main__":
    xm = D(name='xiaoming', age=18, height='170cm',color= 'White',weight= '70kg')
    xm.show()

 继承关系

 

 __init__ 

注意继承关系是钻石形状

但是init关系是 “顺序关系”

python 钻石继承参数传递 

class A():
    def __init__(self, age, **kwargs):
        print("Eneter A Begin")
        self.age = age
        super(A, self).__init__(**kwargs)
        self.sex = 'male'
        print("Eneter A End")

class B(A):
    def __init__(self, name, **kwargs):
        print("Enter B Begin")
        self.name = name
        super(B, self).__init__(**kwargs)
        print("Enter B End")

class C(A):
    def __init__(self,height, **kwargs):
        print("Enter C Begin")
        self.height = height
        super(C, self).__init__(**kwargs)
        print("Enter C End")

class D(C, B):
    def __init__(self, color, weight, **kwargs):
        print("D info", self)
        print(D.mro())
        print("Enter D Begin")
        self.color = color
        self.weight = weight
        super(D, self).__init__(**kwargs)
        print("Enter D End")
    def print_show(self):
        print("name ",self.name)
        print("age ",self.age)
        print("height ",self.height)
        print("color ",self.color)
        print("weight ",self.weight)
        print("sex", self.sex)
'''
需要确定D及D的父类参数
'''
xm = D(name='xiaoming', age=18, height='170cm',color= 'White',weight= '70kg')
xm.print_show()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值