python中类的总结

1、 类中的方法

在类里主要有三种方法:

a、普通方法:在普通方法定义的时候,需要一个对象的实例参数,从而在类中定义普通方法的时候,都必须传送一个参数self,那么这个参数也就是object

b、类方法:在类方法中,传递的参数为类也就是class,在一般使用中参数为cls

c、静态方法:无需任何参数,在静态方法中,仅仅是一个函数,在调用静态方法的时候,不需要传递任何参数即可使用

在类的三种方法中,普通方法和类方法都是绑定的方法,也就是在其中需要传递参数,普通方法传递为slef,类方法传递的为cls


在使用的时候,静态方法仅仅是逻辑上和class绑定在一起的,而类方法可以做动态的匹配,传递进去的是clas,也就是类


在语法上,静态方法需要使用装饰符@staticmethod,而类方法需要使用装饰符@classmethod


使用模块函数比使用静态方法更加常见。

#!/usr/bin/env python

class TestMethod(object):
    def foo(self,x): #定义类中的普通函数,传递参数为self,也就是类的实例
        print 'excuting foo(%s,%s)' % (self,x)
    @staticmethod #定义静态方法,在其中不传递参数
    def static_foo(x):
        print 'excuting static_foo(%s)' % x
    @classmethod #传递类方法,在其中传递的参数为cls
    def class_foo(cls,x):
        print 'excuting class_foo(%s,%s)' % (cls,x)

kel = TestMethod()
kel.foo(1)
kel.static_foo(1)
kel.class_foo(1)
TestMethod.static_foo(1)
TestMethod.class_foo(1)
print (kel.foo)
print (kel.static_foo)
print (kel.class_foo)
执行结果如下:

[root@python 422]# python testMethod.py 
excuting foo(<__main__.TestMethod object at 0x7f41f884c050>,1) #参数为绑定的实例,也就是object
excuting static_foo(1) #静态方法没有其他参数
excuting class_foo(<class '__main__.TestMethod'>,1) #类方法使用的参数是类
excuting static_foo(1)
excuting class_foo(<class '__main__.TestMethod'>,1)
<bound method TestMethod.foo of <__main__.TestMethod object at 0x7f41f884c050>> #普通方法是绑定方法,使用的参数为self
<function static_foo at 0x7f41f884a0c8>
<bound method type.class_foo of <class '__main__.TestMethod'>> # 类方法也是绑定方法,使用的参数为cls


2、 类的特殊的属性

类中具有几种特殊属性,如下所示:

__name__,表示类的名字

__doc__,类的文档字符串

__bases__,类的所有父类构成的元组

__dict__,类的属性

__module__,类所在的模块

__class__,类多对用的类

在使用类的方法__init__方法中,此方法应该返回为None:

#!/usr/bin/env python

class Kel(object):
    def __init__(self):
        print 'this is the init method called'
        return 1

kel = Kel()

在如上的代码中,在init方法中,返回了一个值为1

执行结果如下:

[root@python 422]# python testinitRetun.py 
this is the init method called
Traceback (most recent call last):
  File "testinitRetun.py", line 8, in <module>
    kel = Kel()
TypeError: __init__() should return None, not 'int'

也就是在init方法中,必须返回值为None。


3、 关于方法__init__

#!/usr/bin/env python

class Kel(object): #父类方法
    def __init__(self):
        print 'Kel class called'
class J(Kel):#J是继承Kel类
    def __init__(self): #重写了init方法
        print 'J class is called'
class M(Kel):#M继承Kel类
    def __init__(self):#重写了init方法
        super(M,self).__init__()#调用了父类的方法
        print 'M class is called'
print '-'*10
kel = Kel()
print '-'*10
j = J()
print '-'*10
m = M()
执行结果如下:

[root@python 422]# python testinitRetun.py 
----------
Kel class called
----------
J class is called
----------
Kel class called
M class is called
可以看到,如果子类写了自己的init方法,那么不会自动的调用父类的init方法,必须在子类的init方法中自己进行调用,如上子类M所示


每个子类最好构造自己的构造器,不然积累的构造器会被调用,然而,如果子类重写基类的构造器,基类的构造器就不会被自动调用了。

a、 基类的构造器就必须显式的写出来才会被执行

b、 传递self的实例对象给基类进行调用,在上面的例子中,使用的是内建方法super,最好是使用super方法

在使用super调用基类方法的时候,找到基类方法,并且传入self参数即可,不需要明确指定父类的名字,并且在修改的时候,也是容易修改的。



4、 类的使用

一个类被定义后,目标就是要把它当成一个模块来使用,并把对象嵌入到代码中,同其他数据类型及逻辑执行流混合使用。

使用类的方式有两种,一种是组合,一种是派生

#!/usr/bin/env python
class Phone(object):
    def __init__(self,ph):
        self.phone = ph

class Person(object):
    def __init__(self,nm,ph):
        self.name = nm
        self.phone = Phone(ph)

p = Person('kel','1234143')
print p.name,p.phone.phone

如上代码所示,使用的是组合的方式,在类Phone中,表示手机号码,而在类Person中包含了一个phone对象,那么就表示person has-a phone,类与类之间的关系为has-a的关系,在一个类中包含其他类的实例,就表示为组合。

class Person(object):
    def __init__(self,nm,ph):
        self.name = nm
        self.phone = Phone(ph)

class Kel(Person):
    def shout(self):
        pass
        

如上的代码中Kel继承了person类,从而表示为类与类之间的关系为派生关系,也就是表示:相同的类具有一些不同的功能,从而可以写出自己的方法。

一个子类可以继承它的基类的任何属性,不关是数据属性还是方法。


在继承中可以覆盖父类的方法,——直接使用同名的函数即可进行覆盖,也就是所谓的override方法







posted @ 2016-04-22 19:45  KEL  阅读(291)  评论(0编辑  收藏  举报