主页
  • 主页
  • 分类
  • 热文
  • 教程
  • 面试
  • 标签
Python

Python 基础

Python 主页
Python 概述
Python 历史
Python 功能
Python 与 C++
Python Hello World
Python 应用领域
Python 解释器及其模式
Python 环境设置
Python 虚拟环境
Python 语法
Python 变量
Python 数据类型
Python 类型转换
Python Unicode 系统
Python 文字
Python 运算符
Python 算术运算符
Python 比较运算符
Python 赋值运算符
Python 逻辑运算符
Python 位运算符
Python 成员运算符
Python 身份运算符
Python 运算符优先级
Python 注释
Python 用户输入
Python 数字
Python 布尔值

Python 控制语句

Python 控制流
Python 决策
Python if 语句
Python if-else 语句
Python 嵌套 if 语句
Python match-case 语句
Python 循环
Python For 循环
Python for-else 循环
Python while 循环
Python break 语句
Python Continue 语句
Python pass 语句
Python 嵌套循环

Python 函数和模块

Python 函数
Python 默认参数
Python 关键字参数
Python 关键字专用参数
Python 位置参数
Python 仅限位置参数
Python 任意或可变长度参数
Python 变量作用域
Python 函数注释
Python 模块
Python 内置函数

Python 字符串

Python 字符串
Python 字符串切片
Python 字符串修改
Python 字符串连接
Python 字符串格式化
Python 转义字符
Python 字符串方法
Python 字符串练习

Python 列表

Python 列表
Python 访问列表项
Python 更改列表项
Python 添加列表项
Python 删除列表项
Python 循环列表
Python 列表推导式
Python 排序列表
Python 复制列表
Python 合并列表
Python 列表方法
Python 列表练习

Python 元组

Python 元组
Python 访问元组项
Python 更新元组
Python 解包元组项
Python 循环元组
Python 合并元组
Python 元组方法
Python 元组练习

Python 集合

Python 集合
Python 访问集合项
Python 添加集合项
Python 删除集合项
Python 循环集合
Python 合并集合
Python 复制集合
Python 集合运算符
Python 集合方法
Python 集合练习

Python 字典

Python 字典
Python 访问字典项
Python 更改字典项
Python 添加字典项
Python 移除字典项
Python 字典视图对象
Python 循环字典
Python 复制字典
Python 嵌套字典
Python 字典方法
Python 字典练习

Python 数组

Python 数组
Python 访问数组项
Python 添加数组项
Python 移除数组项
Python 循环数组
Python 复制数组
Python 反转数组
Python 排序数组
Python 合并数组
Python 数组方法
Python 数组练习

Python 文件处理

Python 文件处理
Python 文件写入
Python 文件读取
Python 重命名和删除文件
Python 目录
Python 文件方法
Python 文件/目录方法
Python OS.Path 方法

Python 面向对象编程

Python OOP 概念
Python 类和对象
Python 类属性
Python 类方法
Python 静态方法
Python 构造函数
Python 访问修饰符
Python 继承
Python 多态
Python 方法重写
Python 方法重载
Python 动态绑定
Python 动态类型
Python 抽象
Python 封装
Python 接口
Python 包
Python 内部类
Python 匿名类和对象
Python 单例类
Python 包装器类
Python 枚举
Python 反射

Python 错误和异常

Python 语法错误
Python 异常处理
Python Try-Except
Python Try-Finally
Python 抛出异常
Python 异常链
Python 嵌套 try
Python 用户定义异常
Python 日志记录
Python 断言
Python 内置异常

Python 多线程

Python 多线程
Python 线程生命周期
Python 创建线程
Python 启动线程
Python 合并线程
Python 命名线程
Python 线程调度
Python 线程池
Python 主线程
Python 线程优先级
Python 守护线程
Python 线程同步

Python 同步

Python 线程间通信
Python 死锁
Python 中断线程

Python 网络

Python 网络编程
Python 套接字编程
Python URL 处理
Python 泛型

Python 杂项

