<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國際加速解決方案。 廣告
                ## 10.5.1 定義 結構體可以包含一個或多個?**匿名(或內嵌)字段**,即這些字段沒有顯式的名字,只有字段的類型是必須的,此時類型也就是字段的名字。匿名字段本身可以是一個結構體類型,即?**結構體可以包含內嵌結構體**。 可以粗略地將這個和面向對象語言中的繼承概念相比較,隨后將會看到它被用來模擬類似繼承的行為。Go 語言中的繼承是通過內嵌或組合來實現的,所以可以說,在 Go 語言中,相比較于繼承,組合更受青睞。 考慮如下的程序: 示例 10.8 structs_anonymous_fields.go: ~~~ package main import "fmt" type innerS struct { in1 int in2 int } type outerS struct { b int c float32 int // anonymous field innerS //anonymous field } func main() { outer := new(outerS) outer.b = 6 outer.c = 7.5 outer.int = 60 outer.in1 = 5 outer.in2 = 10 fmt.Printf("outer.b is: %d\n", outer.b) fmt.Printf("outer.c is: %f\n", outer.c) fmt.Printf("outer.int is: %d\n", outer.int) fmt.Printf("outer.in1 is: %d\n", outer.in1) fmt.Printf("outer.in2 is: %d\n", outer.in2) // 使用結構體字面量 outer2 := outerS{6, 7.5, 60, innerS{5, 10}} fmt.Printf("outer2 is:", outer2) } ~~~ 輸出: ~~~ outer.b is: 6 outer.c is: 7.500000 outer.int is: 60 outer.in1 is: 5 outer.in2 is: 10 outer2 is:{6 7.5 60 {5 10}} ~~~ 通過類型?`outer.int`?的名字來獲取存儲在匿名字段中的數據,于是可以得出一個結論:在一個結構體中對于每一種數據類型只能有一個匿名字段。 ## [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/10.5.md#1052-內嵌結構體)10.5.2 內嵌結構體 同樣地結構體也是一種數據類型,所以它也可以作為一個匿名字段來使用,如同上面例子中那樣。外層結構體通過`outer.in1`?直接進入內層結構體的字段,內嵌結構體甚至可以來自其他包。內層結構體被簡單的插入或者內嵌進外層結構體。這個簡單的“繼承”機制提供了一種方式,使得可以從另外一個或一些類型繼承部分或全部實現。 另外一個例子: 示例 10.9 embedd_struct.go: ~~~ package main import "fmt" type A struct { ax, ay int } type B struct { A bx, by float32 } func main() { b := B{A{1, 2}, 3.0, 4.0} fmt.Println(b.ax, b.ay, b.bx, b.by) fmt.Println(b.A) } ~~~ 輸出: ~~~ 1 2 3 4 {1 2} ~~~ **練習 10.5**?anonymous_struct.go: 創建一個結構體,它有一個具名的 float 字段,2 個匿名字段,類型分別是 int 和 string。通過結構體字面量新建一個結構體實例并打印它的內容。 ## [](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/10.5.md#1053-命名沖突)10.5.3 命名沖突 當兩個字段擁有相同的名字(可能是繼承來的名字)時該怎么辦呢? 1. 外層名字會覆蓋內層名字,這提供了一種重載字段或方法的方式 2. 如果相同的名字在同一級別出現了兩次,如果這個名字被程序使用了,將會引發一個錯誤(不使用沒關系)。沒有辦法來解決這種問題引起的二義性,必須由程序員自己修正。 例子: ~~~ type A struct {a int} type B struct {a, b int} type C struct {A; B} var c C; ~~~ 規則 2:使用?`c.a`?是錯誤的,到底是?`c.A.a`?還是?`c.B.a`?呢?會導致編譯器錯誤:**ambiguous DOT reference c.a disambiguate with either c.A.a or c.B.a**。 ~~~ type D struct {B; b float32} var d D; ~~~ 規則1:使用?`d.b`?是沒問題的:它是 float32,而不是?`B`?的?`b`。如果想要內層的?`b`?可以通過?`d.B.b`?得到。
                  <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>

                              哎呀哎呀视频在线观看