python面向对象编程小结

参考:http://sucre.iteye.com/blog/680101

 

 

一、类基础

1、类的定义

class <类名>:

    <其他语句>

class <类名>(父类名):

    <其他语句>

Python代码    收藏代码
  1. >>> class human:  
  2. ...     age=0  
  3. ...     sex=''  
  4. ...     name = ''  
  5. ...  
  6. >>> class student(human):  
  7. ...     school = ''  
  8. ...     number = 0  
  9. ...     grade = 0  
  10. ...  
  11. >>>   

 

2、类的使用

如果直接使用类名修改其属性,那么将影响已经实例化的对象。

 

Python代码    收藏代码
  1. >>> class A:  
  2. ...     name = 'A'  
  3. ...     num = 2  
  4. ...  
  5. >>> A.name  
  6. 'A'  
  7. >>> a = A()       #实例化a对象  
  8. >>> a.name  
  9. 'A'  
  10. >>> A.name = 'B'  
  11. >>> A.name  
  12. 'B'  
  13. >>> a.name  
  14. 'B'  
  15. >>>   

 

二、类的属性和方法

1、类的属性:

如果类的属性是以两条下划线开始则该属性为类的私有属性,不能在类外部被访问。

私有属性的命名形式: __privateAttrs

 

如果在类内部的方法中使用类的私有属性,则应该以如下方式调用。

self.__privateAttrs

Python代码    收藏代码
  1. >>> class book:     
  2. ...     __author = ''    
  3. ...     __name = ''    
  4. ...     __page = 0    
  5. ...     price = 0    
  6. ...     
  7. >>> a = book()     
  8. >>> a.__author     
  9. Traceback (most recent call last):     
  10.   File "<stdin>", line 1in <module>     
  11. AttributeError: book instance has no attribute '__author'    
  12. >>> a.price     
  13. 0    
  14. >>>   

 
2、类的方法

在类的内部使用def关键字可以为类定义一个方法。与函数定义不同的是,类的方法必须包含参数 'self ’ ,

且'self'必须为第一个参数。和类的私有属性命名相同,类的私有方法名也要以两条下划线开始。

Python代码    收藏代码
  1. >>> class book:  
  2. ...     __author = ''  
  3. ...     __name = ''  
  4. ...     __page = 0  
  5. ...     price = 0  
  6. ...     def show(self):  
  7. ...             print self.__author  
  8. ...             print self.__name  
  9. ...     def setname(self,name):  
  10. ...             self.__name = name  
  11. ...  
  12. >>> a = book()  
  13. >>> a.show()  
  14.   
  15.   
  16. >>> a.setname('Tom')  
  17. >>> a.show()  
  18.   
  19. Tom  
  20. >>>   

 

在python中有一类以两条下划线开始并且以两条下划线结束的类方法,称之为专有方法。

__init__  构造函数,生成对象时调用

__del__  析构函数,释放对象时调用

__add__ 加运算

__mul__  乘运算

__cmp__ 比较运算

__repr__ 打印、转换

__setitem__ 按照索引赋值

__getitem__ 按照索引获取值

__len__ 获得长度

__call__ 函数调用

Python代码    收藏代码
  1. >>> class book:  
  2. ...     __author = ''  
  3. ...     __name = ''  
  4. ...     __page = ''  
  5. ...     price = 0  
  6. ...     def __check(self,item):  
  7. ...             if item == '':  
  8. ...                     return 0  
  9. ...             else:  
  10. ...                     return 1  
  11. ...     def show(self):  
  12. ...             if self.__check(self.__author):  
  13. ...                     print self.__author  
  14. ...             else:  
  15. ...                     print 'No value'  
  16. ...             if self.__check(self.__name):  
  17. ...                     print self.__name  
  18. ...             else:  
  19. ...                     print 'No value'  
  20. ...     def setname(self,name):  
  21. ...             self.__name = name  
  22. ...     def __init__(self,author,name):  
  23. ...             self.__author = author  
  24. ...             self.__name = name  
  25. ...   

 三、类的继承

1)单继承

Python代码    收藏代码
  1. >>> class parent:  
  2. ...     __a = ''  
  3. ...     __b = 0  
  4. ...     def __init__(self,a,b):  
  5. ...             self.__a = a  
  6. ...             self.__b = b  
  7. ...     def show(self):  
  8. ...             print self.__a  
  9. ...             print self.__b  
  10. ...  
  11. >>> a = parent('a',2)  
  12. >>> a.show()  
  13. a  
  14. 2  
  15. >>> class child(parent):  
  16. ...     __c = ''  
  17. ...     __d = 4  
  18. ...     def showinfo(self):  
  19. ...             self.show()  
  20. ...  
  21. >>> b = child('c',3)  
  22. >>> b.show()  
  23. c  
  24. 3  
  25. >>> b.showinfo()  
  26. c  
  27. 3  
  28. >>>   

 2)多重继承

Python代码    收藏代码
  1. # -*- coding:utf-8 -*-   
  2. class A:       #定义类A  
  3.  name = 'A'   
  4.  __num = 1  
  5.  def show(self):  
  6.   print self.name  
  7.   print self.__num  
  8.  def setnum(self,num):  
  9.   self.__num = num  
  10. class B:        #定义类B  
  11.  nameb = 'B'  
  12.  __numb = 2  
  13.  def show(self):  
  14.   print self.nameb  
  15.   print self.__numb  
  16.  def setname(self,name):  
  17.   self.__name = name  
  18. class C(A,B):  
  19.  def showall(self):  
  20.   print self.name  
  21.   print self.nameb  
  22.  show = B.show      #在这里表明show方法为B类的show方法,后来修改加上的  
  23.   
  24. >>> import jicheng  
  25. >>> a = jicheng.A()  
  26. >>> a.show()  
  27. A  
  28. 1  
  29. >>> c = jicheng.C()  
  30. >>> c.showall()  
  31. A  
  32. B  
  33. >>> c.show()  #默认调用A类的show方法  
  34. A  
  35. 1  
  36. >>> reload(jicheng)   #修改jicheng.py后重新加载  
  37. <module 'jicheng' from 'jicheng.py'>  
  38. >>> d =jicheng.C()  
  39. >>> d.show()  
  40. B  
  41. 2  
  42. >>>   

 五)重载

1)方法的重载实际上就是在类中使用def关键字重载父类的方法。如果重载父类中的方法,但又需要

在类中使用父类的该方法,可以使用父类名加‘ .’加方法名的形式调用。

Python代码    收藏代码
  1. # -*- coding:utf-8 -*-     
  2. class Mylist:     
  3.     __mylist = []     
  4.     def __init__(self,*args):          
  5.         self.__mylist = []     
  6.         for arg in args:     
  7.             self.__mylist.append(arg)      
  8.     def __add__(self,n):            #重载‘+’运算符     
  9.         for i in range(0, len(self.__mylist)):     
  10.             self.__mylist[i] = self.__mylist[i] + n      
  11.     def show(self):     
  12.         print self.__mylist     
  13.     
  14.     
  15. >>> import chongzai     
  16. >>> L = chongzai.Mylist(1,2,3,4,5)     
  17. >>> L.show()     
  18. [12345]     
  19. >>> L + 2    
  20. >>> L.show()     
  21. [34567]     
  22. >>>    

猜你喜欢

转载自windshg.iteye.com/blog/1533660
今日推荐