Python类的继承


01
#!/usr/bin/python
02
# Filename: inherit.py
03
 
04
class SchoolMember:
05
    '''Represents any school member.'''
06
    def __init__(self, name, age):
07
        self.name = name
08
        self.age = age
09
        print'(Initialized SchoolMember: %s)'% self.name
10
 
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
 
22
    def tell(self):
23
        SchoolMember.tell(self)
24
        print'Salary: "%d"'% self.salary
25
 
26
class Student(SchoolMember):
27
    '''Represents a student.'''
28
    def __init__(self, name, age, marks):
29
        SchoolMember.__init__(self, name, age)
30
        self.marks = marks
31
        print'(Initialized Student: %s)'% self.name
32
 
33
    def tell(self):
34
        SchoolMember.tell(self)
35
        print'Marks: "%d"'% self.marks
36
 
37
t = Teacher('Mrs. Shrividya',40,30000)
38
s = Student('Swaroop',22,75)
39
members = [t, s]
40
for member in members:
41
    member.tell()# works for both Teachers and Students
作者:Mage001

相关内容

    暂无相关文章

评论关闭