<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 4.11\. Sorting 接口(interfaces)提供了一個簡單形式的多態(polymorphism). 他們把對象的定義和 如何實現的分開處理,允許相同的接口可以有不能的實現方法。 參考這個簡單的排序算法(sort algorithm)"progs/sort.go" ``` 13 func Sort(data Interface) { 14 for i := 1; i < data.Len(); i++ { 15 for j := i; j > 0 && data.Less(j, j-1); j-- { 16 data.Swap(j, j-1) 17 } 18 } 19 } ``` 我們要封裝這個排序(sort)的接口(interface)僅需要三個方法。 ``` 07 type Interface interface { 08 Len() int 09 Less(i, j int) bool 10 Swap(i, j int) 11 } ``` 我們可以用任何類型的"Sort"去實現"Len", "Less" 和 "Swap". 這個"sort"包里面 包含一些方法(methods). 下面是整型數組的代碼: ``` 33 type IntArray []int 35 func (p IntArray) Len() int { return len(p) } 36 func (p IntArray) Less(i, j int) bool { return p[i] < p[j] } 37 func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] } ``` 你看到的是一個沒有任何類型的"結構體"(non-struct type). 在你的包里面你可以定義 任何你想定義的類型. 現在用"progs/sortmain.go"程序進行測試,用"sort"包里面的排序函數進行排序。 ``` 12 func ints() { 13 data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586} 14 a := sort.IntArray(data) 15 sort.Sort(a) 16 if !sort.IsSorted(a) { 17 panic("fail") 18 } 19 } ``` 如果我們為sort提供一個新類型,我們就需要為這個類型實現三個方法,如下: ``` 30 type day struct { 31 num int 32 shortName string 33 longName string 34 } 36 type dayArray struct { 37 data []*day 38 } 40 func (p *dayArray) Len() int { return len(p.data) } 41 func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num } 42 func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] } ```
                  <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>

                              哎呀哎呀视频在线观看