深入讲解Python中面向对象编程的相关知识


Posted in Python onMay 25, 2015

 Python从第一天开始就是面向对象的语言。正因为如此,创建和使用类和对象是非常地容易。本章将帮助您在使用Python面向对象编程的技术方面所有提高。

如果没有任何以往面向对象(OO)的编程的经验,那么可能要了解一些基本的入门课程就可以了,或者至少某种形式的教程,让你有了解基本概念。

但是,这里会比较少地介绍面向对象编程(OOP):
OOP术语概述

  •     类: 用户定义的原型对象,它定义了一套描述类的任何对象的属性。属性是数据成员(类变量和实例变量)和方法,通过点符号访问。
  •     类变量:这是一个类的所有实例共享的变量。类变量在类,但外面的任何类的方法定义。类变量不被用作经常作为实例变量。
  •     数据成员:保存与类和对象关联的数据的类变量或实例变量。
  •     函数重载:一个以上的行为特定功能的分配。执行的操作所涉及的对象(自变量)的类型不同而不同。
  •     实例变量:所定义的方法内,只属于一个类的当前实例的变量。
  •     继承:类的特点,即都是由它派生其他类的转移。
  •     实例:某一类的一个单独对象。属于类Circle一个obj对象,例如,是类Circle的一个实例。
  •     实例化:创建一个类的实例。
  •     Method : 一种特殊的函数,函数在类定义中定义。
  •     对象:这是由它的类中定义的数据结构的唯一实例。一个对象包括两个数据成员(类变量和实例变量)和方法。
  •     运算符重载:一个以上的函数功能,特定的操作符分配。

创建类:

类语句将创建一个新的类定义。类的名称紧跟在关键字class后跟一个冒号,如下所示:

class ClassName:
  'Optional class documentation string'
  class_suite

    类有一个文档字符串,它可以通过类名.__ doc__访问。

    class_suite由所有定义的类成员,数据属性与函数组件的语句。

例子

下面是一个简单的Python类的例子:

class Employee:
  'Common base class for all employees'
  empCount = 0

  def __init__(self, name, salary):
   self.name = name
   self.salary = salary
   Employee.empCount += 1
  
  def displayCount(self):
   print "Total Employee %d" % Employee.empCount

  def displayEmployee(self):
   print "Name : ", self.name, ", Salary: ", self.salary

    empCount是一个类变量,其值将是这个类的所有实例共享。这可以从类中或外部进行访问,访问形式为 Employee.empCount。

    第一个方法__init__()是一种特殊的方法,这就是所谓的类构造函数或当创建该类的一个新实例Python调用的初始化方法。

    声明就像正常函数中一样,不同的是第一个参数到每个方法是类的方法。 Python增加了self参数列表;不需要把调用的方法都它列入。

创建实例对象:

要创建一个类的实例,调用类名并传递任何参数给__init__方法接收。

"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)

访问属性:

可以访问使用点运算符来访问对象的属性。而类变量使用类名来访问,如下所示:

emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount

现在,把所有的概念放在一起:

#!/usr/bin/python

class Employee:
  'Common base class for all employees'
  empCount = 0

  def __init__(self, name, salary):
   self.name = name
   self.salary = salary
   Employee.empCount += 1
  
  def displayCount(self):
   print "Total Employee %d" % Employee.empCount

  def displayEmployee(self):
   print "Name : ", self.name, ", Salary: ", self.salary

"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount

当执行上面的代码,产生以下结果:

Name : Zara ,Salary: 2000
Name : Manni ,Salary: 5000
Total Employee 2

在任何时候可以添加,删除或修改类和对象的属性:

emp1.age = 7 # Add an 'age' attribute.
emp1.age = 8 # Modify 'age' attribute.
del emp1.age # Delete 'age' attribute.

