python笔记之面向对象编程

本文介绍了Python中类的方法和对象调用的概念,包括类变量和对象变量的区别,以及如何通过`self`变量来实现与类的联系。此外,文章还详细解释了Python中的继承机制,通过例子展示了如何创建子类并重用父类的功能。
一、类的方法和对象调用 
一般类中方法前初始的变量称为 类的变量,在类的方法中初始的变量为 对象的变量 
定义方法时用self变量作为类的默认属性,所有方法的其他变量(对象变量)赋值通过class.self传递为与类联系的链条。用户调用时通过 class.(对象变量的赋值).(方法)<除self默认外的参数> 
按照A Byte of Python的例子再写一遍应该可以看懂每行的语句含义: 
Python代码   收藏代码
  1. #!/usr/bin/python  
  2. # -*- coding:gb2312 -*- #必须在第一行或者第二行  
  3. #Filename:objvar.py  
  4.   
  5.   
  6. class Person:  
  7.     '''''Represents a person'''   
  8.     population = 0     #初始化类变量  
  9.   
  10.     def __init__(self,name):  
  11.         '''''Initializes the person's data'''  
  12.         self.name = name     #定义__init__方法self.name域  
  13.         print 'Initializing %s' % self.name  
  14.         Person.population += 1  
  15.   
  16.     def __del__(self):    #不需要参数的方法仍要定义 self参数  
  17.         '''''I am dying'''  
  18.         print '%s says bye' % self.name  
  19.   
  20.         Person.population -= 1  
  21.   
  22.         if Person.population == 0:  
  23.             print 'I am the last one'  
  24.         else:  
  25.             print 'There are still %d people left.' % Person.population  
  26.       
  27.     def sayHi(self):  
  28.         '''''Creating by the person. 
  29.  
  30.         Really,that's all it does.'''  
  31.         print 'Hi,my name is %s' % self.name  
  32.   
  33.     def howMany(self):  
  34.         '''''Prints the current population.'''  
  35.         if Person.population == 1:  
  36.             print 'I am the only one person here.'  
  37.         else:  
  38.             print 'We have %d person here.' % Person.population  
  39.   
  40. songy = Person('SongYang')     
  41. songy.sayHi()   # Person('SongYang').sayHi() SongYang实例的sayHi方法调用  
  42. songy.howMany()  
  43.   
  44. swaroop = Person('Swaroop')  
  45. swaroop.sayHi()  
  46. swaroop.howMany()  
  47.   
  48. songy.sayHi()  
  49. songy.howMany()  

从上例也可看出,类的不同实例(对象)之间,都有自己在域上的不用拷贝,他们之间是不相通的。 


二、继承 


A Byte of Python的例子,重抄了一遍,加了些注释 
Python代码   收藏代码
  1. #!/usr/bin/python  
  2. # -*- coding:gb2312 -*-  
  3. # Filename: inherit.py  
  4.   
  5. class SchoolMember:  
  6.     '''''Represents any scholl member.'''  
  7.     def __init__(self,name,age):   #定义SchoolMember类的2个参数  
  8.         self.name = name  
  9.         self.age = age  
  10.         print 'Initialized SchoolMember:%s' % self.name  
  11.       
  12.     def tell(self):           
  13.         '''''Tell details.'''  
  14.         print 'Name:"%s",Age:"%s"' % (self.name,self.age), #别小瞧了末尾的这个逗号  
  15.   
  16. class Teacher(SchoolMember):  
  17.     '''''Represents a teacher.'''  
  18.     def __init__(self,name,age,salary):  #Teacher类的3个属性  
  19.         SchoolMember.__init__(self,name,age)  #从SchoolMember继承2个属性,终于知道原来这就是继承:)  
  20.         self.salary = salary     #别忘了定义其他参数  
  21.         print 'Initialized Teacher:%s' % self.name  
  22.   
  23.     def tell(self):  
  24.         SchoolMember.tell(self)  
  25.         print 'Salary:"%d"' % self.salary  
  26.   
  27. class Student(SchoolMember):  
  28.     '''''Represents a student.'''  
  29.     def __init__(self,name,age,marks):  
  30.         SchoolMember.__init__(self,name,age)  
  31.         self.marks= marks  
  32.         print 'Initialized Student:%s' % self.name  
  33.   
  34.     def tell(self):  
  35.         SchoolMember.tell(self)  
  36.         print 'Marks:"%d"' % self.marks,  
  37.   
  38.       
  39. t = Teacher('Mr.Swaroop',26,30000# 类实例的三个属性  
  40. s = Student('Song',22,77)  
  41.   
  42. members = [t,s]  
  43. for member in members:  #喜欢python当然也少不了对for...in的青睐   
  44.     member.tell()  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值