Python Date and Time
Python math模块
Python 迭代器
Python 生成器
Python 闭包
Python 装饰器
Python 递归
Python 正则表达式
Python Pip
Python 数据库访问
Python 弱引用
Python 序列化
Python 模板技术
Python 输出格式化
Python 性能测量
Python 数据压缩
Python 通用网关接口
Python XML 处理
Python 用户界面(GUI)
Python 命令行参数
Python Docstrings
Python JSON
Python 发送电子邮件
Python 进一步扩展
Python 工具/实用程序
Python GUI

Python 高级概念

Python 抽象基类
Python 自定义异常
Python 高阶函数
Python 对象的内部机制
Python 内存管理
Python 元类
Python 元编程
Python 模拟与桩
Python 猴子补丁
Python 信号处理
Python 类型提示
Python 进行自动化
Python Humanize包
Python 上下文管理器
Python 协程
Python 描述符
Python 内存泄漏
Python 不可变数据结构

基础

Python 主页
Python 概述
Python 历史
Python 功能
Python 与 C++
Python Hello World
Python 应用领域
Python 解释器及其模式
Python 环境设置
Python 虚拟环境
Python 语法
Python 变量
Python 数据类型
Python 类型转换
Python Unicode 系统
Python 文字
Python 运算符
Python 算术运算符
Python 比较运算符
Python 赋值运算符
Python 逻辑运算符
Python 位运算符
Python 成员运算符
Python 身份运算符
Python 运算符优先级
Python 注释
Python 用户输入
Python 数字
Python 布尔值

控制语句

Python 控制流
Python 决策
Python if 语句
Python if-else 语句
Python 嵌套 if 语句
Python match-case 语句
Python 循环
Python For 循环
Python for-else 循环
Python while 循环
Python break 语句
Python Continue 语句
Python pass 语句
Python 嵌套循环

函数和模块

Python 函数
Python 默认参数
Python 关键字参数
Python 关键字专用参数
Python 位置参数
Python 仅限位置参数
Python 任意或可变长度参数
Python 变量作用域
Python 函数注释
Python 模块
Python 内置函数

字符串

Python 字符串
Python 字符串切片
Python 字符串修改
Python 字符串连接
Python 字符串格式化
Python 转义字符
Python 字符串方法
Python 字符串练习

列表

Python 列表
Python 访问列表项
Python 更改列表项
Python 添加列表项
Python 删除列表项
Python 循环列表
Python 列表推导式
Python 排序列表
Python 复制列表
Python 合并列表
Python 列表方法
Python 列表练习

元组

Python 元组
Python 访问元组项
Python 更新元组
Python 解包元组项
Python 循环元组
Python 合并元组
Python 元组方法
Python 元组练习

集合

Python 集合
Python 访问集合项
Python 添加集合项
Python 删除集合项
Python 循环集合
Python 合并集合
Python 复制集合
Python 集合运算符
Python 集合方法
Python 集合练习

字典

Python 字典
Python 访问字典项
Python 更改字典项
Python 添加字典项
Python 移除字典项
Python 字典视图对象
Python 循环字典
Python 复制字典
Python 嵌套字典
Python 字典方法
Python 字典练习

数组

Python 数组
Python 访问数组项
Python 添加数组项
Python 移除数组项
Python 循环数组
Python 复制数组
Python 反转数组
Python 排序数组
Python 合并数组
Python 数组方法
Python 数组练习

文件处理

Python 文件处理
Python 文件写入
Python 文件读取
Python 重命名和删除文件
Python 目录
Python 文件方法
Python 文件/目录方法
Python OS.Path 方法

面向对象编程

Python OOP 概念
Python 类和对象
Python 类属性
Python 类方法
Python 静态方法
Python 构造函数
Python 访问修饰符
Python 继承
Python 多态
Python 方法重写
Python 方法重载
Python 动态绑定
Python 动态类型
Python 抽象
Python 封装
Python 接口
Python 包
Python 内部类
Python 匿名类和对象
Python 单例类
Python 包装器类
Python 枚举
Python 反射

错误和异常

Python 语法错误
Python 异常处理
Python Try-Except
Python Try-Finally
Python 抛出异常
Python 异常链
Python 嵌套 try
Python 用户定义异常
Python 日志记录
Python 断言
Python 内置异常

多线程

Python 多线程
Python 线程生命周期
Python 创建线程
Python 启动线程
Python 合并线程
Python 命名线程
Python 线程调度
Python 线程池
Python 主线程
Python 线程优先级
Python 守护线程
Python 线程同步