除了使用正常的语句来访问属性,可以使用以下函数:

  •     getattr(obj, name[, default]) : 访问对象的属性。
  •     hasattr(obj,name) : 检查一个属性是否存在。
  •     setattr(obj,name,value) : 设置一个属性。如果属性不存在,那么它将被创建。
  •     delattr(obj, name) : 要删除一个属性。
hasattr(emp1, 'age')  # Returns true if 'age' attribute exists
getattr(emp1, 'age')  # Returns value of 'age' attribute
setattr(emp1, 'age', 8) # Set attribute 'age' at 8
delattr(empl, 'age')  # Delete attribute 'age'

内置的类属性:

每个Python类会继续并带有内置属性,他们可以使用点运算符像任何其他属性一样来访问:

  •     __dict__ : 字典包含类的命名空间。
  •     __doc__ : 类的文档字符串,或None如果没有定义。
  •     __name__: 类名称。
  •     __module__: 在类中定义的模块名称。此属性是在交互模式其值为“__main__”。
  •     __bases__ : 一个可能是空的元组包含了基类,其在基类列表出现的顺序。

对于上面的类,尝试访问这些属性:

#!/usr/bin/python

class Employee:
  'Common base class for all employees'
  empCount = 0

  def __init__(self, name, salary):
   self.name = name
   self.salary = salary
   Employee.empCount += 1
  
  def displayCount(self):
   print "Total Employee %d" % Employee.empCount

  def displayEmployee(self):
   print "Name : ", self.name, ", Salary: ", self.salary

print "Employee.__doc__:", Employee.__doc__
print "Employee.__name__:", Employee.__name__
print "Employee.__module__:", Employee.__module__
print "Employee.__bases__:", Employee.__bases__
print "Employee.__dict__:", Employee.__dict__

当执行上面的代码,产生以下结果:

Employee.__doc__: Common base class for all employees
Employee.__name__: Employee
Employee.__module__: __main__
Employee.__bases__: ()
Employee.__dict__: {'__module__': '__main__', 'displayCount':
<function displayCount at 0xb7c84994>, 'empCount': 2, 
'displayEmployee': <function displayEmployee at 0xb7c8441c>, 
'__doc__': 'Common base class for all employees', 
'__init__': <function __init__ at 0xb7c846bc>}

销毁对象(垃圾回收):

Python的删除不需要的对象(内建类型或类的实例),自动释放内存空间。由Python定期回收的内存块不再使用的过程被称为垃圾收集。

Python的垃圾回收器在程序执行过程中运行,当一个对象的引用计数为零时触发。一个对象的引用计数改变为指向它改变别名的数量。

当它分配一个新的名字或放置在容器(列表,元组或字典)的对象的引用计数增加。当对象的引用计数减少使用 del 删除,其基准被重新分配,或者它的引用超出范围。当一个对象的引用计数变为零,Python会自动地收集它。

a = 40   # Create object <40>
b = a    # Increase ref. count of <40> 
c = [b]   # Increase ref. count of <40> 

del a    # Decrease ref. count of <40>
b = 100   # Decrease ref. count of <40> 
c[0] = -1  # Decrease ref. count of <40>

当垃圾回收器销毁孤立的实例,并回收其空间一般不会注意到。但是,一个类可以实现特殊方法__del__(),称为析构函数被调用时,该实例将被摧毁。这个方法可以用于清理所用的一个实例的任何非内存资源。
例子:

__del__()析构函数打印实例,它即将被销毁的类名:

#!/usr/bin/python

class Point:
  def __init( self, x=0, y=0):
   self.x = x
   self.y = y
  def __del__(self):
   class_name = self.__class__.__name__
   print class_name, "destroyed"

pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3

当执行上面的代码,它产生以下结果:

3083401324 3083401324 3083401324
Point destroyed

注意:理想情况下,应该定义类的单独的文件,那么应该使用import语句将其导入主程序文件。详细请查看Python- 模块章节,导入模块和类的更多细节。
类继承:

不用从头开始,可以通过上面列出的括号父类的新类名后,从一个已经存在的类派生它创建一个类。

