Python之继承

内容

  • 继承的概念
  • 单继承
  • 多继承
  • 子类重写父类的同名属性和方法
  • 子类调用父类同名属性和方法
  • 多继承
  • super()
  • 私有属性和私有方法

继承的概念

  • 经典类或旧式类
class 类名:
	代码
	......
  • 新式类
class 类名(object):  # 默认继承基类object,可填充我们想要继承的类名
	代码

单继承

class Master(object):
    def __init__(self):
        self.kongfo = ['古法煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')


class Student(Master):
    pass


student1 = Student()
student1.makeCake()

多继承

当一个类有多个父类的时候,默认使用第一个父类的同名属性和方法。

class Master(object):
    def __init__(self):
        self.kongfo = ['古法煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')


class School(object):
    def __init__(self):
        self.kongfo = ['搞笑煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')


class Student(School, Master):
    pass


student1 = Student()
student1.makeCake()

子类重写父类同名方法和属性

class Master(object):
    def __init__(self):
        self.kongfo = ['古法煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')


class School(object):
    def __init__(self):
        print('创建父类')
        self.kongfo = ['搞笑煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')

    def __del__(self):
        print('删除Master类')


class Student(Master, School):  # 重写父类
    def __init__(self):
        self.kongfo = ['独创秘籍']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')

    def __del__(self):
        print('删除Student')


student1 = Student()
student1.makeCake()
print(Student.__mro__)  # 快速查看继承父类

子类调用父类的同名方法和属性

class Master(object):
    def __init__(self):
        self.kongfo = ['古法煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')


class School(object):
    def __init__(self):
        print('创建父类')
        self.kongfo = ['搞笑煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')


class Student(Master, School):  # 重写父类
    def __init__(self):
        self.kongfo = ['独创秘籍']

    def makeCake(self):
        self.__init__()
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')

    def makeMasterCake(self):
        Master.__init__(self)
        Master.makeCake(self)


student1 = Student()
student1.makeCake()
student1.makeMasterCake()
student1.makeCake()


多层继承

super()调用父类方法

class Master(object):
    def __init__(self):
        self.kongfo = ['古法煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')


class School(Master):
    def __init__(self):
        self.kongfo = ['搞笑煎饼果子配方']

    def makeCake(self):
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')
            # super(School, self).__init__()
            # super(School, self).makeCake()
            super().__init__()
            super().makeCake()


class Student(School):  # 重写父类
    def __init__(self):
        self.kongfo = ['独创秘籍']

    def makeCake(self):
        self.__init__()
        for i in self.kongfo:
            print(f'运用{i}方法做煎饼')

    def makeMasterCake(self):
        Master.__init__(self)
        Master.makeCake(self)

    def makeAllCake(self):
        # Master.__init__(self)
        # Master.makeCake(self)
        # School.__init__(self)
        # School.makeCake(self)
        # 带参 super
        # super(Student, self).__init__()
        # super(Student, self).makeCake()
        # 无参数 super
        super().__init__()
        super().makeCake()


student1 = Student()
# student1.makeCake()
# student1.makeMasterCake()
# student1.makeCake()
student1.makeAllCake()
student2 = School()
student2.makeCake()

私有权限

定义私有属性和方法

设置私有权限的方法:在属性名和方法名前面加上两个下划线__.
私有属性和私有方法只能在类内访问和修改

获取和修改私有属性值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值