• 技术文章 >Python技术 >Python基础教程

    python调用父类的三种方法

    小妮浅浅小妮浅浅2021-10-19 09:44:07原创10623

    面向对象的继承中,我们会涉及到父类的调用。在对继承的基础内容有所了解后,我们进一步探究调用父类的三种方法。

    1、父类名字.父类方法名(self,[参数1,参数2,参数3,……])。

    2、super().父类方法名([参数1,参数2,参数3,……])。

    3、super(当前类名字,self).父类方法名([参数1,参数2……])。

    实例

    class Person(object):
        def __init__(self,name,gender):
            self.name =name
            self.gender =gender
            print("Person类__init__()。", "姓名:",self.name)
    class Student(Person):
        def __init__(self,name,gender,score):
            super().__init__(name,gender)
            self.score =score
            print("Student类__init__()。", "姓名:",self.name)
    student = Student('tom','male',10)

    以上就是python调用父类的三种方法,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python类
    品易云
    上一篇:python公有成员和私有成员的介绍 下一篇:python中MRO原则的使用

    相关文章推荐

    • python类的继承如何定义?• python类的继承链分析• python类实例化如何实现• Python类属性如何使用• python类如何自定义实例化• python类的两种属性• python类属性的概念• python类属性的内存分析• python类方法的使用注意• python类属性的两种分类• python类属性和实例属性的区别

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网