内容
- 继承的概念
- 单继承
- 多继承
- 子类重写父类的同名属性和方法
- 子类调用父类同名属性和方法
- 多继承
- 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()
私有权限
定义私有属性和方法
设置私有权限的方法:在属性名和方法名前面加上两个下划线__.
私有属性和私有方法只能在类内访问和修改