乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      python類定義的講解

       豆芽愛尚閱 2015-04-06
      一、類定義:
      復(fù)制代碼 代碼如下:

      class <類名>:
       <語句>

      類實(shí)例化后,可以使用其屬性,實(shí)際上,創(chuàng)建一個(gè)類之后,可以通過類名訪問其屬性。如果直接使用類名修改其屬性,那么將直接影響到已經(jīng)實(shí)例化的對(duì)象

      類的私有屬性:
        __private_attrs  兩個(gè)下劃線開頭,聲明該屬性為私有,不能在類地外部被使用或直接訪問。在類內(nèi)部的方法中使用時(shí) self.__private_attrs
      類的方法
        在類地內(nèi)部,使用def關(guān)鍵字可以為類定義一個(gè)方法,與一般函數(shù)定義不同,類方法必須包含參數(shù)self,且為第一個(gè)參數(shù)
      私有的類方法
        __private_method 兩個(gè)下劃線開頭,聲明該方法為私有方法,不能在類地外部調(diào)用。在類的內(nèi)部調(diào)用slef.__private_methods

      類的專有方法:
      __init__  構(gòu)造函數(shù),在生成對(duì)象時(shí)調(diào)用
      __del__   析構(gòu)函數(shù),釋放對(duì)象時(shí)使用
      __repr__ 打印,轉(zhuǎn)換
      __setitem__按照索引賦值
      __getitem__按照索引獲取值
      __len__獲得長(zhǎng)度
      __cmp__比較運(yùn)算
      __call__函數(shù)調(diào)用

      __add__加運(yùn)算
      __sub__減運(yùn)算
      __mul__乘運(yùn)算
      __div__除運(yùn)算
      __mod__求余運(yùn)算
      __pow__稱方
      復(fù)制代碼 代碼如下:

      #類定義 
          class people: 
              #定義基本屬性 
              name = '' 
              age = 0 
              #定義私有屬性,私有屬性在類外部無法直接進(jìn)行訪問 
              __weight = 0 
              #定義構(gòu)造方法 
              def __init__(self,n,a,w): 
                  self.name = n 
                  self.age = a 
                  self.__weight = w 
              def speak(self): 
                  print("%s is speaking: I am %d years old" %(self.name,self.age)) 

           
          p = people('tom',10,30) 
          p.speak()

      二、繼承類定義:
      1.單繼承
      復(fù)制代碼 代碼如下:

      class <類名>(父類名)
         <語句>

      復(fù)制代碼 代碼如下:

      class childbook(book)
          age = 10

      復(fù)制代碼 代碼如下:

      #單繼承示例 
          class student(people): 
              grade = '' 
              def __init__(self,n,a,w,g): 
                  #調(diào)用父類的構(gòu)函 
                  people.__init__(self,n,a,w) 
                  self.grade = g 
              #覆寫父類的方法 
              def speak(self): 
                  print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))      

          s = student('ken',20,60,3) 
          s.speak()

      2.類的多重繼承
      復(fù)制代碼 代碼如下:

      class 類名(父類1,父類2,....,父類n)
           <語句1>

      需要注意圓括號(hào)中父類的順序,若是父類中有相同的方法名,而在子類使用時(shí)未指定,python從左至右搜索,即方法在子類中未找到時(shí),從左到右查找父類中是否包含方法
      復(fù)制代碼 代碼如下:

      #另一個(gè)類,多重繼承之前的準(zhǔn)備 
      class speaker(): 
          topic = '' 
          name = '' 
          def __init__(self,n,t): 
              self.name = n 
              self.topic = t 
          def speak(self): 
              print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic)) 

      #多重繼承 
      class sample(speaker,student): 
          a ='' 
          def __init__(self,n,a,w,g,t): 
              student.__init__(self,n,a,w,g) 
              speaker.__init__(self,n,t) 

      test = sample("Tim",25,80,4,"Python") 
      test.speak()#方法名同,默認(rèn)調(diào)用的是在括號(hào)中排前地父類的方法

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多