Python面向对象之类和实例用法分析
本文实例讲述了Python面向对象之类和实例用法。分享给大家供大家参考,具体如下:
类
虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程。至于何为面向对象,在此就不详说了。面向对象程序设计本身就很值得深入学习,如要了解,请参阅网上其他的资料。
面向对象最重要的概念就是类(Class)和实例(Instance),牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同。
以Student类为例,在Python中,定义类是通过class关键字:
注意:Python2.X需要在类名后面加(object) 这边pass语句表示空语句段。
class后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着是(object),表示该类是从哪个类继承下来的,继承的概念我们后面再讲,通常,如果没有合适的继承类,就使用object类,这是所有类最终都会继承的类。
classStudent(object): pass
Python3.X则只需要类名,不需要加(object)
classStudent: pass
实例
创建实例是通过类名+()实现的(若__init__无或仅有self);如定义好了Student类,就可以根据Student类创建出Student的实例,如下:
classStudent(object): pass May=Student()#新建May实例 print(May)
运行结果:
<__main__.Studentobjectat0x0000000001DCC400>
可以看到,变量May指向的就是一个Student的object,后面的0x006C4770是内存地址,每个object的地址都不一样,而Student本身则是一个类。
可以自由地给一个实例变量绑定属性,比如,给实例May绑定一个name属性,这个name属性是实例May特有的,其他新建的实例是没有name属性的
classStudent(object): pass May=Student()#新建May实例 print(May) May.name="May"#给实例May绑定name属性为"May" print(May.name) Peter=Student()#新建Peter实例 #print(Peter.name)#报错,因为Peter没有Name属性
那么,如果我们需要类必须绑定属性,那如何定义呢? 请参见下文。
__init__构造函数
由于类可以起到模板的作用,因此,可以在创建实例的时候,把一些我们认为必须绑定的属性强制填写进去。(注意__init__双下划线)
如对于Student类,我们定义name和score属性(所有Sudent都须有的属性):
__init__方法的第一个参数永远是self,表示创建的实例本身,因此,在__init__方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。
有了__init__方法,在创建实例的时候,就不能传入空的参数了,必须传入与__init__方法匹配的参数,但self不需要传,Python解释器自己会把实例变量传进去:
classStudent(object): def__init__(self,name,score): self.name=name self.score=score May=Student("May",90)#须要提供两个属性 Peter=Student("Peter",85) print(May.name,May.score) print(Peter.name,Peter.score)
__del__析构函数
Justlikethe__init__method,thereisanotherspecialmethod__del__whichiscalledwhenanobjectisgoingtodiei.e.itisnolongerbeingusedandisbeingreturnedtothecomputersystemforreusingthatpieceofmemory.
The__del__methodisrunwhentheobjectisnolongerinuseandthereisnoguaranteewhenthatmethodwillberun.Ifyouwanttoexplicitlyseeitinaction,wehavetousethedelstatementwhichiswhatwehavedonehere.
相对于构造函数Python也有类似C++中的析构函数__del__,Python的垃圾回收过程与常用语言的不一样,如果一定需要,最好需要使用del语句来激活。
私有变量
NoteforC++/Java/C#Programmers
Allclassmembers(includingthedatamembers)arepublicandallthemethodsarevirtualinPython.
Oneexception:Ifyouusedatamemberswithnamesusingthedoubleunderscoreprefixsuchas__privatevar,Pythonusesname-manglingtoeffectivelymakeitaprivatevariable.
Thus,theconventionfollowedisthatanyvariablethatistobeusedonlywithintheclassorobjectshouldbeginwithanunderscoreandallothernamesarepublicandcanbeusedbyotherclasses/objects.RememberthatthisisonlyaconventionandisnotenforcedbyPython(exceptforthedoubleunderscoreprefix).
Python中定义私有变量,命名规则为前缀加两个下划线“__”,注意不可前后都包含__XXX__(该命名表示为类属性或内建变量);还有一种命名为单下划线_XXX,表示约定俗成不可访问的变量。
classPerson(object): def__init__(self,name,sex): self.name=name self.__sex=sex#sex定义为私有变量 defprint_title(self): ifself.sex=="male": print("man") elifself.sex=="female": print("woman") May=Person("May","female") print(May.name) print(May.__sex)#会报错
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python面向对象程序设计入门与进阶教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。