python基础4--继承
代码
# file's name : inherit.py
class SchoolMember:
def __init__(self, name, age):
self.name = name
self.age = age
print "initialized school member : %s" % self.name
def tell(self):
print "Tell you my details:"
print "name : %s, age : %d" % (self.name, self.age)
class Student(SchoolMember):
"""Def a class Student, extends from SchoolMember"""
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print "initialized a student : %s" % self.name
def tell(self):
SchoolMember.tell(self)
print "marks : %d" % self.marks
print "学生的名字 : %s" % self.name
class Teacher(SchoolMember):
"""Def a class Teacher, extends from SchoolMember"""
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print "initialized a teacher : %s" % self.name
def tell(self):
SchoolMember.tell(self)
print "salary : %d" % self.salary
print "老师的名字 : %s" % self.name
t = Teacher("koma", 25, 6000)
s = Student("zhangxd", 25, 60)
for tmp in [t, s]:
tmp.tell()
class SchoolMember:
def __init__(self, name, age):
self.name = name
self.age = age
print "initialized school member : %s" % self.name
def tell(self):
print "Tell you my details:"
print "name : %s, age : %d" % (self.name, self.age)
class Student(SchoolMember):
"""Def a class Student, extends from SchoolMember"""
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print "initialized a student : %s" % self.name
def tell(self):
SchoolMember.tell(self)
print "marks : %d" % self.marks
print "学生的名字 : %s" % self.name
class Teacher(SchoolMember):
"""Def a class Teacher, extends from SchoolMember"""
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print "initialized a teacher : %s" % self.name
def tell(self):
SchoolMember.tell(self)
print "salary : %d" % self.salary
print "老师的名字 : %s" % self.name
t = Teacher("koma", 25, 6000)
s = Student("zhangxd", 25, 60)
for tmp in [t, s]:
tmp.tell()
运行结果:
initialized school member : koma
initialized a teacher : koma
initialized school member : zhangxd
initialized a student : zhangxd
Tell you my details:
name : koma, age : 25
salary : 6000
老师的名字 : koma
Tell you my details:
name : zhangxd, age : 25
marks : 80
学生的名字 : zhangxd