Python编程——类的继承


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------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值