Python 面向对象视频资料分享

巴扎黑
Libérer: 2017-08-25 14:59:03
original
1541 Les gens l'ont consulté

面向对象是目前编程语言里面非常主流的一种思想。Python对于面向对象有非常好的实现。同时,借助Python灵活的语法,可以实现一些很酷的面向对象特性。

Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的。本章节我们将详细介绍Python的面向对象编程。

如果你以前没有接触过面向对象的编程语言,那你可能需要先了解一些面向对象语言的一些基本特征,在头脑里头形成一个基本的面向对象的概念,这样有助于你更容易的学习Python的面向对象编程。

接下来我们先来简单的了解下面向对象的一些基本特征。

58a5098728713515.jpg

面向对象难学的不是概念等知识点,而是面向对象的思维方式:

面向对象的基本思想是封装,继承,多态。

首先是继承:

定义一个类:

代码如下:

class Bird(object): have_feather = True way_of_reproduction = 'egg'
Copier après la connexion

调用这个类:

代码如下:

summer = Bird() print summer.way_of_reproduction
Copier après la connexion


与Java不同是,Python是不需要new来实例化类的。

同样,Python的类下面是可以定方法的:

代码如下:

class Bird(object): have_feather = True way_of_reproduction = 'egg' def say(self, word='hi hi'): print 'i say :' + word
Copier après la connexion

注意一点,所有类的函数,必须至少带有一个参数,这个参数必须是self。

类以外的函数没有这一个限制。

代码如下:

chk = Chicken() print chk.have_feather print chk.sat('hello')
Copier après la connexion

__init__()方法

__init__()是一个特殊方法(special method)。Python里会有一些特殊方法,Python会以特别的方式处理它们。特殊方法的名字的特点是前后都有两个下划线。

__init__()方法的特殊在于,如果你在类中定义了这个方法,一旦你根据这个类建立对象,Python就会自动调用这个方法(这个过程也叫初始化)。

如:

代码如下:

class happyBird(Bird): def __init__(self,more_words): print 'We are happy birds.',more_words hb = happyBird('Happy,Happy!')
Copier après la connexion

父类方法的重载:

代码如下:

class Hello(object): name = 'hello' def __init__(self): self.name='my name is hello' #类中的参数必须带有self参数 def sayhi(self): print 'hi you' class World(Hello): def __init__(self): #这里访问的是父类初始化的变量名 print 'before:',Hello.name super(World,self).__init__() #由于调用了父类的初始化构造函数,继承了父类的变量的改变 print 'after:',self.name #近似于方法重载 def sayhi(self,word='baby'): #调用父类sayhi方法 super(World,self).sayhi() print 'hi '+word def sayWorld(self): print 'hi,hello world' if __name__ == '__main__': c = World() c.sayhi() c.sayWorld()
Copier après la connexion

另外,python是允许多继承的,但是这个是个非常危险的操作,建议不要随便使用。

关于Python的多态,就像JavaScript一样,直接访问对象的属性,不需要使用接口,没有类型转换。

对于类型的判断,有抓们的type()函数,和isinstance()函数判断是否某个函数的子类。

复制代码代码如下:

isinstance(object, classinfo)


判断实例是否是这个类或者object是变量

classinfo 是类型(tuple,dict,int,float)
判断变量是否是这个类型

代码如下:

class objA: pass A = objA() B = 'a','v' C = 'a string' print isinstance(A, objA) print isinstance(B, tuple) print isinstance(C, basestring)
Copier après la connexion

输出结果:
True
True
True

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!