当前位置:网站首页>子类调用父类的同名方法和属性
子类调用父类的同名方法和属性
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())
边栏推荐
- SystemVerilog learning-09-interprocess synchronization, communication and virtual methods
- kotlin位运算的坑(bytes[i] and 0xff 报错)
- UOW of dev XPO comparison
- 交换机配置软件具有的作用
- 机械臂速成小指南(六):步进电机驱动器
- MySQL怎么存储emoji?
- DEV XPO对比之UOW
- Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
- 指数法和Random Forest实现山东省丰水期地表水体信息
- ONEFLOW source code parsing: automatic inference of operator signature
猜你喜欢
![[note] e-commerce order data analysis practice](/img/03/367756437be947b5b995d5f7f55236.png)
[note] e-commerce order data analysis practice

Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village

解决麒麟V10上传文件乱码问题

让田头村变甜头村的特色农产品是仙景芋还是白菜

OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow

HCM Beginner (I) - Introduction

Thesis learning record essay multi label lift

Pla ne colle pas sur le lit: 6 solutions simples

连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?

分布式锁实现
随机推荐
记磁盘扇区损坏导致的Mysql故障排查
HDU - 1501 zipper (memory deep search)
端口扫描工具对企业有什么帮助?
Diffusion (multi-source search)
3D打印机穿线:5种简单的解决方案
HCM Beginner (II) - information type
three.js小结
【文件系统】如何在ubi之上运行squashfs
MongoDB:一、MongoDB是什么?MongoDB的优缺点
How does MySQL store Emoji?
【ManageEngine卓豪】局域网监控的作用
FPGA - clocking -02- clock wiring resources of internal structure of 7 Series FPGA
Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
SystemVerilog learning-06-class encapsulation
Skywalking integrated Nacos dynamic configuration
Transformer le village de tiantou en un village de betteraves sucrières
kubeadm搭建kubenetes 集群(个人学习版)
利用百度地图查询全国地铁线路
HDU - 1501 Zipper(记忆化深搜)
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline