菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

Python super() 函数

Python3 内置函数 Python3 内置函数


super() 是 Python 中用于调用父类(超类)方法的内置函数。

在类的继承中,super() 允许你调用父类的方法,实现代码复用和方法扩展。这是 Python 面向对象编程中非常重要的函数。

单词释义: super 意为"超级的",这里指父类或超类。


基本语法与参数

语法格式

super()
super(type, object-or-type)

参数说明

  • 参数 type:
    • 类型: 类
    • 描述: 要获取父类Proxy的类。
  • 参数 object-or-type:
    • 类型: 对象或类
    • 描述: 必须是 type 的实例或子类。

函数说明

  • 返回值: 返回一个代理对象,用于调用父类的方法。
  • 特点: 支持方法解析顺序(MRO),可以正确处理多重继承。

实例

示例 1:在子类中调用父类构造函数

实例

# 父类
class Animal:
def __init__(self, name):
self.name = name
print(f"动物 {self.name} 出生了")

def speak(self):
print("动物发出声音")

# 子类
class Dog(Animal):
def __init__(self, name, breed):
# 调用父类的 __init__
super().__init__(name)
self.breed = breed
print(f"品种是 {self.breed}")

def speak(self):
# 调用父类的方法
super().speak()
print("汪汪!")

# 测试
dog = Dog("小白", "哈士奇")
# 输出:
# 动物 小白 出生了
# 品种是 哈士奇

dog.speak()
# 输出:
# 动物发出声音
# 汪汪!

运行结果预期:

动物 小白 出生了
品种是 哈士奇
动物发出声音
汪汪!

代码解析:

  1. super().__init__(name) 调用父类的构造函数。
  2. super().speak() 调用父类的方法。
  3. 这样可以在子类中扩展父类的功能。

示例 2:多重继承

实例

# 多重继承示例
class A:
def method(self):
print("A.method")

class B(A):
def method(self):
print("B.method")
super().method() # 调用 A.method

class C(A):
def method(self):
print("C.method")
super().method() # 调用 A.method

class D(B, C):
def method(self):
print("D.method")
super().method() # 按 MRO 顺序调用

# MRO: D -> B -> C -> A
d = D()
d.method()
# 输出:
# D.method
# B.method
# C.method
# A.method

# 查看 MRO
print(D.__mro__)

运行结果预期:

D.method
B.method
C.method
A.method
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)

代码解析:

  • super() 按照 MRO(方法解析顺序)调用。
  • 这确保了多重继承的正确性。

示例 3:不使用参数(推荐)

实例

# Python 3 推荐用法:无需参数
class Parent:
def greet(self, name):
return f"你好, {name}!"

class Child(Parent):
def greet(self, name):
# 调用父类方法并扩展
message = super().greet(name)
return message + " 欢迎回来!"

child = Child()
print(child.greet("Tom"))
# 输出: 你好, Tom! 欢迎回来!

运行结果预期:

在实际环境中运行会显示扩展后的问候语。

super() 是 Python 中实现类继承和多态的关键函数。


Python3 内置函数 Python3 内置函数


AI 思考中...

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /