当前位置:网站首页>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())
边栏推荐
- (上)苹果有开源,但又怎样呢?
- [FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
- Code practice - build your own diffusion models / score based generic models from scratch
- The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
- 【深圳IO】精确食品称(汇编语言的一些理解)
- ctfshow-web355,356(SSRF)
- Is it safe to do fund fixed investment on Great Wall Securities?
- Rclone configuring Minio and basic operations
- 解决无法读取META-INF.services里面定义的类
- 为什么这么多人转行产品经理?产品经理发展前景如何?
猜你喜欢
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
JAX的深度学习和科学计算
SQL learning notes nine connections 2
Image style migration cyclegan principle
Servlet 和 JSP 中的分页
C# 读写自定义的Config文件
Solution to the problem that objects in unity2021 scene view cannot be directly selected
The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
Esp32 monitors the battery voltage with ULP when the battery is powered
JSP - paging
随机推荐
Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
C language implementation [minesweeping game] full version (implementation source code)
【分类模型】Q 型聚类分析
图像风格迁移 CycleGAN原理
【系统分析师之路】第五章 复盘软件工程(逆向净室与模型驱动开发)
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
【LINGO】求解二次规划
ctfshow-web355,356(SSRF)
【MATLAB】求解非线性规划
手机开户选哪个证券公司比较好,哪个更安全
MySQL constraint learning notes
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
[recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
【LINGO】求七个城市最小连线图,使天然气管道价格最低
DC-4 target
Buildreoot override mechanism
Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
5G Massive MIMO的概念和优点总结
Will Internet talents be scarce in the future? Which technology directions are popular?