Python编程之类的继承
#-*-coding=utf-8-*-
import os
class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
print 'SchoolMember.self = %d'%id(self)
self.name = name
self.age = age
print '(Initialized SchoolMember %s)' % self.name
def tell(self):
'''Tell my details.'''
print 'Name: %-16sAge:%s' % (self.name, self.age),
#
class Teacher(SchoolMember):#继承关系的表示方法: 继承元组。在元组之中指明继承的基本类
'''Represents a teacher.'''
def __init__(self, name, age, salary):
print 'Teacher.self = %d'%id(self)
SchoolMember.__init__(self, name, age)
self.salary = salary
print '(Initialized Teacher %s)' % self.name
def tell(self):
SchoolMember.tell(self)
print 'Salary: "%d"' % self.salary
#
class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
print 'Student.self = %d'%id(self)
SchoolMember.__init__(self, name, age)
self.marks = marks
print '(Initialized Student %s)' % self.name
def tell(self):
SchoolMember.tell(self)
print 'Marks: "%d"' % self.marks
#
os.system('cls')
t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 22, 75)
print # prints a blank line
members = [t, s]
for member in members:
member.tell() # works for both Teachers and Students
'''summary:
Python是一个高度面向对象的语言。
1)self表示对象本身,在调用父类的方法传入self时,传入的还是子类对象,此时子类实例仅仅作为父类的实例
2)id(object)
Return the “identity” of an object. In CPython implementation, this is the address of the object in memory.
id(self)返回的是当前对象的内存地址
3)多重继承:类的定义时,如果在继承元组中列出一个以上的类,该类被称为多重继承。'''
-------end------