关于python类_python中类的总结
1、 類中的方法
在類里主要有三種方法:
a、普通方法:在普通方法定義的時(shí)候,需要一個(gè)對象的實(shí)例參數(shù),從而在類中定義普通方法的時(shí)候,都必須傳送一個(gè)參數(shù)self,那么這個(gè)參數(shù)也就是object
b、類方法:在類方法中,傳遞的參數(shù)為類也就是class,在一般使用中參數(shù)為cls
c、靜態(tài)方法:無需任何參數(shù),在靜態(tài)方法中,僅僅是一個(gè)函數(shù),在調(diào)用靜態(tài)方法的時(shí)候,不需要傳遞任何參數(shù)即可使用
在類的三種方法中,普通方法和類方法都是綁定的方法,也就是在其中需要傳遞參數(shù),普通方法傳遞為slef,類方法傳遞的為cls
在使用的時(shí)候,靜態(tài)方法僅僅是邏輯上和class綁定在一起的,而類方法可以做動(dòng)態(tài)的匹配,傳遞進(jìn)去的是clas,也就是類
在語法上,靜態(tài)方法需要使用裝飾符@staticmethod,而類方法需要使用裝飾符@classmethod
使用模塊函數(shù)比使用靜態(tài)方法更加常見。
#!/usr/bin/env python
class TestMethod(object):
def foo(self,x): #定義類中的普通函數(shù),傳遞參數(shù)為self,也就是類的實(shí)例
print 'excuting foo(%s,%s)' % (self,x)
@staticmethod #定義靜態(tài)方法,在其中不傳遞參數(shù)
def static_foo(x):
print 'excuting static_foo(%s)' % x
@classmethod #傳遞類方法,在其中傳遞的參數(shù)為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)執(zhí)行結(jié)果如下:
[root@python 422]# python testMethod.py
excuting foo(<__main__.testmethod object at>,1) #參數(shù)為綁定的實(shí)例,也就是object
excuting static_foo(1) #靜態(tài)方法沒有其他參數(shù)
excuting class_foo(,1) #類方法使用的參數(shù)是類
excuting static_foo(1)
excuting class_foo(,1)
> #普通方法是綁定方法,使用的參數(shù)為self
> # 類方法也是綁定方法,使用的參數(shù)為cls
2、 類的特殊的屬性
類中具有幾種特殊屬性,如下所示:
__name__,表示類的名字
__doc__,類的文檔字符串
__bases__,類的所有父類構(gòu)成的元組
__dict__,類的屬性
__module__,類所在的模塊
__class__,類多對用的類
在使用類的方法__init__方法中,此方法應(yīng)該返回為None:
#!/usr/bin/env python
class Kel(object):
def __init__(self):
print 'this is the init method called'
return 1
kel = Kel()
在如上的代碼中,在init方法中,返回了一個(gè)值為1
執(zhí)行結(jié)果如下:
[root@python 422]# python testinitRetun.py
this is the init method called
Traceback (most recent call last):
File "testinitRetun.py", line 8, in
kel = Kel()
TypeError: __init__() should return None, not 'int'
也就是在init方法中,必須返回值為None。
3、 關(guān)于方法__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__()#調(diào)用了父類的方法
print 'M class is called'
print '-'*10
kel = Kel()
print '-'*10
j = J()
print '-'*10
m = M()執(zhí)行結(jié)果如下:
[root@python 422]# python testinitRetun.py
----------
Kel class called
----------
J class is called
----------
Kel class called
M class is called可以看到,如果子類寫了自己的init方法,那么不會(huì)自動(dòng)的調(diào)用父類的init方法,必須在子類的init方法中自己進(jìn)行調(diào)用,如上子類M所示
每個(gè)子類最好構(gòu)造自己的構(gòu)造器,不然積累的構(gòu)造器會(huì)被調(diào)用,然而,如果子類重寫基類的構(gòu)造器,基類的構(gòu)造器就不會(huì)被自動(dòng)調(diào)用了。
a、 基類的構(gòu)造器就必須顯式的寫出來才會(huì)被執(zhí)行
b、 傳遞self的實(shí)例對象給基類進(jìn)行調(diào)用,在上面的例子中,使用的是內(nèi)建方法super,最好是使用super方法
在使用super調(diào)用基類方法的時(shí)候,找到基類方法,并且傳入self參數(shù)即可,不需要明確指定父類的名字,并且在修改的時(shí)候,也是容易修改的。
4、 類的使用
一個(gè)類被定義后,目標(biāo)就是要把它當(dāng)成一個(gè)模塊來使用,并把對象嵌入到代碼中,同其他數(shù)據(jù)類型及邏輯執(zhí)行流混合使用。
使用類的方式有兩種,一種是組合,一種是派生
#!/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中,表示手機(jī)號(hào)碼,而在類Person中包含了一個(gè)phone對象,那么就表示person has-a phone,類與類之間的關(guān)系為has-a的關(guān)系,在一個(gè)類中包含其他類的實(shí)例,就表示為組合。
class Person(object):
def __init__(self,nm,ph):
self.name = nm
self.phone = Phone(ph)
class Kel(Person):
def shout(self):
pass
如上的代碼中Kel繼承了person類,從而表示為類與類之間的關(guān)系為派生關(guān)系,也就是表示:相同的類具有一些不同的功能,從而可以寫出自己的方法。
一個(gè)子類可以繼承它的基類的任何屬性,不關(guān)是數(shù)據(jù)屬性還是方法。
在繼承中可以覆蓋父類的方法,——直接使用同名的函數(shù)即可進(jìn)行覆蓋,也就是所謂的override方法
總結(jié)
以上是生活随笔為你收集整理的关于python类_python中类的总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 模拟长按菜单键_如何采用
- 下一篇: python控制语句第一章_python