面向对象编程之super内置函数的用法

简介: 先来看一段代码:定义一个名叫People的父类,又定义了一个叫Teacher的老师类和一个叫Student的学生类来继承People的类,并根据这两个子类实例化出两个对象s1和t1。class Date: def __init__(self,year,mon,day): self.

先来看一段代码:

定义一个名叫People的父类,又定义了一个叫Teacher的老师类和一个叫Student的学生类
来继承People的类,并根据这两个子类实例化出两个对象s1和t1

class Date:
    def __init__(self,year,mon,day):
        self.year=year
        self.mon=mon
        self.day=day

    def birth_info(self):
        print("The birth is %s-%s-%s"%(self.year,self.mon,self.day))

class People:
    def __init__(self,name,age):
        self.name=name
        self.age=age


    def walk(self):
        print("%s is walking"%self.name)

class Teacher(People):
    def __init__(self,name,age,year,mon,day,course):
        People.__init__(self,name,age)
        self.course=course
        self.birth=Date(year,mon,day)

    def teach(self):
        print("%s is teaching"%self.name)

class Student(People):
    def __init__(self,name,age,year,mon,day,group):
        People.__init__(self,name,age)
        self.birth = Date(year, mon, day)
        self.group=group

    def study(self):
        print("%s is studying"%self.name)
t1=Teacher("alex",28,1989,9,2,"python")
s1=Student("jack",22,1995,2,8,"group2")

现在问题来了,假如因为需要,我要修改老师类和学生类的父类People的名字。

这样一来,在老师类Teacher和学生类Student中继承的类People也要修改,以及它们
调用的init方法的那个父类也要修改名字,太麻烦了有没有?

这时候就可以使用super()这个内置函数来搞定了。

在python解释器中查看帮助信息:

help(super)

得到如下信息:

Help on class super in module builtins:

class super(object)
 |  super() -> same as super(__class__, <first argument>)
 |  super(type) -> unbound super object
 |  super(type, obj) -> bound super object; requires isinstance(obj, type)
 |  super(type, type2) -> bound super object; requires issubclass(type2, type)

super是一个内置函数,加括号就得到一个对象,对象super()加"."可以直接调用父类的init方法。

这个对象在调用父类的init时,实际上就是在调用父类的绑定方法,所以就不需要在括号里加上self了。

修改后的代码如下:

class Date:
    def __init__(self,year,mon,day):
        self.year=year
        self.mon=mon
        self.day=day

    def birth_info(self):
        print("The birth is %s-%s-%s"%(self.year,self.mon,self.day))

class People:
    def __init__(self,name,age):
        self.name=name
        self.age=age


    def walk(self):
        print("%s is walking"%self.name)

class Teacher(People):
    def __init__(self,name,age,year,mon,day,course):
        super().__init__(name,age)
        self.course=course
        self.birth=Date(year,mon,day)

    def teach(self):
        print("%s is teaching"%self.name)

class Student(People):
    def __init__(self,name,age,year,mon,day,group):
        super().__init__(name,age)
        self.birth = Date(year, mon, day)
        self.group=group

    def study(self):
        print("%s is studying"%self.name)
t1=Teacher("alex",28,1989,9,2,"python")
s1=Student("jack",22,1995,2,8,"group2")     

这样一来,父类的名字改变了,代码里面继承的父类的init方法的名字也不需要修改了。

python2中,也可以使用super,其调用方法为:super(Teacher,self)

使用super()函数时,python会在mro列表中继续搜索下一个类。

只要每个重定义的方法统一使用super()并只调用它一次,那么控制流最终会遍历完整个mro列表。每个方法只会调用一次。
使用super调用的所有的属性,都是从mro列表当前的位置往后找,看mro列表的顺序就可以看到子类的继承关系

查看上面代码中Teacher这个子类的mro列表可以使用这个方法:

Teacher.mro()   

使用super可以避免使用多重继承时,子类继承父类的顺序问题。
子类继承父类的数据属性和函数属性时,先执行的先生效,当后面的代码与前面的代码有冲突时,
后面的代码会把前面的代码覆盖掉,不使用super时需要自己解决继承的顺序问题,使用super就可以很好的解决这个问题了。

目录
相关文章
|
1月前
|
Python
在Python中,如何使用装饰器重写类的方法?
【2月更文挑战第24天】【2月更文挑战第79篇】在Python中,如何使用装饰器重写类的方法?
|
5月前
|
Python
浅谈Python面向对象中的继承与mro
浅谈Python面向对象中的继承与mro
25 0
|
4月前
|
算法 编译器 C++
C++11 Lambda表达式的用法与原理
C++11 Lambda表达式的用法与原理
57 0
|
存储 Python
Python装饰器2-__call__方法与类装饰器
__call__方法、创建类装饰器、装饰器的应用场景
Python装饰器2-__call__方法与类装饰器
|
8月前
|
Python
【从零学习python 】44.面向对象编程中的私有属性和方法
【从零学习python 】44.面向对象编程中的私有属性和方法
43 0
|
8月前
|
安全 Java 编译器
Kotlin 学习笔记(一)—— 基本类型、函数、lambda、类与对象的写法
Kotlin 作为 Android 开发的首选语言,其基础语法和特性需要重点学习。本文概括总结了 Kotlin 的基本类型、关键字、函数、闭包、高阶函数、类与对象等核心知识点,并给出示例代码进行讲解。
152 0
Kotlin 学习笔记(一)—— 基本类型、函数、lambda、类与对象的写法
|
8月前
|
存储 安全 数据安全/隐私保护
【100天精通python】Day12:面向对象编程_属性和继承的基本语法与示例
【100天精通python】Day12:面向对象编程_属性和继承的基本语法与示例
60 0
|
10月前
|
算法 C++ Python
python 多态和 super 用法
python 多态和 super 用法
73 0
|
Python
Python基础 动态绑定属性和方法 (类和对象3)
python类和对象,动态绑定属性,动态绑定方法
Python基础 动态绑定属性和方法 (类和对象3)
|
Python
【透彻】Python装饰器进阶(类装饰器+带参数的装饰器+多装饰器)| Python 主题月
【透彻】Python装饰器进阶(类装饰器+带参数的装饰器+多装饰器)| Python 主题月
146 0

热门文章

最新文章