当前位置:网站首页>子类调用父类的同名方法和属性
子类调用父类的同名方法和属性
2022-07-01 06:13:00 【北山啦】
Python面向对象中,利用子类调用父类的同名方法和属性。
首先我们定义了两个父类,Master和School
# @author: 北山啦
# FileName: 12子类调用父类的同名方法
# @time: 2022/6/5 14:54
#父类Master
class Master():
def __int__(self):
self.kongfu = '[古法煎饼果子配方]'
def make_cake(self):
print(f'运用{
self.kongfu}制作煎饼果子')
#父类School
class School():
def __init__(self):
self.kongfu = '[黑马煎饼果子配方]'
def make_cake(self):
print(f'运用{
self.kongfu}制作煎饼果子')
对于相同属性和方法的父类,子类如何来调用呢
在这里,定义Prentice类,继承了Master和School类,添加了和父类同名属性和方法
- 调用子类的属性和方法
如果调用了父类的属性和方法,父类属性会自动掩盖子类属性,股灾调用属性前,先调用自己子类的初始化
class Prentice(School,Master):
def __init__(self):
self.kongfu = '[独创煎饼果子配方]'
def make_cake(self):
self.__init__
print(f'运用{
self.kongfu}制作煎饼果子')
- 调用父类属性和方法
如果调用父类方法,但是为了保障调用到的是父类的属性,必须在方法前调用父类的初始化
def make_master_cake(self):
Master.__init__(self)
Master.make_cake(self)
def make_school_cake(self):
School.__init__(self)
School.make_cake()
完整代码
# @author: 北山啦
# FileName: 12子类调用父类的同名方法
# @time: 2022/6/5 14:54
class Master():
def __int__(self):
self.kongfu = '[古法煎饼果子配方]'
def make_cake(self):
print(f'运用{
self.kongfu}制作煎饼果子')
class School():
def __init__(self):
self.kongfu = '[北山啦煎饼果子配方]'
def make_cake(self):
print(f'运用{
self.kongfu}制作煎饼果子')
class Prentice(School, Master):
def __init__(self):
self.kongfu = '[独创煎饼果子配方]'
'''如果调用了父类的属性和方法,弗列属性会自动掩盖子类属性,股灾调用属性前,先调用自己子类的初始化'''
def make_cake(self):
# self.__init__
print(f'运用{
self.kongfu}制作煎饼果子')
'''调用父类方法,但是为了保障调用到的是弗列的属性,必须在方法前调用父类的初始化'''
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())
边栏推荐
- three. JS summary
- Projects and dependencies in ABP learning solutions
- Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
- Arcserver password reset (account cannot be reset)
- OpenGL es: (1) origin of OpenGL es (transfer)
- SystemVerilog learning-10-validation quantification and coverage
- Treasure taking from underground palace (memory based deep search)
- Essay learning record essay multi label Global
- ManageEngine卓豪助您符合ISO 20000标准(四)
- Uniapp tree level selector
猜你喜欢

Multi label lsml for essay learning records

手把手教你实现一个深度学习框架...

FPGA - clocking -02- clock wiring resources of internal structure of 7 Series FPGA

记磁盘扇区损坏导致的Mysql故障排查
![[file system] how to run squashfs on UBI](/img/d7/a4769420c510c47f3c2a615b514a8e.png)
[file system] how to run squashfs on UBI

IT服务管理(ITSM)在高等教育领域的应用

高阶-二叉搜索树详解

The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same

数据库产生死锁了请问一下有没有解决办法

3D printer threading: five simple solutions
随机推荐
HDU - 1501 Zipper(记忆化深搜)
Differences between in and exists in MySQL
What if the data in the cloud disk is harmonious?
手把手教你实现一个深度学习框架...
DHT11 temperature and humidity sensor
【ManageEngine卓豪】用统一终端管理助“欧力士集团”数字化转型
【ManageEngine卓豪】局域网监控的作用
Make: g++: command not found
srpingboot security demo
局域网监控软件有哪些功能
three.js小结
SystemVerilog learning-10-validation quantification and coverage
Treasure taking from underground palace (memory based deep search)
Tidb database characteristics summary
Diffusion (multi-source search)
The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same
c# Xml帮助类
Save data in browser to local file
Restframework-simplejwt rewrite authentication mechanism
Database problems, how to optimize Oracle SQL query statements faster and more efficient