Python 编程 | 连载 16 - 类的特性

简介: Python 编程 | 连载 16 - 类的特性

一、类的继承性

什么是继承:

  • 通过继承来获得所继承的类的功能
  • 被继承的类成为父类,继承类成为子类
  • 可以提高代码的重用率

父类与子类的关系:

  • 子类拥有父类的所有属性和方法
  • 父类不具备子类的独有的属性和方法
  • 定义子类时,将父类传入子类的参数内
  • 子类实例化可以调用自己与父类的函数和属性
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()
复制代码

image.png

子类可以调用父类的方法,各个子类之间的方法式独立的,父类不能调用子类的方法

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()
复制代码

image.png

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()
复制代码

image.png

类的多重继承

类的多重继承既一个类可以同时继承多个父类,其他语言如 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()
复制代码

image.png

Student 类继承了 Human 类和 Teacher 类,Student 类的实例化对象可以调用 Human 和 Teacher 的类方法,但是当两个父类中存在同名的函数或者构造方法时,优先使用继承的第一个父类的函数

# 上面代码不变
student_01 = Student('孟子')
student_01.teach()
student_01.breath()
复制代码

这里使用 Teacher 类的构造函数进行实例化对象

image.png

错误提示是缺少 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放在前面、

image.png

根据打印结果,Human类的构造方法和breath方法都被覆盖了。

__mor__ 函数将显示类的继承顺序。

print(Student.__mro__)
复制代码

image.png

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()
复制代码

image.png

在子类中定义同名函数,即可重写父类中的函数,并实现与父类不同的功能


相关文章
|
5天前
|
存储 索引 Python
元组(Tuple)在Python编程中的应用与实例
元组(Tuple)在Python编程中的应用与实例
17 2
|
5天前
|
机器学习/深度学习 数据可视化 数据挖掘
Python编程的深入探索与实用案例
Python编程的深入探索与实用案例
16 3
|
15天前
|
存储 算法 安全
Python编程实验六:面向对象应用
Python编程实验六:面向对象应用
41 1
|
1天前
|
数据采集 运维 API
适合所有编程初学者,豆瓣评分8.6的Python入门手册开放下载!
Python是一种跨平台的计算机程序设计语言,它可以用来完成Web开发、数据科学、网络爬虫、自动化运维、嵌入式应用开发、游戏开发和桌面应用开发。 Python上手很容易,基本有其他语言编程经验的人可以在1周内学会Python最基本的内容(PS:没有基础的人也可以直接学习,速度会慢一点) 今天给小伙伴们分享一份Python语言及其应用的手册,这份手册主要介绍 Python 语言的基础知识及其在各个领域的具体应用,基于最新版本 3.x。
|
1天前
|
缓存 测试技术 Python
Python编程中的装饰器应用及性能优化
装饰器是Python中一种强大的功能,它允许我们修改或增强函数或类的行为,而无需修改其本身的代码。装饰器在Python中广泛应用,包括日志记录、性能测试、事务处理、缓存等。 **一、装饰器的基础
|
2天前
|
Python 数据安全/隐私保护 开发工具
练手必备!Python编程实战—23个有趣的实战项目带你快速进阶
Python的练手项目有哪些值得推荐? 已经有6.4W关注,700W次浏览,回答都有450条了,本来遇到这种问题我是不会回答的,毕竟已经有太多人给出了答案,我再去回答就没什么意义了。 但想了想确实有很多刚学Python的并不清楚从哪里去找项目来练手,于是就有了这篇文章,基于这个目的,我也是找了好久,最后还是选择了分享这份手册,毕竟里面有细致的讲解,确实更适合练手一些。
|
4天前
|
Python
Python中的类与对象
Python中的类与对象
8 2
|
5天前
|
C语言 Python
专为编程小白设计的Python零基础入门教程,GitHub星标破W
市面上大多数技术类的书籍都着重于一步步的构建系统的知识体系,并不是说这样就是不对的,但这样按部就班的学习注定了需要花费大量的时间用来掌握“基础知识”,或死记硬背,或慢慢理解。 然而世界不会迁就你,而是在步步紧逼的告诉你要赶紧学完,赶紧找工作,赶紧挣钱,这才是你生活的基础。 今天给小伙伴们带来了一份《编程小白的第一步Python书》,这本书是专为零基础小白设计的,不会告诉“先学C语言,会更好理解Python”这种狗屁道理。而是先带你掌握搭建项目所用到的最少得知识,再真实的项目搭建中实践自己的所学,逐渐的完善知识体系。
|
6天前
|
Python 容器
Python GUI编程(Tkinter)
Python GUI编程(Tkinter)
19 1
|
6天前
|
移动开发 开发框架 安全
Python CGI编程
Python CGI编程
10 0