继承

# 单继承
class GrandP():
    print('GrandP')
class Father(GrandP):
    print('Father')
class Son(Father):
    print('Son')
Son()
#结果
GrandP
Father
Son

# 多继承
class Father1():
    print('Father1')
class Father2():
    print('Father2')
class Son(Father1,Father2):
    print('Son')
Son()
#结果
Father1
Father2
Son

# 派生类
class Animal():
    def __init__(self,data):
        self.name = data['name']
        self.age = data['age']
        self.sex = data['sex']
    def walk(self):
        print(self.name)
class dog(Animal):
    def bite(self):
        print('')
data={
    'name': '小明',
    'age': 21,
    'sex': 'male'
}
dog(data).walk()
#结果
小明

#组合
class Mobile():
    def __init__(self, color):
        self.color = color
        
    def call(self):
        print(self.color+'手机打电话')
        
class People():
    def __init__(self, name, mobile):
        self.name = name
        self.mobile = mobile
        
mobile = Mobile('红色')
people = People('小明', mobile)
print(people.name)
people.mobile.call()
#结果
小明
红色手机打电话
posted @ 2019-12-25 09:58  略略略额  阅读(114)  评论(0编辑  收藏  举报