亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python使用type關(guān)鍵字創(chuàng)建類(lèi)步驟詳解

 更新時(shí)間:2019年07月23日 15:59:49   作者:卡和我  
在本文里我們給讀者們整理了關(guān)于Python如何使用type關(guān)鍵字創(chuàng)建類(lèi)的相關(guān)知識(shí)點(diǎn),需要的朋友們參考學(xué)習(xí)下。

Python使用type關(guān)鍵字創(chuàng)建類(lèi)

打開(kāi)命令行窗口,輸入python,進(jìn)入python交互環(huán)境

python

一般創(chuàng)建類(lèi)使用class關(guān)鍵字即可,測(cè)試命令如下:

class Coo:

  pass

obj1 = Coo()

print (obj1)

c = Coo

obj2 = c()

print (obj2)

type關(guān)鍵字可以動(dòng)態(tài)的創(chuàng)建類(lèi),接收參數(shù)(類(lèi)名,父類(lèi)元組,屬性的字典),如創(chuàng)建一個(gè)類(lèi),沒(méi)有父類(lèi),沒(méi)有屬性,命令如下:

Test = type('Test',(),{})

print (Test)

t = Test()

print (t)

接收type函數(shù)返回的變量可以是任意命令,傳入type的才是類(lèi)名,變量只是類(lèi)的引用

使用type創(chuàng)建有屬性的類(lèi),命令如下:

Test = type('Test2',(),{'hi':True})

print (Test)

print (Test.hi)

t = Test()

print (t.hi)

使用type創(chuàng)建并繼承的類(lèi)

Test3 = type('Test3',(Test,),{})

t = Test3()

print (t.hi)

使用type創(chuàng)建帶實(shí)例方法的類(lèi),命令如下:

def echo(self):

  print (self.hi)

Test4 = type('Test4',(Test,),{'echo':echo})

hasattr(Test,'echo')

hasattr(Test4,'echo')

使用type創(chuàng)建帶靜態(tài)方法,命令如下:

@staticmethod

def staticm():

  print ('staticm')

Test5 = type('Test5',(Test,),{'echo':echo,'staticm':staticm})

t = Test5()

t.staticm()

使用type創(chuàng)建帶類(lèi)方法的類(lèi),命令如下:

@classmethod

def classm(cls):

  print (cls.hi)

Test6 = type('Test6',(Test,),{'echo':echo,'staticm':staticm,'classm':classm})

Test6.classm()

以上就是相關(guān)Python如何使用type關(guān)鍵字創(chuàng)建類(lèi)的全部?jī)?nèi)容,感謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論