<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 類 --- [TOC] --- ## 創建類 class Person(object): >若類沒有繼承,則object ## 所有實例化方法傳入self def read(self): return r'["Tim", "Bob", "Alice"]' ## 類的聲明 demo ``` class Person(object): 'this is a document' __count = 0 @classmethod def how_many(cls): return cls.__count #return Person.__count #效果同上 def __init__(self, name): self.name = name Person.count = Person.count + 1 ``` ### 類的屬性的聲明 >1. `__count`屬性的聲明 ,可以在實例之間調用, >3. 調用類的方法可通過 類方法的`cls`調用,可以類名字調用`Person.__count` ### 類的方法的聲明 >1. 在方法前加`@classmethod` 類放法中傳入類的參數`cls`,可自定義 >2. self 為類的實例的參數 ### 私有化 >1. `_count` 為類的*protected*型 允許本身和子類調用,代碼規范,可通過外部訪問 >2. `__count` 為類的*private*型只有本身調用 ,代碼規范,其實可訪問,如 >``` >a = Person() >a._Person__count #需在屬性前加類名 >``` ## 繼承 ```python class Children(Person): def __init__(self, name, age): super(Children, self).__init__(name) #繼承父類的name self.age=age #調用父類的方法 def add(self,number): number+=number result = super(Children,self).add(number) return result ``` > 1. `super(Children, self).method()`將返回當前類繼承的父類 ## 重載 ### 基礎重載方法 |方法|說明|調用方法| |---|---|---| |__init__|構造函數|`a=Person()`| |__del__|析構方法|`del object` | |__repr__||`repr(obj)`| |__call__(self, value)|函數形式調用|`object("hello")`| |__str__|字符串調用|`str(obj)`,`print(obj)`| |__getitem__(self,index) | 索引取值 |`a[12]`| |__setitem__(self,k,v) | 索引賦值 |`a[12]='asd'`| |__getattr__(self,name) | 訪問不存在的屬性 |`a.x`| |__setattr__(self,name,value) |設置不存在的屬性 |`a.x='hello`| |__iter__(self)/__next__(self) |迭代調用 || >1.` __repr__`與`__str__` 后者傾向于把類當作字符串打印,一般會定義`__repr__ = __str__` ### 比較運算符號 |方法|說明|調用方法| |---|---|---| |__cmp__(self,other) |對象比較 |`cmp(obj, x)`| |__eq__(self,other) |相等判斷 |s==s| |__lt__(self,other) |小于判定 |s<3| |__gt__(self,other) |于判定 |s>3| `__iter__`/與`__next__`的配合 ``` class Next(object): def __init__(self, data=1): self.data = data def __iter__(self): return self def __next__(self): if self.data<5: self.data +=1 return self.data else: raise StopIteration for i in Next(3): print(i) ``` `__cmp__`方法實現 ``` class Student(object): def __init__(self, name, score): self.name = name self.score = score def __cmp__(self, s): if self.score >s.score: return -1 elif self.score<s.score: return 1 else: if self.name > s.name: return 1 elif self.name<s.name: return -1 else: ?return 0 #實現 L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)] print sorted(L) #[(Alice: 99), (Tim: 99), (Bob: 88) ``` ### 數字運算符 |方法|說明| |---|---| |__add__(self,other) |`object+1`,`other值為1` | |__sub__(self,other) |`object-1` | |__mul__(self,other) |`object*2` | |__div__(self,other) |`object/2` | ### 邏輯符 |方法|說明| |---|---| |__or__(self,other) |`object+1`,`other值為1` | |__and__(self,other) |`object-1` |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看