最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python类成员继承重写如何实现 Python类成员继承重写实现代码
时间:2020-09-16 编辑:袖梨 来源:一聚教程网
Python类成员继承重写如何实现?本篇文章小编给大家分享一下Python类成员继承重写实现代码,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
类成员的继承和重写
成员继承:子类继承了父类除构造方法外的所有成员
方法重写:子类可以重新定义父类中的方法,这样就会覆盖父类中的方法,也称为重写
代码如下
class Person: def __init__(self,name,age): self.name = name self.__age = age def say_age(self): print('我的年龄:',self.__age) def say_introduce(self): print('我的名字是{0}'.format(self.name)) class Student(Person): def __init__(self,name,age,score): Person.__init__(self,name,age) self.score = score def say_introduce(self): print('不是,我的名字叫做{0}'.format(self.name)) s = Student('Xujie',18,70) s.say_age() s.say_introduce()
结果
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27