日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Python的內(nèi)置方法和類(lèi)的繼承舉例-創(chuàng)新互聯(lián)

1.類(lèi)的內(nèi)置方法

Python內(nèi)部類(lèi):
所謂內(nèi)部類(lèi),就是在類(lèi)的內(nèi)部定義的類(lèi),主要目的是為了更好的抽象現(xiàn)實(shí)世界。
例子:
汽車(chē)是一個(gè)類(lèi),汽車(chē)的底盤(pán)輪胎也可以抽象為類(lèi),將其定義到汽車(chē)內(nèi)中,而形成內(nèi)部類(lèi),
更好的描述汽車(chē)類(lèi),因?yàn)榈妆P(pán)輪胎是汽車(chē)的一部分。
內(nèi)部類(lèi)實(shí)例化方法:

在門(mén)頭溝等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供做網(wǎng)站、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都營(yíng)銷(xiāo)網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站制作,門(mén)頭溝網(wǎng)站建設(shè)費(fèi)用合理。

方法1:直接使用外部類(lèi)調(diào)用內(nèi)部類(lèi)
方法2:先對(duì)外部類(lèi)進(jìn)行實(shí)例化,然后再實(shí)例化內(nèi)部類(lèi)
out_name = outclass_name()
in_name = out_name.inclass_name()
in_name.method()

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有屬性

    class Chinese(object):
        print("I am chinese")

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #調(diào)用類(lèi)的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類(lèi)的方法 
    def test1():    
        print ("this is static method")

jack = People.Chinese()
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有屬性

    class Chinese(object):
        name ="I am a Chinese."

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #調(diào)用類(lèi)的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類(lèi)的方法 
    def test1():    
        print ("this is static method")

jack = People.Chinese()  #外部類(lèi)調(diào)用內(nèi)部類(lèi)
print jack.name     #外部類(lèi)調(diào)用內(nèi)部類(lèi)對(duì)象

另一種方法,外部類(lèi)調(diào)用內(nèi)部類(lèi)對(duì)象

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有屬性

    class Chinese(object):
        name ="I am a Chinese."

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #調(diào)用類(lèi)的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類(lèi)的方法 
    def test1():    
        print ("this is static method")

ren = People()            #實(shí)例化外部類(lèi)
jack = ren.Chinese()   #實(shí)例化內(nèi)部類(lèi)
print jack.name           #打印內(nèi)部類(lèi)屬性

或
print People.Chinese.name
print People.Chinese().name
魔術(shù)方法:

str(self)
構(gòu)造函數(shù)與析構(gòu)函數(shù)
構(gòu)造函數(shù):

用于初始化類(lèi)的內(nèi)部狀態(tài),Python提供的構(gòu)造函數(shù)是__init__():
__init__():方法是可選的,如果不提供,python會(huì)給出一個(gè)默認(rèn)的__init__方法。

析構(gòu)函數(shù):

用于釋放對(duì)象占用的資源,python提供的析構(gòu)函數(shù)是__del__():
__del__():也是可選的,如果不提供,則python會(huì)在后臺(tái)提供默認(rèn)析構(gòu)函數(shù)。

構(gòu)造函數(shù)str

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有屬性

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #調(diào)用類(lèi)的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類(lèi)的方法 
    def test1():    
        print ("this is static method")

ren = People()            #實(shí)例化外部類(lèi)
print ren     #默認(rèn)執(zhí)行__str__

init(self)初始化類(lèi):

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有屬性

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def __init__(self,c='white'):   #類(lèi)實(shí)例化時(shí)自動(dòng)執(zhí)行
        self.color = c
 self.think()

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #調(diào)用類(lèi)的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類(lèi)的方法 
    def test1():    
        print ("this is static method")

jack = People('green')
ren = People()            #實(shí)例化外部類(lèi)
print ren.color        #通過(guò)對(duì)象訪問(wèn)屬性是初始化后的值
print People.color    #通過(guò)類(lèi)訪問(wèn)還是原來(lái)的值   

[root@localhost 20180110]# python test1.py 
I am a black 
I am a thinker
30
black
yellow

析構(gòu)函數(shù)del():釋放資源

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有屬性

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def __init__(self,c='white'):   #類(lèi)實(shí)例化時(shí)自動(dòng)執(zhí)行
        print ("initing...")
 self.color = c
        self.think()
        f = open('test.py')

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #調(diào)用類(lèi)的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類(lèi)的方法 
    def test1():    
        print ("this is static method")

     def __del__(self):
          print ("del....")
   self.f.close()

jack = People('green')
ren = People()            #實(shí)例化外部類(lèi)
print ren.color        #通過(guò)對(duì)象訪問(wèn)屬性是初始化后的值
print People.color    #通過(guò)類(lèi)訪問(wèn)還是原來(lái)的值
垃圾回收機(jī)制:

