Python类继承及super()函数使用说明
作者:waifdzdn 发布时间:2023-12-06 19:23:07
标签:Python,类继承,super
Python中单类继承
Python是一门面向对象的编程语言,支持类继承。
新的类称为子类(Subclass),被继承的类称为父类、基类或者超类。子类继承父类后,就拥有父类的所有特性。
类继承的简单例子:
普通类方法继承
class Fruit():
def color(self):
print("colorful")
class Apple(Fruit):
pass
class Orange(Fruit):
pass
apple = Apple()
orange = Orange()
apple.color()
orange.color()
# 输出
# colorful
# colorful
这里Fruit为父类,Apple和Orange为子类,子类继承了父类的特性,因此Apple和Orange也拥有Color方法。
子类除了可以继承父类的方法,还可以覆盖父类的方法:
class Fruit():
def color(self):
print("colorful")
class Apple(Fruit):
def color(self):
print("red")
class Orange(Fruit):
def color(self):
print("orange")
apple = Apple()
orange = Orange()
apple.color()
orange.color()
# 输出
# red
# orange
子类可以在继承父类方法的同时,对方法进行重构。
这样一来,子类的方法既包含父类方法的特性,同时也包含子类自己的特性:
class Fruit():
def color(self):
print("Fruits are colorful")
class Apple(Fruit):
def color(self):
super().color()
print("Apple is red")
class Orange(Fruit):
def color(self):
super().color()
print("Orange is orange")
apple = Apple()
orange = Orange()
apple.color()
orange.color()
# 输出
# Fruits are colorful
# Apple is red
# Fruits are colorful
# Orange is orange
初始化函数继承
如果我们需要给类传入参数,需要使用初始化函数。如果所有子类中部分参数是相同的,那么可以在父类的初始化函数中定义这些参数,然后子类继承父类的初始化函数,这样所有子类就可共享这些参数,而不需要在每个子类中单独定义。
初始化函数的继承:
class Fruit():
def __init__(self, color, shape):
self.color = color
self.shape = shape
class Apple(Fruit):
def __init__(self, color, shape, taste):
Fruit.__init__(self, color, shape) # 等价于super().__init__(color, shape)
self.taste = taste
def feature(self):
print("Apple's color is {}, shape is {} and taste {}".format(
self.color, self.shape, self.taste))
class Orange(Fruit):
def __init__(self, color, shape, taste):
Fruit.__init__(self, color, shape)
self.taste = taste
def feature(self):
print("Orange's color is {}, shape is {} and taste {}".format(
self.color, self.shape, self.taste))
apple = Apple("red", "square", "sour")
orange = Orange("orange", "round", "sweet")
apple.feature()
orange.feature()
# 输出
# Apple's color is red, shape is square and taste sour
# Orange's color is orange, shape is round and taste sweet
Python中多类继承
在单类继承中,super()函数用于指向要继承的父类,且不需要显式的写出父类名称。
但是在多类继承中,会涉及到查找顺序(MRO)、钻石继承等问题。
MRO 是类的方法解析顺序表, 也就是继承父类方法时的顺序表。
钻石继承
A
/ \
B C
\ /
D
如图所示,A是父类,B和C继承A,D继承B和C。
下面举例说明钻石继承的继承顺序
class Plant():
def __init__(self):
print("Enter plant")
print("Leave plant")
class Fruit(Plant):
def __init__(self):
print("Enter Fruit")
super().__init__()
print("Leave Fruit")
class Vegetable(Plant):
def __init__(self):
print("Enter vegetable")
super().__init__()
print("Leave vegetable")
class Tomato(Fruit, Vegetable):
def __init__(self):
print("Enter Tomato")
super().__init__()
print("Leave Tomato")
tomato = Tomato()
print(Tomato.__mro__)
# 输出
# Enter Tomato
# Enter Fruit
# Enter vegetable
# Enter plant
# Leave plant
# Leave vegetable
# Leave Fruit
# Leave Tomato
# (<class '__main__.Tomato'>, <class '__main__.Fruit'>, <class '__main__.Vegetable'>, <class '__main__.Plant'>, <class 'object'>)
来源:https://blog.csdn.net/w1301100424/article/details/93858890


猜你喜欢
- python 调用系统ffmpeg进行视频截图,并进行图片http发送ffmpeg ,视频、图片的各种处理。 最近在做视频、图片
- SOA 服务用消息进行通信,该消息通常使用XML Schema来定义(也叫做XSD, XML Schema Definition)。消费者和
- 这篇文章主要介绍了Python搭建HTTP服务过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 使用 pd.Series把dataframe转成Seriests = pd.Series(df['Value'].value
- MySQL数据库管理软件有两种版本,一种是企业版,一种是社区版,其中,前者是收费的,如果是个人使用的,社区版足矣。下载mysql-5.7.1
- 我所接触的多标签数据,主要包括两类:1、一张图片属于多个标签,比如,data:一件蓝色的上衣图片.jpg,label:蓝色,上衣。其中lab
- 网站中很多表单都会用到上传图片,logo,照片,用户也会上传图片,这个时候网站就需要一个上传图片的功能,而且在上传后希望能预览一下看上传的对
- 链表由一系列不必在内存中相连的结构构成,这些对象按线性顺序排序。每个结构含有表元素和指向后继元素的指针。最后一个单元的指针指向NULL。为了
- 前言最近因为工作需要 用selenium做了一个QQ邮箱的爬虫(登录时部分帐号要滑动解锁),先简单记录一下。这个问题先可以分为两个部分:1.
- 前言MySQL 的权限表在数据库启动的时候就载入内存,当用户通过身份认证后,就在内存中进行相应权限的存取,这样,此用户就可以在数据库中做权限
- 打桩测试当我们在编写单元测试的时候,有时我们非常想 mock 掉其中一个方法,但是这个方法又没有接口去定义和实现(无法用 gith
- 该 GIF 图来自于官网,文末有给出链接。描述依托于百度网盘巨大的的云存储空间,绝大数人会习惯性的将一些资料什么的存储到上面,但是有的私密链
- 定时任务:1、 线程睡眠函数 sleep() ——粗暴!一直占有 CPU 资源,导致后续操作无法执行2、 threading.Timer(1
- 严格控制Session可以将不需要Session的内容(比如帮助画面,访问者区域,等等)移动到关闭Session的独立ASP应用程序中。在基
- 最近在学习Python3网络爬虫开发实践(崔庆才 著)刚好也学习到他使用代理爬取公众号文章这里,但是照着他的代码写,出现了一些问题。在这里我
- 阅读上一篇:FrontPage XP设计教程5——表单的设计 在制作出图文并茂的网页之后,很多读者朋友还想让自己的网页能够播放音乐、视频等多
- Scrapy回调函数回调方法示例:yield Request(url=self.base_url + 'QueryInfo'
- 前言大家应该都有所体会,在不同的项目可能会使用不同的Django版本,兼任性是大问题,如果不幸要去接手不同版本的项目,比较惨烈!如果想重装一
- 在机器上首次安装MySQL,操作系统是win7mysql 的安装文件是 zip 格式的,版本是5.7.17解压之后,安装步骤是1、首先找个文
- 问题你需要将数字格式化后输出,并控制数字的位数、对齐、千位分隔符和其他的细节。解决方案格式化输出单个数字的时候,可以使用内置的 format