当前位置:网站首页>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())
边栏推荐
- rclone中文文档:常用命令大全
- Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
- 开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- Router 6/ and the difference with router5
- go-etcd
- Code practice - build your own diffusion models / score based generic models from scratch
- Fix the problem that the AI video intelligent platform easycvr device video cannot be played
- Reply and explanation on issues related to "online training of network security education in 2022"
- EasyNVS云管理平台功能重构:支持新增用户、修改信息等
- Record an online interface slow query problem troubleshooting
猜你喜欢
![[lingo] find the shortest path problem of undirected graph](/img/14/1ccae0f33f5857b546d7fd0aa74c35.png)
[lingo] find the shortest path problem of undirected graph

Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?

DC-4靶机

1286_ Implementation analysis of task priority setting in FreeRTOS

C# 读写自定义的Config文件

ctfshow-web354(SSRF)

ctfshow-web355,356(SSRF)

ctfshow-web352,353(SSRF)

ESP32 - ULP 协处理器在低功耗模式下读片内霍尔传感器HALL SENSOR

WiFi settings for raspberry Pie 4
随机推荐
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
Product learning (I) - structure diagram
Which securities company does qiniu school cooperate with? Is it safe to open an account?
We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
图像风格迁移 CycleGAN原理
STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
Database objects: view learning records
【深圳IO】精确食品称(汇编语言的一些理解)
Operation and maintenance management system, humanized operation experience
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
运维管理系统,人性化操作体验
Is it safe to buy funds on the brokerage account
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
[lingo] find the shortest path problem of undirected graph
Esp32 monitors the battery voltage with ULP when the battery is powered
Product learning (II) - competitive product analysis
JSP - paging