一、类基础
1、类的定义
class <类名>:
<其他语句>
class <类名>(父类名):
<其他语句>
>>> class human: ... age=0 ... sex='' ... name = '' ... >>> class student(human): ... school = '' ... number = 0 ... grade = 0 ... >>>
2、类的使用
如果直接使用类名修改其属性,那么将影响已经实例化的对象。
>>> class A: ... 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
>>> class book: ... __author = '' ... __name = '' ... __page = 0 ... price = 0 ... >>> a = book() >>> a.__author Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: book instance has no attribute '__author' >>> a.price 0 >>>
2、类的方法
在类的内部使用def关键字可以为类定义一个方法。与函数定义不同的是,类的方法必须包含参数 'self ’ ,
且'self'必须为第一个参数。和类的私有属性命名相同,类的私有方法名也要以两条下划线开始。
>>> class book: ... __author = '' ... __name = '' ... __page = 0 ... price = 0 ... def show(self): ... print self.__author ... print self.__name ... def setname(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__ 函数调用
>>> class book: ... __author = '' ... __name = '' ... __page = '' ... price = 0 ... def __check(self,item): ... if item == '': ... return 0 ... else: ... return 1 ... def show(self): ... if self.__check(self.__author): ... print self.__author ... else: ... print 'No value' ... if self.__check(self.__name): ... print self.__name ... else: ... print 'No value' ... def setname(self,name): ... self.__name = name ... def __init__(self,author,name): ... self.__author = author ... self.__name = name ...
三、类的继承
1)单继承
>>> class parent: ... __a = '' ... __b = 0 ... def __init__(self,a,b): ... self.__a = a ... self.__b = b ... def show(self): ... print self.__a ... print self.__b ... >>> a = parent('a',2) >>> a.show() a 2 >>> class child(parent): ... __c = '' ... __d = 4 ... def showinfo(self): ... self.show() ... >>> b = child('c',3) >>> b.show() c 3 >>> b.showinfo() c 3 >>>
2)多重继承
# -*- coding:utf-8 -*- class A: #定义类A name = 'A' __num = 1 def show(self): print self.name print self.__num def setnum(self,num): self.__num = num class B: #定义类B nameb = 'B' __numb = 2 def show(self): print self.nameb print self.__numb def setname(self,name): self.__name = name class C(A,B): def showall(self): print self.name print self.nameb show = B.show #在这里表明show方法为B类的show方法,后来修改加上的 >>> import jicheng >>> a = jicheng.A() >>> a.show() A 1 >>> c = jicheng.C() >>> c.showall() A B >>> c.show() #默认调用A类的show方法 A 1 >>> reload(jicheng) #修改jicheng.py后重新加载 <module 'jicheng' from 'jicheng.py'> >>> d =jicheng.C() >>> d.show() B 2 >>>
五)重载
1)方法的重载实际上就是在类中使用def关键字重载父类的方法。如果重载父类中的方法,但又需要
在类中使用父类的该方法,可以使用父类名加‘ .’加方法名的形式调用。
# -*- coding:utf-8 -*- class Mylist: __mylist = [] def __init__(self,*args): self.__mylist = [] for arg in args: self.__mylist.append(arg) def __add__(self,n): #重载‘+’运算符 for i in range(0, len(self.__mylist)): self.__mylist[i] = self.__mylist[i] + n def show(self): print self.__mylist >>> import chongzai >>> L = chongzai.Mylist(1,2,3,4,5) >>> L.show() [1, 2, 3, 4, 5] >>> L + 2 >>> L.show() [3, 4, 5, 6, 7] >>>