同步

Python 线程间通信
Python 死锁
Python 中断线程

网络

Python 网络编程
Python 套接字编程
Python URL 处理
Python 泛型

杂项

Python Date and Time
Python math模块
Python 迭代器
Python 生成器
Python 闭包
Python 装饰器
Python 递归
Python 正则表达式
Python Pip
Python 数据库访问
Python 弱引用
Python 序列化
Python 模板技术
Python 输出格式化
Python 性能测量
Python 数据压缩
Python 通用网关接口
Python XML 处理
Python 用户界面(GUI)
Python 命令行参数
Python Docstrings
Python JSON
Python 发送电子邮件
Python 进一步扩展
Python 工具/实用程序
Python GUI

高级概念

Python 抽象基类
Python 自定义异常
Python 高阶函数
Python 对象的内部机制
Python 内存管理
Python 元类
Python 元编程
Python 模拟与桩
Python 猴子补丁
Python 信号处理
Python 类型提示
Python 进行自动化
Python Humanize包
Python 上下文管理器
Python 协程
Python 描述符
Python 内存泄漏
Python 不可变数据结构

Python 继承


上一章 下一章

继承是像 Python 这样的面向对象编程语言中最重要特性之一。它允许一个类继承另一个类的特性和行为。继承另一个类的类被称为子类,而被继承的类则被称为基类或父类。

如果需要设计一个新的类,而这个新类的大部分属性在已有的类中已经被很好地定义了,那么就没有必要重新定义它们。继承使得现有类的功能可以被重用,并且在必要的情况下扩展来设计新的类。

当一个新类与现有的类之间存在“是一个”的关系时,继承就起作用了。例如,汽车是一个交通工具,巴士是一个交通工具,自行车也是一个交通工具。在这里,交通工具是父类,而汽车、巴士和自行车是子类。

inheritance

创建父类

父类是指其属性和方法被继承的类。它就像其他类一样使用 class 关键字来定义。

语法

创建父类的语法如下所示:

class ParentClassName:
   {class body}

创建子类

继承自基类的类声明类似于其父类,不过我们需要在括号内提供父类的名称。

语法

子类的语法如下:

class SubClassName (ParentClass1[, ParentClass2, ...]):
   {sub class body}

继承的类型

在 Python 中,继承可以分为五种不同的类别:

  • 单继承
  • 多继承
  • 多级继承
  • 层次继承
  • 混合继承

types of inheritance

Python - 单继承

这是最简单的继承形式,其中子类仅从一个父类继承属性和方法。

示例

下面的例子展示了 Python 中的单继承概念:

# 父类
class Parent: 
   def parentMethod(self):
      print ("调用父类的方法")

# 子类
class Child(Parent): 
   def childMethod(self):
      print ("调用子类的方法")

# 子类的实例
c = Child()  
# 调用子类的方法
c.childMethod() 
# 调用父类的方法
c.parentMethod() 

执行上述代码后,将会得到以下结果:

调用子类的方法
调用父类的方法

Python - 多继承

Python 中的多继承允许你基于多个父类构建一个类。因此,子类继承了所有父类的属性和方法。子类可以覆盖任何父类的方法。

语法

class parent1:
   # 语句

class parent2:
   # 语句

class child(parent1, parent2):
   # 语句

示例

Python 的标准库中有一个内置的 divmod() 函数,返回一个包含两个元素的元组。第一个数字是两个参数的除法结果,第二个是模数。

这个例子试图模仿 divmod() 函数。我们定义了两个类 division 和 modulus,然后有一个继承自它们的 div_mod 类。

class division:
   def __init__(self, a, b):
      self.n = a
      self.d = b
   def divide(self):
      return self.n / self.d

class modulus:
   def __init__(self, a, b):
      self.n = a
      self.d = b
   def mod_divide(self):
      return self.n % self.d
      
class div_mod(division, modulus):
   def __init__(self, a, b):
      self.n = a
      self.d = b
   def div_and_mod(self):
      divval = division.divide(self)
      modval = modulus.mod_divide(self)
      return (divval, modval)

子类有一个新的方法 div_and_mod(),该方法内部调用从继承类继承的 divide() 和 mod_divide() 方法来返回除法和模数。

