当前位置:网站首页>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())
边栏推荐
- Introduction to spark (one article is enough)
- 【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现
- Fix the problem that the AI video intelligent platform easycvr device video cannot be played
- 為什麼這麼多人轉行產品經理?產品經理發展前景如何?
- Code practice - build your own diffusion models / score based generic models from scratch
- Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
- Why did grayscale fall from the altar?
- C# Newtonsoft.Json中JObject的使用
- ctfshow-web355,356(SSRF)
- ctfshow-web355,356(SSRF)
猜你喜欢

WiFi settings for raspberry Pie 4

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

1286_ Implementation analysis of task priority setting in FreeRTOS

Unity2021-Scene视图中物体无法直接选中的解决办法

Dirty reading, unreal reading and unrepeatable reading

DC-4靶机

Are there any practical skills for operation and maintenance management
![[matlab] solve nonlinear programming](/img/2e/7a1f520b602b7539be479efb198f6a.png)
[matlab] solve nonlinear programming

DC-4 target

ESP32 ESP-IDF ADC监测电池电压(带校正)
随机推荐
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
如何通过cdn方式使用阿里巴巴矢量图字体文件
记一次线上接口慢查询问题排查
Database objects: view learning records
Operation and maintenance management system, humanized operation experience
K8s set up redis cluster
Stored procedure learning notes
(上)苹果有开源,但又怎样呢?
【分类模型】Q 型聚类分析
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
Is it safe to do fund fixed investment on Great Wall Securities?
Pourquoi tant de gens sont - ils devenus des gestionnaires de produits? Quelles sont les perspectives de développement des gestionnaires de produits?
Solution to the problem that objects in unity2021 scene view cannot be directly selected
[lingo] solve quadratic programming
Jena基于OWL的默认推理查询
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
rclone配置minio及基本操作
LeetCode+ 71 - 75
Product learning (I) - structure diagram
Chinese explanation of common rclone subcommands