继承
- 继承:子类默认继承父类所有的属性和方法
#定义父类
class A(object):
def __init__(self):
self.num=1
def print_info(self):
print(self.num)
# 定义子类
class B(A):
pass
# 创建对象
result=B()
result.print_info() #1
# object类是顶级类或者基类,其他子类是派生类
2.单继承
# 师傅类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def make_cake(self):
print(f'运用{self.cake}制作蛋糕')
# 徒弟类
class Tudi(Shifu):
pass
# 用徒弟类创建对象,调用实例属性和方法
Dizi=Tudi()
print(Dizi.cake) #蛋糕配方
Dizi.make_cake() #运用蛋糕配方制作蛋糕
3.多继承:一个类同时继承了多个父类
注意:如果一个类继承多个父类,优先继承第一个父类的同名属性和方法
# 师傅类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 徒弟类
class Tudi(Shifu,School):
pass
# 用徒弟类创建对象,调用实例属性和方法
Dizi=Tudi()
print(Dizi.cake) #蛋糕配方
Dizi.print_info() #运用蛋糕配方制作蛋糕
# 师傅类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 徒弟类
class Tudi(School,Shifu):
pass
# 用徒弟类创建对象,调用实例属性和方法
Dizi=Tudi()
print(Dizi.cake) #培训蛋糕配方
Dizi.print_info() #运用培训蛋糕配方制作蛋糕
4.子类重写父类同名方法和属性
注意:如果子类和父类拥有同名属性和方法,子类创建对象调用属性和方法时,调用到的是子类里面的同名属性和方法
# 师父类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 独创类
class Owner(Shifu,School):
def __init__(self):
self.cake='独创蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 用徒弟类创建对象,调用实例属性和方法
Dizi=Owner()
print(Dizi.cake) #独创蛋糕配方
Dizi.print_info() #运用独创蛋糕配方制作蛋糕
4.1子类的继承关系
# 师父类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 独创类
class Owner(Shifu,School):
def __init__(self):
self.cake='独创蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 用徒弟类创建对象,调用实例属性和方法
Dizi=Owner()
print(Dizi.cake) #独创蛋糕配方
Dizi.print_info() #运用独创蛋糕配方制作蛋糕
print(Owner.__mro__) #(<class '__main__.Owner'>, <class '__main__.Shifu'>, <class '__main__.School'>, <class 'object'>)
# 使用__mro__体现子类的继承关系
5.子类调用父类的同名方法和属性
# 师父类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 独创类
class Owner(Shifu,School):
def __init__(self):
self.cake='独创蛋糕配方'
def print_info(self):
# 如果是先调用了父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化
self.__init__()
print(f'运用{self.cake}制作蛋糕')
# 调用父类方法,但是为保证调用到的也是父类的属性,必须在调用方法前调用父类的初始化
def make_Shifu_cake(self):
Shifu.__init__(self)
Shifu.print_info(self)
def make_School_cake(self):
School.__init__(self)
School.print_info(self)
# 用徒弟类创建对象,调用实例属性和方法
Dizi=Owner()
print(Dizi.cake) #独创蛋糕配方
Dizi.print_info() #运用独创蛋糕配方制作蛋糕
Dizi.make_Shifu_cake() #运用蛋糕配方制作蛋糕
Dizi.make_School_cake() #运用培训蛋糕配方制作蛋糕
6.多层继承
# 师父类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 独创类
class Owner(Shifu,School):
def __init__(self):
self.cake='独创蛋糕配方'
def print_info(self):
# 如果是先调用了父类的属性和方法,父类属性会覆盖子类属性,故在调用属性前,先调用自己子类的初始化
self.__init__()
print(f'运用{self.cake}制作蛋糕')
# 调用父类方法,但是为保证调用到的也是父类的属性,必须在调用方法前调用父类的初始化
def make_Shifu_cake(self):
Shifu.__init__(self)
Shifu.print_info(self)
def make_School_cake(self):
School.__init__(self)
School.print_info(self)
# 徒孙类
class Tusun(Owner):
pass
# 用徒孙类创建对象,调用实例属性和方法
xiaoming=Tusun()
xiaoming.print_info() #运用独创蛋糕配方制作蛋糕
xiaoming.make_Shifu_cake() #运用蛋糕配方制作蛋糕
xiaoming.make_School_cake() #运用培训蛋糕配方制作蛋糕
7.super()调用父类方法
注意:使用super()可以自动查找父类
# 师傅类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(Shifu):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# # 方法2.1:super(当前类名,self).函数()
# super(School, self).__init__()
# super(School, self).print_info()
#方法2.2:super().函数()
super().__init__()
super().print_info()
# 徒弟类
class Owner(School):
#一次性调用父类School,Shifu的方法
def make_old_cake(self):
#方法1:代码量大,冗余
# School.__init__(self)
# School.print_info(self)
# Shifu.__init__(self)
# Shifu.print_info(self)
#方法2.1:super(当前类名,self).函数()
# super(Owner,self).__init__()
# super(Owner, self).print_info()
#方法2.2:super().函数()
super().__init__()
super().print_info()
Dizi=Owner()
Dizi.make_old_cake()
8.私有权限
8.1定义私有属性和方法
设置私有权限的方法:在属性名和方法名前面加上两个下划线
# 师父类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 独创类
class Owner(Shifu,School):
def __init__(self):
self.cake='独创蛋糕配方'
#定义为私有
self.__money=1000
#定义为私有
def __print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 徒孙类
class Tusun(Owner):
pass
# 用徒孙类创建对象,调用实例属性和方法
xiaoming=Tusun()
# print(xiaoming.money) #报错
# xiaoming.__print_info() #报错
8.2获取和修改私有属性值
用get_XX来获取私有属性
用set_XX来修改私有属性
# 师父类
class Shifu(object):
def __init__(self):
self.cake='蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 培训类
class School(object):
def __init__(self):
self.cake='培训蛋糕配方'
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 独创类
class Owner(Shifu,School):
def __init__(self):
self.cake='独创蛋糕配方'
#定义为私有
self.__money=1000
#获取私有属性
def get_money(self):
return self.__money
#修改私有属性
def set_money(self):
self.__money=2000
def print_info(self):
print(f'运用{self.cake}制作蛋糕')
# 徒孙类
class Tusun(Owner):
pass
# 用徒孙类创建对象,调用实例属性和方法
xiaoming=Tusun()
print(xiaoming.get_money()) #1000
xiaoming.set_money()
print(xiaoming.get_money()) #2000