Python类继承简单实现
代码:
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-com06-vm3.db01.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类继承简单实现
版权所有: 本文系米扑博客原创、转载、摘录,或修订后发表,最后更新于 2010-10-27 17:04:31
侵权处理: 本个人博客,不盈利,若侵犯了您的作品权,请联系博主删除,莫恶意,索钱财,感谢!
转载注明: Python类继承简单实现 (米扑博客)