Python实现类继承实例
作者:shichen2014 时间:2023-11-13 07:44:24
Python是一种解释型、面向对象、动态数据类型的高级程序设计语言,本文就举一例Python类继承的实例。
实例代码如下:
#! /usr/bin/python
# Filename: inherit.py
# Author: yanggang
class SchoolMember:
def __init__(self,name,age):
self.name = name
self.age = age
print 'init SchoolMember: ', self.name
def tell(self):
print 'name:%s; age:%s' % (self.name, self.age)
class Teacher(SchoolMember):
def __init__(self,name,age,salary):
SchoolMember.__init__(self,name,age)
self.salary = salary
print 'init Teacher: ', self.name
def tell(self):
SchoolMember.tell(self)
print 'salary: ', self.salary
class Student(SchoolMember):
def __init__(self,name,age,marks):
SchoolMember.__init__(self,name,age)
self.marks = marks
print 'init Student: ', self.name
def tell(self):
SchoolMember.tell(self)
print 'marks: ', self.marks
t = Teacher('yanggang', 20, 1000)
s = Student('liming', 12, 86)
members = [t,s]
for member in members:
member.tell()
运行结果:
[work@db-testing.baidu.com python]$ python inherit.py
init SchoolMember: yanggang
init Teacher: yanggang
init SchoolMember: liming
init Student: liming
name:yanggang; age:20
salary: 1000
name:liming; age:12
marks: 86
标签:Python,类继承
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python之生产者消费者模型实现详解
2021-12-03 14:37:15
Mysql 删除重复数据保留一条有效数据(最新推荐)
2024-01-20 17:08:11
![](https://img.aspxhome.com/file/2023/1/126291_0s.png)
jQuery+AJAX实现网页无刷新上传
2024-04-17 10:39:39
![](https://img.aspxhome.com/file/2023/8/136188_0s.jpg)
Python爬虫设置ip代理过程解析
2021-05-27 15:08:30
Python特效之文字成像方法详解
2021-08-09 09:34:06
![](https://img.aspxhome.com/file/2023/8/68348_0s.jpg)
优化SQL Server的内存占用之执行缓存
2012-04-13 11:45:06
python NetworkX库生成并绘制带权无向图
2023-06-09 08:56:58
![](https://img.aspxhome.com/file/2023/4/111924_0s.png)
python实现获取序列中最小的几个元素
2023-12-24 19:11:32
python使用Matplotlib绘制多种常见图形
2023-05-26 05:06:28
![](https://img.aspxhome.com/file/2023/4/101594_0s.webp)
python 列表输出重复值以及对应的角标方法
2021-06-19 13:29:57
透明数据加密(TDE)库的备份和还原
2012-07-21 14:44:08
sql中的常用的字符串处理函数大全
2024-01-19 21:37:41
python之tensorflow手把手实例讲解斑马线识别实现
2021-11-11 05:53:19
![](https://img.aspxhome.com/file/2023/0/68460_0s.png)
Python操作Word批量生成合同的实现示例
2023-06-17 09:59:14
Python利用Selenium实现自动观看学习通视频
2022-03-12 16:32:03
![](https://img.aspxhome.com/file/2023/0/81670_0s.jpg)
Python爬虫实现的根据分类爬取豆瓣电影信息功能示例
2022-10-14 04:27:51
如何列出我所需要数据视图?
2010-01-12 20:00:00
pytorch tensor计算三通道均值方式
2022-06-26 00:02:41
![](https://img.aspxhome.com/file/2023/4/87574_0s.png)
ES6入门教程之let和const命令详解
2024-05-22 10:37:07
用Pycharm实现鼠标滚轮控制字体大小的方法
2023-02-08 15:34:32
![](https://img.aspxhome.com/file/2023/2/108152_0s.jpg)