x = div_mod(10, 3)
print ("除法:", x.divide())
print ("模除:", x.mod_divide())
print ("divmod:", x.div_and_mod())

输出结果如下:

除法: 3.3333333333333335
模除: 1
divmod: (3.3333333333333335, 1)

方法解析顺序(MRO)

方法解析顺序(MRO)与 Python 中的多继承有关。在 Python 中,继承可以在多个层级上进行。例如,A 是 B 的父类,而 B 又是 C 的父类。类 C 可以覆盖继承的方法或者其对象可以按照在父类中定义的方式调用它。那么 Python 是如何找到适当的方法来调用的呢?

每个 Python 类都有一个 mro() 方法,返回 Python 用来解析方法调用的层次顺序。解析顺序是从继承链的底部到顶部。

在前面的例子中,div_mod 类继承了 division 和 modulus 类。因此,mro 方法返回的顺序如下:

[<class '__main__.div_mod'>, <class '__main__.division'>, <class '__main__.modulus'>, <class 'object'>]

Python - 多级继承

在多级继承中,一个类从另一个已经派生出的类中派生。存在多层继承。我们可以想象成祖父母-父母-孩子的关系。

示例

下面的例子展示了多级继承的工作原理:

# 父类
class Universe: 
   def universeMethod(self):
      print ("我在宇宙中")

# 子类
class Earth(Universe): 
   def earthMethod(self):
      print ("我在地球上")
      
# 另一个子类
class India(Earth): 
   def indianMethod(self):
      print ("我在印度")      

# 创建实例 
person = India()  
# 方法调用
person.universeMethod() 
person.earthMethod() 
person.indianMethod() 

当我们执行上述代码时,将会得到以下结果:

我在宇宙中
我在地球上
我在印度

Python - 层次继承

这种类型的继承包含多个从单一基类继承的派生类。这类似于组织内的层次结构。

示例

下面的例子展示了层次继承。这里我们定义了 Manager 类的两个子类。

# 父类
class Manager: 
   def managerMethod(self):
      print ("我是经理")

# 子类
class Employee1(Manager): 
   def employee1Method(self):
      print ("我是员工一")
      
# 第二个子类
class Employee2(Manager): 
   def employee2Method(self):
      print ("我是员工二")      

# 创建实例 
emp1 = Employee1()  
emp2 = Employee2()
# 方法调用
emp1.managerMethod() 
emp1.employee1Method()
emp2.managerMethod() 
emp2.employee2Method()  

执行上述程序后,你会得到以下输出:

我是经理
我是员工一
我是经理
我是员工二

Python - 混合继承

两种或多种类型的继承的组合被称为混合继承。例如,它可以是单继承和多继承的混合。

示例

在这个例子中,我们将单继承和多继承结合起来形成类的混合继承。

# 父类
class CEO: 
   def ceoMethod(self):
      print ("我是 CEO")
      
class Manager(CEO): 
   def managerMethod(self):
      print ("我是经理")

class Employee1(Manager): 
   def employee1Method(self):
      print ("我是员工一")
      
class Employee2(Manager, CEO): 
   def employee2Method(self):
      print ("我是员工二")      

# 创建实例 
emp = Employee2()
# 方法调用
emp.managerMethod() 
emp.ceoMethod()
emp.employee2Method()

执行上述程序后,将会得到以下结果:

我是经理
我是 CEO
我是员工二

super() 函数

在 Python 中,super() 函数允许你在子类中访问父类的方法和属性。

示例

在下面的例子中,我们创建了一个父类,并使用 super() 函数从子类中访问其构造函数。

# 父类
class ParentDemo:
   def __init__(self, msg):
      self.message = msg

   def showMessage(self):
      print(self.message)

# 子类
class ChildDemo(ParentDemo):
   def __init__(self, msg):
      # 使用 super 函数
      super().__init__(msg)  

# 创建实例
obj = ChildDemo("欢迎来到 Tutorialspoint!!")
obj.showMessage()  

执行上述程序后,将会得到以下结果:

欢迎来到 Tutorialspoint!!
上一章 下一章
阅读号二维码

关注阅读号

联系二维码

联系我们

© 2024 Yoagoa. All rights reserved.

粤ICP备18007391号

站点地图