Python super 函数

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门


一.Python super 函数简介

     Python 内置函数 super 主要用于类的多继承中,用来查找并调用父类的方法,所以在单重继承中用不用 super 都没关系;但是,使用 super  是一个好的习惯。一般我们在子类中需要调用父类的方法时才会这么用;


二.Python super 函数语法

'''
参数:
    type — 类,一般是类名;
    object-or-type — 类,一般是 self;

返回值:无
'''

super(type,object-or-type)

三.Python super 函数使用

1.案例一

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python super 函数.py
@Time:2021/04/29 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""

class A:
    def m(self):
        print('A')

class B:
    def m(self):
        print('B')

class C(A):
    def m(self):
        print('C')
        super().m()

C().m()

'''
输出结果:
C
A
'''

     代码分析:这样做的好处就是:如果你要改变子类继承的父类(由 A 改为 B ),你只需要修改一行代码(class C(A): -> class C(B))即可,而不需要在 class C 的大量代码中去查找、修改基类名,另外一方面代码的可移植性和重用性也更高。


2.案例二:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python super 函数.py
@Time:2021/04/29 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""

class Dog:
    def __init__(self):
          self.fly = False
    def print_fly(self):
          if self.fly:
               print('不是普通狗,能飞')
          else:
               print('普用狗不会飞')

class xiaotianquan(Dog):
     def __init__(self):
         self.sound = True

     def print_sing(self):
          if self.sound:
              print("汪汪汪")
          else:
              print("假狗狗")

if __name__ == '__main__':
    dog = xiaotianquan()
    dog.print_sing()  # 能正常输出
    dog.print_fly()  # 报错,AttributeError: 'xiaotianquan' object has no attribute 'fly'

     代码分析:虽然子类 xiaotianquan 继承父类 Dog ,但是子类直接调用父类的 print_fly 函数,依然会报错,因为子类没有父类的 fly 属性,上面代码可以通过在 __init__ 函数中调用 super 完成,例如:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python super 函数.py
@Time:2021/04/29 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""


class Dog:
    def __init__(self):
          self.fly = False
    def print_fly(self):
          if self.fly:
               print('不是普通狗,能飞')
          else:
               print('普用狗不会飞')

class xiaotianquan(Dog):
     def __init__(self): 
         super().__init__() # 等效  super(xiaotianquan,self).__init__()
         self.fly = True
         self.sound = True


     def print_sing(self):
          if self.sound:
              print("汪汪汪")
          else:
              print("假狗狗")

if __name__ == '__main__':
    dog = xiaotianquan()
    dog.print_sing()  
    dog.print_fly()

'''
输出结果:

汪汪汪

不是普通狗,能飞
'''

四.猜你喜欢

  1. Python for循环
  2. Python 字符串
  3. Python 列表list
  4. Python 元组tuple
  5. Python 字典 dict
  6. Python 条件推导式
  7. Python 列表推导式
  8. Python 字典推导式
  9. Python 函数声明和调用
  10. Python 不定长参数 *argc/**kargcs
  11. Python 匿名函数 lambda
  12. Python return 逻辑判断表达式
  13. Python 字符串/列表/元组/字典之间的相互转换
  14. Python 局部变量和全局变量
  15. Python type 函数和 isinstance 函数区别
  16. Python is 和 == 区别
  17. Python 可变数据类型和不可变数据类型
  18. Python 浅拷贝和深拷贝

ChatGPT 3.5 国内中文镜像站免费使用啦
文章版权声明 1、本网站名称:猿说编程
2、本站永久网址:https://www.codersrc.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。

© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容