欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Python的內(nèi)置方法和類的繼承舉例-創(chuàng)新互聯(lián)

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

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

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

方法1:直接使用外部類調(diào)用內(nèi)部類
方法2:先對外部類進行實例化,然后再實例化內(nè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)用類的方法 
    def test(self):
        print ("this is class method")

    @staticmethod  #調(diào)用類的方法 
    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)用類的方法 
    def test(self):
        print ("this is class method")

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

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

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

#!/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)用類的方法 
    def test(self):
        print ("this is class method")

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

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

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

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

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

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

用于釋放對象占用的資源,python提供的析構(gòu)函數(shù)是__del__():
__del__():也是可選的,如果不提供,則python會在后臺提供默認析構(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)用類的方法 
    def test(self):
        print ("this is class method")

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

ren = People()            #實例化外部類
print ren     #默認執(zhí)行__str__

init(self)初始化類:

#!/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'):   #類實例化時自動執(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)用類的方法 
    def test(self):
        print ("this is class method")

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

jack = People('green')
ren = People()            #實例化外部類
print ren.color        #通過對象訪問屬性是初始化后的值
print People.color    #通過類訪問還是原來的值   

[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'):   #類實例化時自動執(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)用類的方法 
    def test(self):
        print ("this is class method")

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

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

jack = People('green')
ren = People()            #實例化外部類
print ren.color        #通過對象訪問屬性是初始化后的值
print People.color    #通過類訪問還是原來的值
垃圾回收機制:

Python采用垃圾回收機制來清理不再使用的對象;python提供gc模塊釋放不再使用的對象。
Python采用“引用計數(shù)”的算法方式來處理回收,即:當然某個對象在其作用域內(nèi)不再被其
他對象引用的時候,python就自動化清除對象。
gc模塊collect()可以一次性收集所有待處理的對象(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'):   #類實例化時自動執(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)用類的方法 
    def test(self):
        print ("this is class method")

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

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

print gc.collect()     如果是0是沒有回收的。
jack = People('green')
ren = People()            #實例化外部類
print ren.color        #通過對象訪問屬性是初始化后的值
print People.color    #通過類訪問還是原來的值   

2.類的繼承

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

子類繼承了父類的所有公有屬性和方法,

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

Python在類名后使用一對括號來表示繼承關(guān)系,括號中的即類為父類

class Myclass(ParentClass),

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

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

如果子類需要擴展父類的行為,可以添加__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()

父類中有構(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ù)大于兩個:

#!/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支持多重繼承,第一個類可以繼承多個父類

語法:

class class_name(Parent_c1,Parent_c2,...)

注意:

當父類中出現(xiàn)多個自定義的__init__的方法時,

多重繼承,只執(zhí)行第一個累的__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()

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

本文名稱:Python的內(nèi)置方法和類的繼承舉例-創(chuàng)新互聯(lián)
當前URL:http://aaarwkj.com/article10/jcpgo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供自適應網(wǎng)站、網(wǎng)站維護、用戶體驗網(wǎng)站策劃、面包屑導航微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
日本欧美一区二区精品| 国产精品乱码中文字幕| 岛国免费av在线观看| 永久免费看黄在线观看| 久久精品国产精品日韩欧美| 美国一级黄片在线观看| 欧美日韩一区二区高清在线| 麻豆视传媒短视频网站免费| 精品视频日韩在线观看| 日韩一区二区高清视频在线观看| 亚洲熟妇丰满多毛的大昊| 日韩高清在线不卡视频| 欧美日韩亚洲国产专区精品| 要爽死国产一区在线播放| 国产在线拍揄自揄视频不卡99| 亚洲欧洲久久激情久av| 色吊丝日韩在线观看| 日韩精品一区二区三区都在看| 精品日韩欧美在线观看91| av在线高清免费观看| 一区二区三区一级黄色| 免费爱爱视频在线观看| 亚洲日本韩国视频久久精品| 本色啪啪人妻夜嗨嗨av| 日韩高清不卡在线视频| 国产精品一区二在线观看| 91中文字幕精品一区二区| 原创国产av剧情精品| 91麻豆精品国产91久| 国产一级一片内射视频 | 日韩精品人妻一区二区免| 亚洲一区二区三区免费在线视频| 国产精品亚洲视频欧美视频| 91伊人手机在线观看| 一区二区三区蜜桃91| 白色发布会在线观看免费| 日本成人精品一区二区三区| 午夜未满十八禁止观看| 欧美性生活真实的视频| 国产一区二区日本在线| 一区二区三区av天堂|