python面向对象-2 继承

面向对象-继承

经典类:不由任意内置类型派生出的类,称之为经典类

python面向对象-2 继承
新式类:
python面向对象-2 继承
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面向对象-2 继承

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

① 单继承

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()

python面向对象-2 继承
② 多继承
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()

python面向对象-2 继承
当一个类有多个父类时,默认使用第一个父类的同名属性和方法。

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()

python面向对象-2 继承

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()

python面向对象-2 继承

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()

python面向对象-2 继承
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()

python面向对象-2 继承
使用super()可以自动查找父类,调用顺序遵循__mro__类属性的属性。

(什么是mro类属性)

python面向对象-2 继承
【适合单继承情况使用】
在多继承的情况下:
每个类开始调用根据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面向对象-2 继承

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

② 获取和修改私有属性值

在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())

python面向对象-2 继承

Original: https://blog.csdn.net/weixin_44226181/article/details/121266367
Author: Ding Jiaxiong
Title: python面向对象-2 继承

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783058/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球