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

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

    • 分享

      Python中的面向對象編程詳解(下)

       imelee 2016-12-15

      繼承

      繼承描述了基類的屬性如何“遺傳”給派生類。一個子類可以繼承它的基類的任何屬性,不管是數(shù)據(jù)屬性還是方法。
      創(chuàng)建子類的語法看起來與普通(新式)類沒有區(qū)別,一個類名,后跟一個或多個需要從其中派生的父類:

      復制代碼 代碼如下:

      class SubClassName (ParentClass1[, ParentClass2, ...]):
          'optional class documentation string'
          class_suite

      實例
      復制代碼 代碼如下:

      class Parent(object): # define parent class 定義父類
          def parentMethod(self):
          print 'calling parent method'

      class Child(Parent): # define child class 定義子類
          def childMethod(self):
          print 'calling child method'


      繼承與覆蓋

      繼承

      不同于Java,python的子類繼承父類后,會把父類的所有的方法,包括構造器init()也繼承下來.

      復制代碼 代碼如下:

      class Parent():
          def __init__(self):
              print "init Parent class instance"

          def func(self):
              print "call parent func"

      class Child(Parent):
          def __init__(self):
              print "init Child class instance"

      child = Child()
      child.func()


      輸出
      復制代碼 代碼如下:

      init Child class instance
      call parent func

      super關鍵字

      super 是用來解決多重繼承問題的,直接用類名調用父類方法在使用單繼承的時候沒問題,但是如果使用多繼承,會涉及到查找順序(MRO)、重復調用(鉆石繼承)等種種問題。語法如下

      復制代碼 代碼如下:

      super(type[, obj])

      示例
      復制代碼 代碼如下:

      class C(B):
          def method(self, arg):
              super(C, self).method(arg)

      注意

      super繼承只能用于新式類,用于經(jīng)典類時就會報錯。
      新式類:必須有繼承的類,如果沒什么想繼承的,那就繼承object
      經(jīng)典類:沒有父類,如果此時調用super就會出現(xiàn)錯誤:『super() argument 1 must be type, not classobj』
      實例

      復制代碼 代碼如下:

      class Parent(object):
          def __init__(self):
              self.phone = '123456'
              self.address = 'abcd'

      class Child(Parent):
          def __init__(self):
              super(Child, self).__init__()
              self.data = 100

      def main():
          child = Child()
          print "phone is: ", child.phone
          print "address is: ", child.address
          print "data is: ", child.data

      if __name__ == '__main__':
          main()


      輸出
      復制代碼 代碼如下:

      phone is:  123456
      address is:  abcd
      data is:  100

      重寫

      子類只要重新定義一個與父類的方法同名的方法,就可以重寫覆蓋父類的方法. 子類只要把上例父類的func(self)重寫就行了.

      復制代碼 代碼如下:

      class Parent():
      def __init__(self):
      print "init Parent class instance"
      def func(self):
      print "call parent func"
      class Child(Parent):
      def __init__(self):
      print "init Child class instance"

      child = Child()
      child.func()


      輸出
      復制代碼 代碼如下:

      init Child class instance
      call Child func

      多重繼承

      同 C++一樣,Python 允許子類繼承多個基類。但一般不推薦用多重繼承.語法如下:

      復制代碼 代碼如下:

      class Father():
          def __init__(self):
              print "init Father instance"

      class Mother():
          def __init__(self):
              print "init Mother instance"

      class Child(Father, Mother):
          pass


      類、實例和其他對象的內建函數(shù)

      issubclass()

      布爾函數(shù)判斷一個類是另一個類的子類或子孫類。它有如下語法:

      復制代碼 代碼如下:

      issubclass(sub, sup)

      isinstance()

      布爾函數(shù)在判定一個對象是否是另一個給定類的實例時,非常有用。它有如下語法:

      復制代碼 代碼如下:

      isinstance(obj1, obj2)

      attr()系列函數(shù)

      ●hasattr()
      它的目的就是為了決定一個對象是否有一個特定的屬性,一般用于訪問某屬性前先作一下檢查。
      ●getattr()和setattr()
      ●getattr()和 setattr()函數(shù)相應地取得和賦值給對象的屬性,

      ●delattr()
      刪除特定的屬性

      實例

      復制代碼 代碼如下:

      class Child(Parent):
          def __init__(self):
              self.data = 100

      child = Child()
      print "has data attr?", hasattr(child, 'data')

      print "delete attr"
      delattr(child, 'data')

      print "has data attr?", hasattr(child, 'data')

      print "set data attr to 200"
      setattr(child, 'data', 200)
      print "data attr is: ", getattr(child, 'data')


      輸出
      復制代碼 代碼如下:

      has data attr? True
      delete attr
      has data attr? False
      set data attr to 200
      data attr is:  200

      私有化

      Python沒有像Java那樣實現(xiàn)真正的封裝,只是用雙劃線和單劃線實現(xiàn)私有化.

      ●雙劃線
      防止外部訪問.如在func前加雙劃線,可以防止包括子類的實例的訪問.

      復制代碼 代碼如下:

          def __func(self):
              print "call"

      ●單劃線
      防止模塊的屬性用“from mymodule import *”來加載。

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

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多