python面向对象编程小结

简介:

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

一、类基础

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:&nbsp;&nbsp;   
  2. ...&nbsp;&nbsp;&nbsp;&nbsp; __author = ''&nbsp;   
  3. ...&nbsp;&nbsp;&nbsp;&nbsp; __name = ''&nbsp;   
  4. ...&nbsp;&nbsp;&nbsp;&nbsp; __page = 0&nbsp;   
  5. ...&nbsp;&nbsp;&nbsp;&nbsp; price = 0&nbsp;   
  6. ...&nbsp;&nbsp;   
  7. >>> a = book()&nbsp;&nbsp;   
  8. >>> a.__author&nbsp;&nbsp;   
  9. Traceback (most recent call last):&nbsp;&nbsp;   
  10. &nbsp; File "<stdin>", line 1in <module>&nbsp;&nbsp;   
  11. AttributeError: book instance has no attribute '__author'&nbsp;   
  12. >>> a.price&nbsp;&nbsp;   
  13. 0&nbsp;   
  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. 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)多重继承

 

 
  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关键字重载父类的方法。如果重载父类中的方法,但又需要

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

 

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

 

本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/382766,如需转载请自行联系原作者

相关文章
|
1月前
|
Python
python-面向对象
python-面向对象
15 2
|
3月前
|
Python
python 面向对象编程(2)
python 面向对象编程(2)
|
1月前
|
Python
Python中的面向对象编程与继承
本文将深入探讨Python中面向对象编程的核心概念,重点讨论继承的实现原理以及在实际开发中的应用。通过详细的示例和解释,读者将能够全面理解Python中继承的使用方式和优势,为提高代码的复用性和可维护性提供有效的技术支持。
|
17天前
|
Python
Python面向对象编程学习应用案例详解
面向对象编程在Python中通过类定义对象结构和行为。示例:1) 使用`class`关键字定义类,如`class Person`;2) `__init__`方法初始化对象属性,如`self.name`和`self.age`;3) 实例化对象,如`person1 = Person(&quot;张三&quot;, 25)`;4) 访问属性和方法,如`person1.name`;5) 定义类方法,如`def introduce(self)`;6) 调用方法,如`person1.introduce()`;7) 类继承,如`class Student(Person)`;8) 多态,通过继承重写方法实现。
9 1
|
30天前
|
Python
Python面向对象编程简介
Python面向对象编程简介
18 1
|
1月前
|
存储 机器学习/深度学习 数据安全/隐私保护
【Python 基础教程 24】全面入门Python面向对象编程:深度探索与实战教程
【Python 基础教程 24】全面入门Python面向对象编程:深度探索与实战教程
79 0
|
1月前
|
Java 程序员 数据安全/隐私保护
Python教程第6章 | Python面向对象
Python面向对象概念、类的定义和调用、类方法、修改和增加类属性、类和对象、初始化函数、类的继承、类的多态、类的访问控制
44 0
|
1月前
|
存储 Python
Python的面向对象编程(OOP)
Python的面向对象编程(OOP)
15 0
|
1月前
|
存储 Python
python面向对象编程
python面向对象编程
11 0
|
1月前
|
Python
Python中的面向对象编程:基础与实践
Python中的面向对象编程:基础与实践
11 0