子类继承父类的属性,可以使用父类的这些属性,就好像它们是在子类中定义的一样。子类也可以覆盖父类的数据成员和方法。
语法

派生类的声明很像它们的父类; 从基类的列表后给出类名继承:

class SubClassName (ParentClass1[, ParentClass2, ...]):
  'Optional class documentation string'
  class_suite

例子

#!/usr/bin/python

class Parent:    # define parent class
  parentAttr = 100
  def __init__(self):
   print "Calling parent constructor"

  def parentMethod(self):
   print 'Calling parent method'

  def setAttr(self, attr):
   Parent.parentAttr = attr

  def getAttr(self):
   print "Parent attribute :", Parent.parentAttr

class Child(Parent): # define child class
  def __init__(self):
   print "Calling child constructor"

  def childMethod(self):
   print 'Calling child method'

c = Child()     # instance of child
c.childMethod()   # child calls its method
c.parentMethod()   # calls parent's method
c.setAttr(200)    # again call parent's method
c.getAttr()     # again call parent's method

当执行上面的代码,产生以下结果:

Calling child constructor
Calling child method
Calling parent method
Parent attribute : 200

类似的方式,可以按如下继承多个父类的类:

class A:        # define your class A

.....
class B:         # define your calss B

.....
class C(A, B):   # subclass of A and B

.....

可以使用issubclass()或isinstance()函数来检查两个类和实例的关系。

  •     issubclass(sub, sup) 如果给定的子类子确实是超sup的子类布尔函数返回true。
  •     isinstance(obj, Class) 如果obj是Class类的实例,或者是类的一个子类的实例布尔函数返回true

重写方法:

可以覆盖父类的方法。原因之一重写父的方法,因为可能想在子类特殊或实现不同的功能。
例子

#!/usr/bin/python

class Parent:    # define parent class
  def myMethod(self):
   print 'Calling parent method'

class Child(Parent): # define child class
  def myMethod(self):
   print 'Calling child method'

c = Child()     # instance of child
c.myMethod()     # child calls overridden method

当执行上面的代码,产生以下结果:

Calling child method

基础重载方法:

下表列出了一些通用的功能,可以在类重写中:

深入讲解Python中面向对象编程的相关知识

 重载运算符:

假设要创建了一个Vector类来表示二维向量,当使用加运算符来增加他们发生了什么?最有可能是Python会?拍恪?/p>

可以,但是定义__add__方法在类中进行矢量相加,再加上操作符的行为会按预期:
例子:

#!/usr/bin/python

class Vector:
  def __init__(self, a, b):
   self.a = a
   self.b = b

  def __str__(self):
   return 'Vector (%d, %d)' % (self.a, self.b)
  
  def __add__(self,other):
   return Vector(self.a + other.a, self.b + other.b)

v1 = Vector(2,10)
v2 = Vector(5,-2)
print v1 + v2

当执行上面的代码,产生以下结果:

Vector(7,8)

数据隐藏:

对象的属性可以是或可以不在类定义外部可见。对于这些情况,可以命名以双下划线前缀属性,这些属性将无法直接让外部可视。
例子:

#!/usr/bin/python

class JustCounter:
  __secretCount = 0
 
  def count(self):
   self.__secretCount += 1
   print self.__secretCount

counter = JustCounter()
counter.count()
counter.count()
print counter.__secretCount

当执行上面的代码,产生以下结果:

1
2
Traceback (most recent call last):
 File "test.py", line 12, in <module>
  print counter.__secretCount
AttributeError: JustCounter instance has no attribute '__secretCount'

Python的保护成员通过内部更改名称以包含类名。可以访问这些属性通过object._className__attrName。如果想更换最后一行,那么它会工作如下:

.........................
print counter._JustCounter__secretCount

当执行上面的代码,产生以下结果:

