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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## CLASSES 通過 class 關鍵字定義一個類: ~~~ class Person { var clothes: String var shoes: String } ~~~ 但是上面有個問題,Swift 不允許創建未被正確初始化的變量。解決方法有三種: 1. 將這兩個變量定義為 Optional。(這會致使代碼中出現大量不必要的 Optional 類型) 2. 給變量附上一個初始值。(這行得通,但這有點浪費,除非這個初始值一定會被使用) 3. 自己寫一個初始化方法。(最佳) 通過創建一個 init() 方法來實現自定義初始化方法: ~~~ class Person { var clothes: String var shoes: String init(clothes: String, shoes: String) { self.clothes = clothes self.shoes = shoes //init方法不需要添加 func 關鍵字 } } ~~~ 實例化一個類: ~~~ var taylor = Person(name: "Taylor", age: 25) ~~~ 繼承一個類: ~~~ class Singer: Person { } ~~~ 在 Swift 中,想要在繼承類(子類)中覆蓋父類的方法,需要使用 override 關鍵字: class Singer: Person { override func sing() { print(“Trucks, girls, and liquor”) } } 通過 super 關鍵字調用父類方法: ~~~ init(name: String, age: Int, noiseLevel: Int) { self.noiseLevel = noiseLevel super.init(name: name, age: age) } ~~~ * 在 Swift 中,類和結構體有點相似,都可以擁有屬性和方法,區別在于結構體是值拷貝,這意味著改變拷貝值不會改變原來的值,而類是指針拷貝,拷貝的變量會指向相同的實例,見下例: ~~~ // Value type example struct S { var data: Int = -1 } var a = S() var b = a // a is copied to b a.data = 42 // Changes a, not b println("\(a.data), \(b.data)") // prints "42, -1" // Reference type example class C { var data: Int = -1 } var x = C() var y = x // x is copied to y x.data = 42 // changes the instance referred to by x (and y) println("\(x.data), \(y.data)") // prints "42, 42" ~~~
                  <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>

                              哎呀哎呀视频在线观看