如果你没有任何以往的经验与面向对象(OO)编程,你可能想咨询或至少某种教程的入门课程,所以,你必须掌握的基本概念.

然而,这里是小推出的面向对象编程(OOP)给你带来更好的学习速度:

OOP的术语概述

类: 用户定义的一个对象,它定义了一套类的任何对象的属性特点的原型。属性数据成员(类变量和实例变量)和方法,通过点符号访问.

类变量: 一个类的所有实例共享变量。类变量被定义在一个类以外的任何类的方法。不作为类变量经常作为实例变量.

数据成员:A类变量或实例变量持有一个类和它的对象的相关数据.

函数重载: 分配到一个特定功能的多个行为。执行的操作,因涉及的对象(参数)的类型。

实例变量: 一个方法内定义的变量只属于一类的当前实例.

继承 : 传递类的特点是从它派生的其他类.

实例: 某个类的一个单独的对象。例如,一个对象obj属于类Circle,是类Circle的一个实例.

实例化 : 创建一个类的实例.

方法 : 一个是定义在类定义中的一种特殊功能.

对象: 这是由它的类定义一个数据结构的一个唯一的实例。对象包括数据成员(类变量和实例变量)和方法.

运算符重载: 多个函数分配到一个特定的运算符.

创建类:

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

class ClassName:

'Optional class documentation string'

class_suite

类有一个文档可以是通过ClassName.__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中添加自参数到你的列表,当你调用的方法,你不需要包括它.

创建实例对象:

要创建一个类的实例,调用类,使用类的名称,并通过在其__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]) : 访问对象的属性.

The hasattr(obj,name) : 检查是否存在一个属性.

The setattr(obj,name,value) : 设置一个属性。如果属性不存在,然后将它创建.

The 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__ : 如果未定义类的文档字符串,则没有.

__name__: 类名.

__module__: 在类定义的模块名称。此属性是“__main__”的交互模式.

__bases__ : 一个基类包含在其发生在基类列表中的顺序可能是空的元组,.

对于上面的类,让我们尝试访问所有这些属性:

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':, 'empCount': 2,

'displayEmployee':,

'__doc__': 'Common base class for all employees',

'__init__':}

销毁对象(垃圾收集):

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

Python的垃圾收集器运行程序执行过程中被触发时,一个对象的引用计数达到零。别名对象的引用计数的变化,指出它的变化:

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

a = 40 # Create objectb = a # Increase ref. count ofc = [b] # Increase ref. count ofdel a # Decrease ref. count ofb = 100 # Decrease ref. count ofc[0] = -1 # Decrease ref. count of

你通常不会注意到,当垃圾收集器会破坏一个孤立的实例,并回收其空间。但是,一个类可以实现特殊方法__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) 布尔函数返回true,如果给定的子类subis的确是一个超类的子类sup.

isinstance(obj, Class) 布尔函数返回true,如果obj是一个Class类的实例,或者是一类的子类的一个实例.

重写方法:

你总是可以覆盖父类方法。覆盖父方法的原因之一是因为你可能想在你的子类特殊或不同的功能.

例如:

#!/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

相应重载方法:

下表列出了一些通用的功能,你可以在自己的类覆盖:

SN

方法、描述和简单调用

1

__init__ ( self [,args...] )

Constructor (with any optional arguments)

Sample Call : obj = className(args)

2

__del__( self )

Destructor, deletes an object

Sample Call : dell obj

3

__repr__( self )

Evaluatable string representation

Sample Call : repr(obj)

4

__str__( self )

Printable string representation

Sample Call : str(obj)

5

__cmp__ ( self, x )

Object comparison

Sample Call : cmp(obj, x)

重载操作符:

假设你已经创建了一个Vector类来表示二维向量。当您使用加运算,将它们添加,会发生什么?最有可能的Python会骂你.

但是,你可以在类中定义__ 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)

Data Hiding:

An object's attributes may or may not be visible outside the class definition. For these cases, you can name attributes with a double underscore prefix, and those attributes will not be directly visible to outsiders:

Example:

#!/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

This would produce following result:

1

2

Traceback (most recent call last):

File "test.py", line 12, inprint counter.__secretCountAttributeError: JustCounter instance has no attribute '__secretCount'

Python protects those members by internally changing the name to include the class name. You can access such attributes as object._className__attrName.

If you would replace your last line as following, then it would work for you:

.........................

print counter._JustCounter__secretCount

This would produce following result:

1

2

2

