python面向对象编程小结
参考:http://sucre.iteye.com/blog/680101
一、类基础
1、类的定义
class <类名>:
<其他语句>
class <类名>(父类名):
<其他语句>
Python代码
>>>classhuman:
... age=0
... sex=''
... name = ''
...
>>> classstudent(human):
... school = ''
... number = 0
... grade = 0
...
>>>
2、类的使用
如果直接使用类名修改其属性,那么将影响已经实例化的对象。
Python代码
>>>classA:
... name = 'A'
... num = 2
...
>>> A.name
'A'
>>> a = A() #实例化a对象
>>> a.name
'A'
>>> A.name = 'B'
>>> A.name
'B'
>>> a.name
'B'
>>>
二、类的属性和方法
1、类的属性:
如果类的属性是以两条下划线开始则该属性为类的私有属性,不能在类外部被访问。
私有属性的命名形式: __privateAttrs
如果在类内部的方法中使用类的私有属性,则应该以如下方式调用。
self.__privateAttrs
Python代码
>>>classbook:
... __author = ''
... __name = ''
... __page = 0
... price = 0
...
>>> a = book()
>>> a.__author
Traceback (most recent call last):
File "", line1,in
AttributeError: book instance has no attribute '__author'
>>> a.price
0
>>>
2、类的方法
在类的内部使用def关键字可以为类定义一个方法。与函数定义不同的是,类的方法必须包含参数 'self ’ ,
且'self'必须为第一个参数。和类的私有属性命名相同,类的私有方法名也要以两条下划线开始。
Python代码
>>>classbook:
... __author = ''
... __name = ''
... __page = 0
... price = 0
... defshow(self):
... printself.__author
... printself.__name
... defsetname(self,name):
... self.__name = name
...
>>> a = book()
>>> a.show()
>>> a.setname('Tom')
>>> a.show()
Tom
>>>
在python中有一类以两条下划线开始并且以两条下划线结束的类方法,称之为专有方法。
__init__ 构造函数,生成对象时调用
__del__ 析构函数,释放对象时调用
__add__ 加运算
__mul__ 乘运算
__cmp__ 比较运算
__repr__ 打印、转换
__setitem__ 按照索引赋值
__getitem__ 按照索引获取值
__len__ 获得长度
__call__ 函数调用
Python代码
>>>classbook:
... __author = ''
... __name = ''
... __page = ''
... price = 0
... def__check(self,item):
... ifitem =='':
... return0
... else:
... return1
... defshow(self):
... ifself.__check(self.__author):
... printself.__author
... else:
... print'No value'
... ifself.__check(self.__name):
... printself.__name
... else:
... print'No value'
... defsetname(self,name):
... self.__name = name
... def__init__(self,author,name):
... self.__author = author
... self.__name = name
...
三、类的继承
1)单继承
Python代码
>>>classparent:
... __a = ''
... __b = 0
... def__init__(self,a,b):
... self.__a = a
... self.__b = b
... defshow(self):
... printself.__a
... printself.__b
...
>>> a = parent('a',2)
>>> a.show()
a
2
>>> classchild(parent):
... __c = ''
... __d = 4
... defshowinfo(self):
... self.show()
...
>>> b = child('c',3)
>>> b.show()
c
3
>>> b.showinfo()
c
3
>>>
2)多重继承
Python代码
# -*- coding:utf-8 -*-
classA:#定义类A
name = 'A'
__num = 1
defshow(self):
printself.name
printself.__num
defsetnum(self,num):
self.__num = num
classB:#定义类B
nameb = 'B'
__numb = 2
defshow(self):
printself.nameb
printself.__numb
defsetname(self,name):
self.__name = name
classC(A,B):
defshowall(self):
printself.name
printself.nameb
show = B.show #在这里表明show方法为B类的show方法,后来修改加上的
>>> importjicheng
>>> a = jicheng.A()
>>> a.show()
A
1
>>> c = jicheng.C()
>>> c.showall()
A
B
>>> c.show() #默认调用A类的show方法
A
1
>>> reload(jicheng) #修改jicheng.py后重新加载
>>> d =jicheng.C()
>>> d.show()
B
2
>>>
五)重载
1)方法的重载实际上就是在类中使用def关键字重载父类的方法。如果重载父类中的方法,但又需要
在类中使用父类的该方法,可以使用父类名加‘ .’加方法名的形式调用。
Python代码
# -*- coding:utf-8 -*-
classMylist:
__mylist = []
def__init__(self,*args):
self.__mylist = []
forarginargs:
self.__mylist.append(arg)
def__add__(self,n):#重载‘+’运算符
foriinrange(0, len(self.__mylist)):
self.__mylist[i] =self.__mylist[i] + n
defshow(self):
printself.__mylist
>>> importchongzai
>>> L = chongzai.Mylist(1,2,3,4,5)
>>> L.show()
[1,2,3,4,5]
>>> L + 2
>>> L.show()
[3,4,5,6,7]
>>>