Python 第二十三章 单继承+多继承

# Python 三大特性之一
# 继承 封装 多态
# 继承分为:单继承 多继承
# 普通的对象
class Human:
    def __init__(self,name,sex,age,):
        self.name = name
        self.age = age
        self.sex = sex

class Dog:
    def __init__(self,name,sex,age,):
        self.name = name
        self.age = age
        self.sex = sex

class Cat:
    def __init__(self,name,sex,age,):
        self.name = name
        self.age = age
        self.sex = sex

# 单继承标准格式
class Animal:
    def __init__(self,name,sex,age,):
        self.name = name
        self.age = age
        self.sex = sex

class Human(Animal):
    pass
class Dog(Animal):
    pass
class Cat(Animal):
    pass

# B继承A,B叫做A的子类,派生类
# A叫做B的父类,超类,基类
# B类以及B类的对象使用A类的所有的属性以及方法

person = Human('zs','女',18)
print(person.name)

# py3默认有object方法,默认执行__init__方法

"""
继承的优点:
1、减少重复代码
2、增加类之间的耦合性
3、代码更加清晰流畅
"""



# 1、类名执行父类属性的方法
class Animal:
    live = '生命' #1类名执行父类属性的方法
    def __init__(self,name,sex,age,):
        self.name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('吃饭')

class Human(Animal):
    live = '人类有生命' #1类名执行本类属性的方法
    body = '有脑子'
class Dog(Animal):

    pass
class Cat(Animal):
    pass

print(Human.live)

# 2、派生类 执行父类的属性方法
obj = Human('zs','女',18) # 实例化给Human传参
print(obj.live)
obj.eat()
print(obj)
# A1 = Animal() # 父类实例化
#print(A1.body)# 父类不能找子类

# 查询顺序:单向不可逆
# 子类使用父类的属性方法,父类不能使用子类的属性方法


# 3、既要执行子类的方法,又要执行父类的方法
# 方法一 不依赖继承
class Animal:

    def __init__(self,name,sex,age,): # 函数定义 self形参
        self.name = name
        self.age = age
        self.sex = sex


class Human(Animal):
    def __init__(self,name,sex,age,hobby,):
        # self = obj
        # Animal.__init__(人类对象,姓名,性别,年龄)
        # Animal.__init__(self,'zs','1','2')# 对象封装的属性写死了
        Animal.__init__(self,name,sex,age,)
        # Animal.__init__(self)# 函数() 调用函数 self 实参
        self.hobby = hobby

class Dog(Animal):

    pass
class Cat(Animal):
    pass

obj = Human('zs','1',12,'33')

# 原理
def func(self):
    print(self)
self = 3
func(self)

# 方法二 依赖于继承
# 即执行子类又执行父类
class Animal:

    def __init__(self,name,sex,age):
        self.name = name
        self.age = age
        self.sex = sex
    def eat(self):
        print('吃饭')

class Human(Animal):
    def __init__(self,name,sex,age,hobby):
        # super(Human,self).__init__(name,sex,age)
        super().__init__(name,sex,age)# 执行父类的__init__方法,重构父类方法
        self.hobby = hobby

    def eat(self):

        super().eat()
        print(f'{self.name}需要吃饭')

# obj1 = Human('zs','1',12,'33')
# obj1.eat('dong')

p1 = Human('春哥','laddboy',18,'有思想')

print(p1.__dict__)
p1.eat()



# 多继承
# Python类分为两种:
# Python2:2.2之前都是经典类
# 2.2之后都是新式类
# 1、经典类:不继承object类
# 查询顺序: 深度优先原则
# 2、新式类:继承object类
# 查询顺序:mro(C3)算法

# 多继承基本结构
class Shenxian:
    def fei(self):
        print('飞')

class Monkey:
    def chitao(self):
        print('吃桃')

class SunWuKong(Shenxian,Monkey): # 同时继承多个类
    pass

sxz = SunWuKong()
sxz.chitao()
sxz.fei()


class Shenxian:
    def fei(self):
        print('飞')
    def walk(self):
        print('走路1')

class Monkey:
    def chitao(self):
        print('吃桃')
    def walk(self):
        print('走路2')

class SunWuKong(Monkey,Shenxian):
    pass

sxz = SunWuKong()
sxz.chitao()
sxz.fei()
sxz.walk() # 走路2先调用左边的
# 经典类:深度优先 从左至右
# c3方法

猜你喜欢

转载自www.cnblogs.com/zhangshan33/p/11309456.html