Python采用垃圾回收機(jī)制來(lái)清理不再使用的對(duì)象;python提供gc模塊釋放不再使用的對(duì)象。
Python采用“引用計(jì)數(shù)”的算法方式來(lái)處理回收,即:當(dāng)然某個(gè)對(duì)象在其作用域內(nèi)不再被其
他對(duì)象引用的時(shí)候,python就自動(dòng)化清除對(duì)象。
gc模塊collect()可以一次性收集所有待處理的對(duì)象(gc.collect)

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    __age = 30   #私有屬性

    class Chinese(object):
        name ="I am a Chinese."

    def __str__(self):
        return "This is People class"

    def __init__(self,c='white'):   #類(lèi)實(shí)例化時(shí)自動(dòng)執(zhí)行
        print ("initing...")
                 self.color = c
        self.think()
        f = open('test.py')

    def think(self):
        self.color = "black"
        print "I am a %s "  % self.color
        print ("I am a thinker")
        print self.__age

    def  __talk(self):
        print "I am talking with Tom"

    @classmethod #調(diào)用類(lèi)的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類(lèi)的方法 
    def test1():    
        print ("this is static method")

     def __del__(self):
          print ("del....")
   self.f.close()

print gc.collect()     如果是0是沒(méi)有回收的。
jack = People('green')
ren = People()            #實(shí)例化外部類(lèi)
print ren.color        #通過(guò)對(duì)象訪問(wèn)屬性是初始化后的值
print People.color    #通過(guò)類(lèi)訪問(wèn)還是原來(lái)的值   

2.類(lèi)的繼承

類(lèi)的繼承
繼承是面向?qū)ο蟮闹匾匦灾唬?
繼承關(guān)系繼承是相對(duì)兩個(gè)類(lèi)而言的父子關(guān)系

子類(lèi)繼承了父類(lèi)的所有公有屬性和方法,

繼承,實(shí)現(xiàn)了代碼重用
使用繼承
繼承可以重用已經(jīng)存在的數(shù)據(jù)和行為,減少代碼的重復(fù)編寫(xiě),

Python在類(lèi)名后使用一對(duì)括號(hào)來(lái)表示繼承關(guān)系,括號(hào)中的即類(lèi)為父類(lèi)

class Myclass(ParentClass),

如果父類(lèi)定義了__init__方法,子類(lèi)必須顯式調(diào)用父類(lèi)的__init__方法,

ParentClass.__init__(self,[args...])

如果子類(lèi)需要擴(kuò)展父類(lèi)的行為,可以添加__init__方法的參數(shù).
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'

    def think(self):
    self.color = "black"
    print "I am a %s "  % self.color
    print ("I am a thinker")

class Chinese(People):
    pass

cn = Chinese()
print cn.color
cn.think()

父類(lèi)中有構(gòu)造函數(shù):

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
     def __init__(self):
        print "Init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s "  % self.color
        print ("I am a thinker")
class Chinese(People):
    pass
cn = Chinese()
print cn.dwell
cn.think()

參數(shù)大于兩個(gè):

#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    def __init__(self,c):
        print "Init..."
        self.dwell = 'Earth'
     def think(self):
        print "I am a %s "  % self.color
        print ("I am a thinker")
class Chinese(People):
     def __init__(self):
        People.__init__(self,'red')
        pass
cn = Chinese()

Super 函數(shù)

class A(object):
        def __init__(self):
            print "enter A"
            print "leave A"
class B(object):
        def __init__(self):
            print "enter B"
            super(B,self),__init__()
            print "leave B"
b = B()
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    def __init__(self,c):
        print "Init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s "  % self.color
        print ("I am a thinker")
class Chinese(People):
    def __init__(self):
       super(Chinese,self).__init__('red')
       pass
cn = Chinese()
cn.think()
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    def __init__(self,c):
        print "Init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s "  % self.color
        print ("I am a thinker")
class Chinese(People):
    def __init__(self):
        super(Chinese,self).__init__('red')
     def talk(self):
        print "I like taking."
cn = Chinese()
cn.think()
cn.talk()

多重繼承

Python支持多重繼承,第一個(gè)類(lèi)可以繼承多個(gè)父類(lèi)

語(yǔ)法:

class class_name(Parent_c1,Parent_c2,...)

注意:

當(dāng)父類(lèi)中出現(xiàn)多個(gè)自定義的__init__的方法時(shí),

多重繼承,只執(zhí)行第一個(gè)累的__init_方法,其他不執(zhí)行。
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    color = 'yellow'
    def __init__(self):
        print "Init..."
        self.dwell = 'Earth'
    def think(self):
        print "I am a %s "  % self.color
        print ("My home is %s ") % self.dwell
class Martian(object):
    color = 'red'
    def __init__(self):
        self.dwell = 'Martian'
class Chinese(People,Martian):
    def __init__(self):
        People.__init__(self)
cn = Chinese()
cn.think()
#!/usr/bin/env python
#-*- coding:utf-8  -*-
class People(object):
    def __init__(self):
        self.dwell = 'Earth'
         self.color = 'yellow'
    def think(self):
        print "I am a %s "  % self.color
        print ("My home is %s ") % self.dwell
class Martian(object):
    color = 'red'
    def __init__(self):
        self.dwell = 'Martian'
    def talk(self):
        print "I like talking"
class Chinese(Martian,People):
    def __init__(self):
        People.__init__(self)
cn = Chinese()
cn.think()
cn.talk()

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。


當(dāng)前題目:Python的內(nèi)置方法和類(lèi)的繼承舉例-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://www.dlmjj.cn/article/jcpgo.html