python对象编程例子-Python 面向对象编程实例讲解相关推荐

  1. python归一化 增大差异_简学Python第六章__class面向对象编程与异常处理

    Python第六章__class面向对象编程与异常处理 欢迎加入Linux_Python学习群 群号:478616847 目录: 面向对象的程序设计 类和对象 封装 继承与派生 多态与多态性 特性pr ...

  2. python浓缩(13)面向对象编程

    为什么80%的码农都做不了架构师?>>>    本章主题 ? 引言 ? 面向对象编程 ? 类 ? 实例 ? 绑定与方法调用 ? 子类,派生和继承 ? 内建函数 ? 定制类 ? 私有性 ...

  3. Python学习笔记:16 面向对象编程入门

    文章目录 类和对象 面向对象的过程 定义类 创建和给对象发消息 打印对象 面向对象编程的支柱 经典案例 例子1:定义一个类描述数字时钟,可以显示时/分/秒,可以运转(走字) 例子2:扑克游戏:四个玩家 ...

  4. Python基础十五:面向对象编程四:高级特性

    Python基础十五:面向对象编程四:高级特性 Python基础系列内容为学习廖雪峰老师Python3教程的记录,廖雪峰老师官网地址:廖雪峰Python3教程 Author:yooongchun Em ...

  5. Python开发系列课程(11) - 面向对象编程进阶

    面向对象编程进阶 在前面的章节我们已经了解了面向对象的入门知识,知道了如何定义类,如何创建对象以及如何给对象发消息.为了能够更好的使用面向对象编程思想进行程序开发,我们还需要对Python中的面向对象 ...

  6. Python开发系列课程(9) - 面向对象编程基础

    面向对象编程基础 活在当下的程序员应该都听过"面向对象编程"一词,也经常有人问能不能用一句话解释下什么是"面向对象编程",我们先来看看比较正式的说法. 把一组数 ...

  7. 第八章:对象、类与面向对象编程

    第八章:对象.类与面向对象编程 8.1 理解对象 new一个对象的时候发生了什么 8.1.1 属性的类型 通过两个中括号访问内部特性 1. 数据属性 数据属性包含一个保存数据值的位置 有 4 个特性描 ...

  8. python3 next_对Python 3.2 迭代器的next函数实例讲解

    在python中,使用iter函数可以获得有序聚合类型的迭代器,我个人将迭代器理解为带有next指针的单向链表,获取到的迭代器为链表的表头,表头内容为空,next指针指向有序聚合类型的第一个元素.在访 ...

  9. python list查找元素下标,python 获取list特定元素下标的实例讲解

    在平时开发过程中,经常遇到需要在数据中获取特定的元素的信息,如到达目的地最近的车站,橱窗里面最贵的物品等等.怎么办?看下面 方法一:利用数组自身的特性 a.index(target), 其中a是你的目 ...

  10. python数组随机打乱_对Python random模块打乱数组顺序的实例讲解

    在我们使用一些数据的过程中,我们想要打乱数组内数据的顺序但不改变数据本身,可以通过改变索引值来实现,也就是将索引值重新随机排列,然后生成新的数组.功能主要由python中random模块的sample ...

最新文章

  1. Spring Boot集成Sharding-jdbc + Mybatis-Plus实现分库分表
  2. 在IIS上部署基于django WEB框架的python网站应用
  3. python爬虫技术可以干什么-Python除了爬虫,还能干啥?
  4. 强化学习总结(1-2)——model-base(policy evaluation;policy control)
  5. OC2_点语法(属性关键字)
  6. 人工智能时代来临,还需要那么多人吗?
  7. 544. Top k Largest Numbers【medium】
  8. golang之strings
  9. 通读5G 半导体产业有新商机
  10. 二阶系统级联_二阶系统时域特性.ppt
  11. 样本均值的特征与分布
  12. Flask学习(二)-Jinji2模板引擎
  13. Ubuntu下修改只读文件方法
  14. jquery.uploadify php,jquery插件uploadify使用详解
  15. 【p4】perforce命令笔记
  16. JAVA的诞生及版本
  17. html 输入框变红色,为什么CAD的动态输入框变成红色?
  18. python中的时间处理模块(二):datetime模块之timedelta类详解
  19. 风雨沧桑50年:中国卫星通信的发展历程(上)
  20. 诚之和:元宇宙的“诸神之战” 风口还是风险

热门文章

  1. 【財務会計】固定資産の除却と廃棄の違い
  2. Jersey Restful部署到Tomcat注意事项
  3. East Central North America Region 2015
  4. .Net 基础new关键字的用法
  5. 【Android】Android 设置Activity窗体 不显示标题和全屏显示
  6. 基础中的基础。CANVAS step01
  7. AutoCAD 命令参考手册
  8. jsonproperty注解_Jackson注解详解
  9. python3.8.2安装教程-在服务器上安装python3.8.2环境的教程详解
  10. 自学python要看哪些书籍-学习Python编程的最好的7本书