首页 | 新闻 | 交流 | 问吧 | 文档 | 手册 | 下载 | 博客

python_类

作者:  时间: 2011-05-29

类的继承

1 #!/usr/bin
2 # Filename: inherit.py
3
4 ####################################################################
5 class SchoolMember:
6 '''Represents any school member'''
7 def __init__(self, name, age) :
8 self.name = name
9 self.age = age
10 print 'Initialized School member: %s' % self.name
11 def tell(self) :
12 '''Tell my details'''
13 print "Name: '%s' Age: '%s' " % (self.name,self.age)
14
15 class Teacher(SchoolMember) :
16 '''Represents a teacher'''
17 def __init__(self, name, age, salary) :
18 SchoolMember.__init__(self, name, age)
19 self.salary = salary
20 print '(Initialized Teacher: "%s")' % self.name
21 def tell(self) :
22 SchoolMember.tell(self)
23 print 'Salary: "%d"' % self.salary
24 class Student(SchoolMember) :
25 '''Represents a student'''
26 def __init__(self, name, age, marks):
27 SchoolMember.__init__(self, name, age)
28 self.marks = marks
29 print '(Initialized Student: "%s")' % self.name
30 def tell(self) :
31 SchoolMember.tell(self)
32 print 'Marks: "%d"' % self.marks
33 ######################################################################
34
35 t = Teacher('Mrs.Shrividya', 40, 30000)
36 s = Student('Swaroop', 22, 75)
37
38 print #print a blank line
39
40 members = [t,s]
41
42 for member in members :
43 member.tell() #works for both Teachers and Students