Python实现类继承实例

时间:2021-05-22

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] print for member in members: member.tell()

运行结果:

[work@db-testing.baidu.com python]$ python inherit.py init SchoolMember: yangganginit Teacher: yangganginit SchoolMember: liminginit Student: limingname:yanggang; age:20salary: 1000name:liming; age:12marks: 86

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章