1
2
2
Python 相关文章推荐
Python的词法分析与语法分析
May 18 Python
Python的Django框架中的select_related函数对QuerySet 查询的优化
Apr 01 Python
python使用psutil模块获取系统状态
Aug 27 Python
Django使用Mysql数据库已经存在的数据表方法
May 27 Python
Pycharm设置去除显示的波浪线方法
Oct 28 Python
Python3 使用cookiejar管理cookie的方法
Dec 28 Python
python重试装饰器的简单实现方法
Jan 31 Python
python实现整数的二进制循环移位
Mar 08 Python
3种适用于Python的疯狂秘密武器及原因解析
Apr 29 Python
Python使用matplotlib绘制圆形代码实例
May 27 Python
keras中的loss、optimizer、metrics用法
Jun 15 Python
Visual Studio Code搭建django项目的方法步骤
Sep 17 Python
详解详解Python中writelines()方法的使用
May 25 #Python
Python中操作文件之write()方法的使用教程
May 25 #Python
在Python中操作文件之truncate()方法的使用教程
May 25 #Python
Python中tell()方法的使用详解
May 24 #Python
在Python中操作文件之seek()方法的使用教程
May 24 #Python
简单介绍Python中的readline()方法的使用
May 24 #Python
在Python中操作文件之read()方法的使用教程
May 24 #Python
You might like
一个PHP验证码类代码分享(已封装成类)
2011/07/17 PHP
PHP简洁函数(PHP简单明了函数语法)
2012/06/10 PHP
win7+apache+php+mysql环境配置操作详解
2013/06/10 PHP
php实现的pdo公共类定义与用法示例
2017/07/19 PHP
PHP实现图片压缩
2020/09/09 PHP
JavaScript语言中的Literal Syntax特性分析
2007/03/08 Javascript
jQuery的load()方法及其回调函数用法实例
2015/03/25 Javascript
简介JavaScript中Math.cos()余弦方法的使用
2015/06/15 Javascript
BootStrap实用代码片段之一
2016/03/22 Javascript
全面解析Bootstrap中Carousel轮播的使用方法
2016/06/13 Javascript
JavaScript实现点击文本自动定位到下拉框选中操作
2016/06/15 Javascript
利用ES6语法重构React组件详解
2017/03/02 Javascript
简单实现JavaScript弹幕效果
2020/08/27 Javascript
Node.js连接mongodb实例代码
2017/06/06 Javascript
js数字滑动时钟的简单实现(示例讲解)
2017/08/14 Javascript
React-Native之定时器Timer的实现代码
2017/10/04 Javascript
ReactNative之FlatList的具体使用方法
2017/11/29 Javascript
在微信小程序中渲染HTML内容的方法示例
2018/09/28 Javascript
vue 设置 input 为不可以编辑的实现方法
2019/09/19 Javascript
django之常用命令详解
2016/06/30 Python
python3解析库BeautifulSoup4的安装配置与基本用法
2018/06/26 Python
python批量修改图片大小的方法
2018/07/24 Python
PyQt4 treewidget 选择改变颜色,并设置可编辑的方法
2019/06/17 Python
Python+OpenCV 实现图片无损旋转90°且无黑边
2019/12/12 Python
Django数据库操作之save与update的使用
2020/04/01 Python
Python实现Kerberos用户的增删改查操作
2020/12/14 Python
详解Django关于StreamingHttpResponse与FileResponse文件下载的最优方法
2021/01/07 Python
CSS3 实现的火焰动画
2020/12/07 HTML / CSS
介绍一下Python中webbrowser的用法
2013/05/07 面试题
客服专员岗位职责范本
2013/11/29 职场文书
商务英语大学生职业生涯规划书范文
2014/01/01 职场文书
单位刻章介绍信范文
2014/01/11 职场文书
草房子读书笔记
2015/06/29 职场文书
2016廉洁从政心得体会
2016/01/19 职场文书
 Redis 串行生成顺序编码的方法实现
2022/04/03 Redis
零基础学java之带返回值的方法的定义和调用
2022/04/10 Java/Android