44、class类:
一、类定义的一般形式:
1、简单的形式:实例化对象没有自己独有的数据属性。
>>> class fistclass():
... data1 = ‘hello world‘ ### ==>这是类的数据属性或类成员属性。
... def printdata(self): ###==>这是类的方法,而且这里至少有一个参数self!!!
... print(‘nihao%s‘ %self.data1)
...
>>> class1 = fistclass() ##==>类实例化
>>> class1.
class1.__class__( class1.__format__( class1.__le__( class1.__reduce_ex__( class1.__weakref__
class1.__delattr__( class1.__ge__( class1.__lt__( class1.__repr__( class1.data1
class1.__dict__ class1.__getattribute__( class1.__module__ class1.__setattr__( class1.printdata(
class1.__dir__( class1.__gt__( class1.__ne__( class1.__sizeof__(
class1.__doc__ class1.__hash__( class1.__new__( class1.__str__(
class1.__eq__( class1.__init__( class1.__reduce__( class1.__subclasshook__(
>>> class1.data1 ##类数据属性调用
‘hello world‘
>>> class1.printdata() ##类的方法的调用
nihaohello world
2、定义对象独有的数据属性:
>>> class seclass():
... data1 = ‘hello seclass‘
... def setdata(self,x):
... self.str1 = x
... def printdata(self):
... print(self.str1)
...
>>> ins1 = seclass()
>>> ins1.
ins1.__class__( ins1.__format__( ins1.__le__( ins1.__reduce_ex__( ins1.__weakref__
ins1.__delattr__( ins1.__ge__( ins1.__lt__( ins1.__repr__( ins1.data1
ins1.__dict__ ins1.__getattribute__( ins1.__module__ ins1.__setattr__( ins1.printdata(
ins1.__dir__( ins1.__gt__( ins1.__ne__( ins1.__sizeof__( ins1.setdata(
ins1.__doc__ ins1.__hash__( ins1.__new__( ins1.__str__(
ins1.__eq__( ins1.__init__( ins1.__reduce__( ins1.__subclasshook__(
>>> ins1.data1
‘hello seclass‘
>>> ins1.setdata(‘abc‘)
>>> ins1.printdata()
abc
>>> ins2 = seclass()
>>> ins2.data1
‘hello seclass‘
>>> ins2.setdata(‘xy‘)
>>> ins2.printdata()
xy
3、__init__()方法:
创建实例时,python会自动调用类中的__init__方法,以隐性的为实例提供属性。
__init__称之为构造器。
如果类中没有定义__init__方法,则实例之初仅仅是创建一个简单的名称空间。
>>> class thirdclass():
... data3 = ‘hello thirdclass‘
... def __init__(self,who):
... self.name = who
...
>>> ins3 = thirdclass(‘lucy‘)
>>> ins3.
ins3.__class__( ins3.__format__( ins3.__le__( ins3.__reduce_ex__( ins3.__weakref__
ins3.__delattr__( ins3.__ge__( ins3.__lt__( ins3.__repr__( ins3.data3
ins3.__dict__ ins3.__getattribute__( ins3.__module__ ins3.__setattr__( ins3.name
ins3.__dir__( ins3.__gt__( ins3.__ne__( ins3.__sizeof__(
ins3.__doc__ ins3.__hash__( ins3.__new__( ins3.__str__(
ins3.__eq__( ins3.__init__( ins3.__reduce__( ins3.__subclasshook__(
>>> ins3.data3
‘hello thirdclass‘
>>> ins3.name
‘lucy‘
4、__dict__:
>>> ins3.__dict__ 实例的属性
{‘name‘: ‘lucy‘}
>>> thirdclass.__dict__ 类的属性。
mappingproxy({‘data3‘: ‘hello thirdclass‘, ‘__init__‘: <function thirdclass.__init__ at 0x7f8e0a440ea0>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘thirdclass‘ objects>, ‘__module__‘: ‘__main__‘, ‘__doc__‘: None, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘thirdclass‘ objects>})
5、类中的可用变量:
>>> class c1():
... var1 = ‘hello c1‘ ###==>类的静态变量!!!!
... def __init__(self,who):
... self.insdata = who ###==>实例变量。
... self.name = ‘123‘ ###==>局部变量:只属于这个方法。
...
>>> ins1 = c1(12)
>>> ins1.
ins1.__class__( ins1.__format__( ins1.__le__( ins1.__reduce_ex__( ins1.__weakref__
ins1.__delattr__( ins1.__ge__( ins1.__lt__( ins1.__repr__( ins1.insdata
ins1.__dict__ ins1.__getattribute__( ins1.__module__ ins1.__setattr__( ins1.name
ins1.__dir__( ins1.__gt__( ins1.__ne__( ins1.__sizeof__( ins1.var1
ins1.__doc__ ins1.__hash__( ins1.__new__( ins1.__str__(
ins1.__eq__( ins1.__init__( ins1.__reduce__( ins1.__subclasshook__(
>>> ins1.name
‘123‘
>>> ins1.var1
‘hello c1‘
>>> ins1.insdata
12
6、类的继承:python允许多重继承,属性搜索方法:从左往右,从下往上。
>>> class PClass(object):
... gender = ‘male‘
... def __init__(self,who):
... self.name = who
...
>>> class CClass(PClass):
... def displayinfo(self):
... print(self.gender,self.name)
...
>>> ins1 = CClass(‘lucy‘)
>>> ins1.
ins1.__class__( ins1.__format__( ins1.__le__( ins1.__reduce_ex__( ins1.__weakref__
ins1.__delattr__( ins1.__ge__( ins1.__lt__( ins1.__repr__( ins1.displayinfo(
ins1.__dict__ ins1.__getattribute__( ins1.__module__ ins1.__setattr__( ins1.gender
ins1.__dir__( ins1.__gt__( ins1.__ne__( ins1.__sizeof__( ins1.name
ins1.__doc__ ins1.__hash__( ins1.__new__( ins1.__str__(
ins1.__eq__( ins1.__init__( ins1.__reduce__( ins1.__subclasshook__(
>>> ins1.gender
‘male‘
>>> ins1.name
‘lucy‘
>>> ins1.displayinfo()
male lucy