iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python面向对象编程小结
  • 383
分享到

python面向对象编程小结

小结面向对象python 2023-01-31 05:01:36 383人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

这个是跟着教程一步一步走过来的,所以记下自己学习的过程。 一、类基础 1、类的定义 class <类名>:     <其他语句> class <类名>(父类名):     <其他语句> >

这个是跟着教程一步一步走过来的,所以记下自己学习的过程。

一、类基础

1、类的定义

class <类名>:

    <其他语句>

class <类名>(父类名):

    <其他语句>

  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、类的使用

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

  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

  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 1, in <module>     
  11. AttributeError: book instance has no attribute '__author'    
  12. >>> a.price     
  13. 0    
  14. >>>  

2、类的方法

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

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

  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__ 函数调用

  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)单继承

 

  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. >>> class child(parent):  
  15. ...     __c = '' 
  16. ...     __d = 4 
  17. ...     def showinfo(self):  
  18. ...             self.show()  
  19. ...  
  20. >>> b = child('c',3)  
  21. >>> b.show()  
  22. c  
  23. >>> b.showinfo()  
  24. c  
  25. >>>  

 2)多重继承

 

  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. >>> c = jicheng.C()  
  29. >>> c.showall()  
  30. A  
  31. B  
  32. >>> c.show()  #默认调用A类的show方法  
  33. A  
  34. >>> reload(jicheng)   #修改jicheng.py后重新加载  
  35. <module 'jicheng' from 'jicheng.py'>  
  36. >>> d =jicheng.C()  
  37. >>> d.show()  
  38. B  
  39. >>>  

 五)重载

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

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

 

  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. [1, 2, 3, 4, 5]     
  19. >>> L + 2    
  20. >>> L.show()     
  21. [3, 4, 5, 6, 7]     
  22. >>>   

 

--结束END--

本文标题: python面向对象编程小结

本文链接: https://www.lsjlt.com/news/189223.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作