Python 编程 | 连载 16 - 类的特性
Python,编程,连载,16,特性
2025-03-13 21:27:21 时间
一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第11天,点击查看活动详情。
一、类的继承
什么是继承:
- 通过继承来获得所继承的类的功能
- 被继承的类成为父类,继承类成为子类
- 可以提高代码的重用率
父类与子类的关系:
- 子类拥有父类的所有属性和方法
- 父类不具备子类的独有的属性和方法
- 定义子类时,将父类传入子类的参数内
- 子类实例化可以调用自己与父类的函数和属性
class Human():
def __init__(self, name, gender):
self.name = name
self.gender = gender
def breath(self):
print('{}可以呼吸'.format(self.name))
class Student(Human):
def study(self):
print('{}可以学习'.format(self.name))
class Teacher(Human):
def teach(self):
print('{}可以教学'.format(self.name))
human = Human('夸父', '男')
human.breath()
teacher = Teacher('孔子', '男')
teacher.teach()
student = Student('子渊', '男')
student.study()
子类可以调用父类的方法,各个子类之间的方法式独立的,父类不能调用子类的方法
super 关键字
Python中子类继承父类的方法而使用的关键字,当子类继承父类后,就可以通过super调用父类的方法,无须传递self参数
class Human():
def __init__(self):
print('I am Human')
class Student(Human):
def __init__(self):
print('I am Student')
# 调用父类的构造方法
super().__init__()
if __name__ == '__main__':
stu = Student()
super函数传值,直接在调用时传入自定义的参数即可,self无须传递
class Human():
def __init__(self, name):
self.name = name
print('I am Human, My name is {}'.format(name))
class Student(Human):
def __init__(self):
print('I am Student')
super().__init__('女娲')
if __name__ == '__main__':
stu = Student()
类的多重继承
类的多重继承既一个类可以同时继承多个父类,其他语言如Java中只能同时继承一个类。Python中实现多重继承只需要将被继承的类放入子类的参数位中,使用逗号隔开,继承的顺序是从左向右依次继承
class Human():
def __init__(self, name, gender):
self.name = name
self.gender = gender
def breath(self):
print('{}可以呼吸'.format(self.name))
class Teacher():
def __init__(self, name):
self.name = name
def teach(self):
print('{}可以教学'.format(self.name))
class Student(Teacher, Human):
def study(self):
print('{}可以学习'.format(self.name))
# 使用Human父类的构造函数实例化
student = Student('子渊', '男')
student.study()
student.breath()
student.teach()
Student类继承了Human类和Teacher类,Student类的实例化对象可以调用Human和Teacher的类方法,但是当两个父类中存在同名的函数或者构造方法时,优先使用继承的第一个父类的函数
# 上面代码不变
student_01 = Student('孟子')
student_01.teach()
student_01.breath()
这里使用Teacher类的构造函数进行实例化对象
错误提示是缺少gender参数,这是Human类的构造方法,因此Teacher类的构造方法被覆盖了
class Human():
def __init__(self, name, gender):
self.name = name
self.gender = gender
def breath(self):
print('{}可以呼吸'.format(self.name))
class Teacher():
def __init__(self, name):
self.name = name
def breath(self):
print('Teacher也可以呼吸')
def teach(self):
print('{}可以教学'.format(self.name))
class Student(Teacher, Human):
def study(self):
print('{}可以学习'.format(self.name))
student_01 = Student('孟子')
student_01.teach()
student_01.breath()
在Teacher类中也增加一个breath方法,并且将Student继承时将Teacher放在前面
根据打印结果,Human类的构造方法和breath方法都被覆盖了
__mor__将显示类的继承顺序
print(Student.__mro__)
Student类先继承的Teacher类,再继承的Human类,最后继承了基类object
二、类的多态
类的多态既同一个功能或函数多状态化,在子类中重写父类的方法即可实现多态
class Human():
def breath(self):
print('Human can breath')
class Student(Human):
def breath(self):
print('Student can breath')
if __name__ == '__main__':
stu = Student()
stu.breath()
在子类中定义同名函数,即可重写父类中的函数,并实现与父类不同的功能
相关文章
- Python Flask 编程 | 连载 04 - Flask 响应
- Python 编程骚操作连载(二)- 类与对象
- Python 编程 | 连载 20 - 文件 I/O
- Python Flask 编程 | 连载 08 - Jinja2 过滤器
- Python 编程 | 连载 23 - 常用函数与高阶函数
- Python 编程 | 连载 17 - 高阶函数与装饰器
- Python Flask 编程 | 连载 06 - Jinja2 语法
- 常用Python库_编程代码大全
- Python 编程 | 连载 04 - 字典与运算符
- macbookpro安装anaconda_mac下python编程软件
- Python 编程 | 连载 07 - 列表与元组操作
- Python 编程 | 连载 08 - 列表与元组操作
- python滑动验证码_python编程是啥
- Python 编程 | 连载 18 - 异常处理
- PyCharm下使用 ipython 交互式编程「建议收藏」
- Python 编程 | 连载 25 - Python 多进程
- Python Flask 编程 | 连载 03 - Flask 请求
- Python 编程 | 连载 06 - 格式化与转义字符
- Python Flask 编程 | 连载 09 - Jinja2 模板特性
- Python升级之路( Lv14 ) 并发编程初识