Python构造函数的继承

简介: 在继承中,子类会自动获得一份父类的所有属性和方法,同时还可以定义自己的属性和方法。

在继承中,子类会自动获得一份父类的所有属性和方法,同时还可以定义自己的属性和方法。


对于构造函数的定义会有以下几种情况:


1、父类有构造函数,子类没有


class Base:
    def __init__(self, name, age):
        self.m_Name = name
        self.m_Age = 10
        self.m_Address = 'China'
        pass
    def PrintName(self):
        print('My name is ' + self.m_Name)
    pass
class Derived(Base):
    pass
derived = Derived()

改代码运行会出现错误:

image.png

由错误提示可看出,需要有两个实参传给__init__()函数,而子类中并没有定义,说明子类继承了父类的构造函数。

更改之后代码如下:

class Base:
    def __init__(self, name, age):
        self.m_Name = name
        self.m_Age = 10
        self.m_Address = 'China'
        pass
    def PrintName(self):
        print('My name is ' + self.m_Name)
    pass
class Derived(Base):
    pass
derived = Derived('Foer Kent', 18)
print(derived.m_Age)
derived.PrintName()

执行结果为:

image.png

此时说明,子类已经通过继承下来的构造函数,对继承下来的属性进行赋值,方法也可执行。


2、父类中有构造函数,子类中也有,但是没有使用super()函数


class Base:
    def __init__(self, name, age):
        self.m_Name = name
        self.m_Age = 10
        self.m_Address = 'China'
        pass
    def PrintName(self):
        print('My name is ' + self.m_Name)
    pass
class Derived(Base):
    def __init__(self, name, age):
        pass
    pass
derived = Derived('Foer Kent', 18)
print(derived.m_Age)
derived.PrintName()

会出现错误:

image.png

由错误提示可见,此时子类中并没继承父类中的实例属性,是因为当子类定义了构造函数时,相当于重写了父类中的方法,此时相当于在父类中与子类重名的方法没有被继承,而m_Age几个属性是在父类构造函数中定义的实例属性,没有调用父类构造函数自然就不会有这几个属性,就不会被继承下来。但如果是父类中的类属性,会被继承下来,具体操作不在演示,读者可自行尝试。

所以连后面的print方法也不会执行,因为就没有m_Name这个属性。

注意:Python中多态的重写方法问题,重写的方法只要方法名相同,参数列表中个数不同,也相当于发生了重写,如果想调用父类中的方法,而不是子类中的方法,将用到以下要讲的super()函数。

多态可参照https://blog.csdn.net/Falcont/article/details/115695910

属性分类可参照https://blog.csdn.net/Falcont/article/details/115696093


3、父类中有构造函数,子类中有,且通过super()函数进行关联


class Base:
    def __init__(self, name, age):
        self.m_Name = name
        self.m_Age = 10
        self.m_Address = 'China'
        pass
    def PrintName(self):
        print('My name is ' + self.m_Name)
    pass
class Derived(Base):
    def __init__(self, name, age):
        super(Derived, self).__init__(name, age)
        pass
    pass
derived = Derived('Foer Kent', 18)
print(derived.m_Age)
derived.PrintName()

super(子类名,self).init(value1,value2),此处参数列表不需要self,只需要与父类中除self之外的相同即可。super后面的()中参数可以不写。即直接super().init(父类中构造函数除了self以外的形参)

此时将显示:

image.png

super()函数使得Python调用的是父类的构造函数,而不是子类的。

目录
相关文章
|
13天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
14 1
|
27天前
|
Python
Python函数使用(四)
Python函数使用(四)
60 0
|
1天前
|
数据挖掘 数据处理 索引
python常用pandas函数nlargest / nsmallest及其手动实现
python常用pandas函数nlargest / nsmallest及其手动实现
9 0
|
6天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
38 1
|
6天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
|
8天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
48 0
|
8天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
8天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
9天前
|
Python
python学习10-函数
python学习10-函数
|
9天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环