当前位置: 首页 > 图灵资讯 > 行业资讯> python子类如何重用父类功能

python子类如何重用父类功能

发布时间:2024-06-11 17:27:09

1、根据名称直接调用某一类下的函数,而不是依赖继承关系。

2、调用父类提到的方法,即严格依赖继承关系。如果你调用super,你会得到一个特殊的对象。该对象将参考发起属性搜索的类别mro,并在当前类别的父类别中找到属性。

实例

classpeople:
def__init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex




classboss(people):
def__init__(self,name,age,sex,level,salary):
#以下两行的代码效果同样相同
super(boss,self).__init__(name,age,sex)
super().__init__(name,age,sex)

self.level=level
self.salary=salary

print(boss.mro())
print('='*50)
tea_obj=boss('资本家',18,'不限',10,9000)
print(tea_obj.__dict__)

以上是python子类重用父类功能的方法,希望对大家有所帮助。更多Python学习指导:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09