• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

python面向对象-2 继承

武飞扬头像
Ding Jiaxiong
帮助1

面向对象-继承

  1.  

经典类:不由任意内置类型派生出的类,称之为经典类
学新通
新式类:
学新通
python面向对象的继承指的是多个类之间的所属关系,即子类默认继承父类的所有属性和方法。

class A(object):
    def __init__(self):
        self.num = 1

    def info_print(self):
        print(self.num)

class B(A):
    pass

res = B()
res.info_print()

学新通

在python中,所有类都默认继承object类,object类是顶级类或基类,其他子类称之为派生类。

  1.  

① 单继承

class Master(object):
    def __init__(self):
        self.kongfu = "[古法煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}制作煎饼果子")

class Tudi(Master):
    pass

xiaoding = Tudi()
print(xiaoding.kongfu)
xiaoding.make_cake()

学新通
② 多继承

class Master(object):
    def __init__(self):
        self.kongfu = "[古法煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = "[现代煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}制作煎饼果子")

class Tudi(School,Master):
    pass

xiaoding = Tudi()
print(xiaoding.kongfu)
xiaoding.make_cake()
学新通

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

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

class Master(object):
    def __init__(self):
        self.kongfu = "[古法煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = "[现代煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}制作煎饼果子")

class Tudi(School,Master):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

xiaoding = Tudi()
print(xiaoding.kongfu)
xiaoding.make_cake()
学新通

学新通

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

class Master(object):
    def __init__(self):
        self.kongfu = "[古法煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = "[现代煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}制作煎饼果子")

class Tudi(School,Master):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
    def make_cake(self):
        self.__init__() #调用属性前,先调用自己子类的初始化
        print(f'运用{self.kongfu}制作煎饼果子')

    #调用父类方法,为保证调用到的是父类属性,先初始化
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)

    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

xiaoding = Tudi()
xiaoding.make_cake()
xiaoding.make_master_cake()
xiaoding.make_school_cake()
学新通

学新通

5.多层继承

class Master(object):
    def __init__(self):
        self.kongfu = "[古法煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = "[现代煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}制作煎饼果子")

class Tudi(School,Master):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')

    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

class Tusun(Tudi):
    pass

xiaowang = Tusun()
print(xiaowang.kongfu)
xiaowang.make_cake()
学新通

学新通
6.super()调用父类方法

class Master(object):
    def __init__(self):
        self.kongfu = "[古法煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(Master):
    def __init__(self):
        self.kongfu = "[现代煎饼果子配方]"
    def make_cake(self):
        print(f"运用{self.kongfu}制作煎饼果子")

        super().__init__()
        super(School, self).make_cake()

class Tudi(School):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')
    
    #子类调用父类的同名方法和属性,把父类的同名属性和方法封装
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)
    #一次性调用父类的同名属性和方法
    def make_old_cake(self):
        super().__init__()
        super(Tudi, self).make_cake()

xiaoding = Tudi()
xiaoding.make_old_cake()
学新通

学新通
使用super()可以自动查找父类,调用顺序遵循__mro__类属性的属性。

(什么是mro类属性)

学新通
【适合单继承情况使用】
在多继承的情况下:
每个类开始调用根据mro顺序逐个开始,然后逐个进行结束

7.私有权限

① 定义私有属性和方法(设置某个实例属性或实例方法不继承给子类)

设置私有权限的方法,在属性名和方法名前面加上两个下划线

class Master(object):
    def __init__(self):
        self.kongfu = "[传统煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = "[现代煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class Tudi(School,Master):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
        #定义私有属性
        self.__money = 200
    def __info_print(self):
        print(self.kongfu)
        print(self.__money)
    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

class Tusun(Tudi):
    pass

xiaoding = Tudi()
#实例对象不能访问私有属性和私有方法
print(xiaoding.__money)
xiaoding.__info_print()
#子类同样不能访问
xiaowang = Tusun()
print(xiaowang.__money)
xiaowang.__info_print()
学新通

学新通

私有属性和私有方法只能在类中访问和修改

② 获取和修改私有属性值

在python中,一般定义函数名get_xx用来获取私有属性值,定义set_xx用来修改私有属性值

class Master(object):
    def __init__(self):
        self.kongfu = "[传统煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class School(object):
    def __init__(self):
        self.kongfu = "[现代煎饼果子配方]"
    def make_cake(self):
        print(f'运用{self.kongfu}制作煎饼果子')

class Tudi(School,Master):
    def __init__(self):
        self.kongfu = "[独创煎饼果子配方]"
        #定义私有属性
        self.__money = 200
    def get_money(self):
        return self.__money
    def set_money(self):
        self.__money = 50
    def __info_print(self):
        print(self.kongfu)
        print(self.__money)
    def make_cake(self):
        self.__init__()
        print(f'运用{self.kongfu}制作煎饼果子')
    def make_master_cake(self):
        Master.__init__(self)
        Master.make_cake(self)
    def make_school_cake(self):
        School.__init__(self)
        School.make_cake(self)

class Tusun(Tudi):
    pass

xiaoding = Tudi()
print(xiaoding.get_money())
xiaoding.set_money()
print(xiaoding.get_money())
学新通

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgceaib
系列文章
更多 icon
同类精品
更多 icon
继续加载