当前位置:网站首页>Subclasses call methods and properties of the parent class with the same name
Subclasses call methods and properties of the parent class with the same name
2022-07-01 07:07:00 【Beishan】
Python Object oriented , Use the subclass to call the methods and properties of the parent class with the same name .
First, we define two parent classes ,Master and School
# @author: Beishan
# FileName: 12 The subclass calls the method of the parent class with the same name
# @time: 2022/6/5 14:54
# Parent class Master
class Master():
def __int__(self):
self.kongfu = '[ Old recipe for pancakes and fruit ]'
def make_cake(self):
print(f' Application {
self.kongfu} Make pancakes and fruit ')
# Parent class School
class School():
def __init__(self):
self.kongfu = '[ Black horse pancake fruit formula ]'
def make_cake(self):
print(f' Application {
self.kongfu} Make pancakes and fruit ')
For the parent class of the same property and method , How to call subclasses
ad locum , Definition Prentice class , Inherited Master and School class , Added properties and methods with the same name as the parent class
- Call the properties and methods of subclasses
If the properties and methods of the parent class are called , The parent class attribute will automatically mask the cover class attribute , Before stock disaster call attribute , First call your own subclass initialization
class Prentice(School,Master):
def __init__(self):
self.kongfu = '[ Original recipe for pancakes and fruits ]'
def make_cake(self):
self.__init__
print(f' Application {
self.kongfu} Make pancakes and fruit ')
- Call parent class properties and methods
If you call a parent method , But to ensure that the property of the parent class is called , The initialization of the parent class must be called before the method
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake()
Complete code
# @author: Beishan
# FileName: 12 The subclass calls the method of the parent class with the same name
# @time: 2022/6/5 14:54
class Master():
def __int__(self):
self.kongfu = '[ Old recipe for pancakes and fruit ]'
def make_cake(self):
print(f' Application {
self.kongfu} Make pancakes and fruit ')
class School():
def __init__(self):
self.kongfu = '[ Beishanla pancake fruit recipe ]'
def make_cake(self):
print(f' Application {
self.kongfu} Make pancakes and fruit ')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[ Original recipe for pancakes and fruits ]'
''' If the properties and methods of the parent class are called , The Foley attribute will automatically mask the cover class attribute , Before stock disaster call attribute , First call your own subclass initialization '''
def make_cake(self):
# self.__init__
print(f' Application {
self.kongfu} Make pancakes and fruit ')
''' Call the superclass method , But in order to ensure that the property of Frey is called , The initialization of the parent class must be called before the method '''
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake()
beishan = Prentice()
print(beishan.make_cake())
print(beishan.make_master_cake())
print(beishan.make_school_cake())
边栏推荐
- Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
- K8s set up redis cluster
- 如何制作专属的VS Code主题
- 【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
- MySQL data type learning notes
- 8 张图 | 剖析 Eureka 的首次同步注册表
- [Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
- AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
- Principle of introducing modules into node
- Is it safe to buy funds on the brokerage account
猜你喜欢

C# 读写自定义的Config文件

Jena default inference query based on OWL

Code practice - build your own diffusion models / score based generic models from scratch

Product learning (I) - structure diagram

Fix the problem that the AI video intelligent platform easycvr device video cannot be played

【计网】(一) 集线器、网桥、交换机、路由器等概念
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)

How to enter the Internet industry and become a product manager? How to become a product manager without project experience?

【LINGO】求无向图的最短路问题

ctfshow-web354(SSRF)
随机推荐
Rclone access web interface
Paging in servlets and JSPS
运维面临挑战?智能运维管理系统来帮您
Problem: officeexception: failed to start and connect (II)
ctfshow-web355,356(SSRF)
如何进入互联网行业,成为产品经理?没有项目经验如何转行当上产品经理?
[network planning] (I) hub, bridge, switch, router and other concepts
go-etcd
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
Image style migration cyclegan principle
【图像处理】图像直方图均衡化系统含GUI界面
Understanding of Turing test and Chinese Room
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
【深圳IO】精确食品称(汇编语言的一些理解)
如何制作专属的VS Code主题
AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
Rclone Chinese document: a collection of common commands
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing