<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之旅 廣告
                Haxe允許許多類型的參數化,就像類字段(第4章)和枚舉構造函數(第2.4.1節)。類型參數通過閉合的用逗號分隔的類型參數名尖括號來定義。一個簡單的例子來自于Haxe標準庫,就是 Array: > Haxe allows parametrization of a number of types, as well as class ?elds (4) and enum constructors (2.4.1). Type parameters are de?ned by enclosing comma-separated type parameter names in angle brackets <>. A simple example from the Haxe Standard Library is Array: ~~~ class Array<T> { function push(x : T) : Int; } ~~~ 每當一個Array的實例被創建,它的類型參數 T 成為一個 單形(第2.9節)。也就是說,它可以被綁定到任何類型,但是一次只有一個。這個綁定可以是顯式的發生,通過調用構造函數使用顯式的類型(new Array<String>()),或者隱式的發生,通過類型推斷(第3.6節),例如當我們調用arrayInstance.push("foo") 。 > Whenever an instance of Array is created, its type parameter T becomes a monomorph (2.9). That is, it can be bound to any type, but only one at a time. This binding can happen explicitly by invoking the constructor with explicit types (new Array<String>()) or implicitly by type inference (3.6), e.g. when invoking arrayInstance.push("foo"). 一個類的定義內部使用類型參數,這些類型參數都是非特定的類型。除非約束(第3.2.1節)被添加,編譯器必須假定類型參數可以被和任何類型一起使用。因此,不能訪問類型參數的字段或者轉換(第5.23)為一個類型參數類型。也不可能創建一個新的類型參數類型的實例,除非類型參數是泛型(第3.3節)并且被相應的約束。 > Inside the de?nition of a class with type parameters,these type parameters are an unspeci?c type. Unless constraints (3.2.1) are added, the compiler has to assume that the type parameters could be used with any type. As a consequence, it is not possible to access ?elds of type parameters or cast (5.23) to a type parameter type. It is also not possible to create a new instance of a type parameter type, unless the type parameter is generic (3.3) and constrained accordingly. 下面的表格顯示允許類型參數的地方: > The following table shows where type parameters are allowed: |位置|綁定在|注意| | -- | -- | -- | |Class|實例化|也可以被綁定到成員字段訪問上| |Enum|實例化|| |Enum構造函數|實例化|| |Function|調用中|允許用于方法和命名的局部 lvalue 函數| |Structure|實例化|| 使用函數類型參數被綁定到調用中,這樣一個類型參數(如果無約束)接受任何類型。然而,每次調用只有一個類型被接受。這可以在有多個參數的函數中被利用: > With function type parameters being bound upon invocation, such a type parameter (if unconstrained) accepts any type. However, only one type per invocation is accepted. This can be utilized if a function has multiple arguments: ~~~ class Main { static public function main() { equals(1, 1); // runtime message: bar should be foo equals("foo", "bar"); // compiler error: String should be Int equals(1, "foo"); } static function equals<T>(expected:T, actual:T) { if (actual != expected) { trace(’$actual should be $expected’); } } } ~~~ equals函數所有預期的和實際的參數都有一個類型 T 。這意味著對于每個equals的調用,這兩個參數必須是相同類型。編譯器承認第一次調用(兩種參數類型都是Int)和第二個調用(兩個參數都是String),但是第三次嘗試引發一個編譯器錯誤。 > Both arguments expected and actual of the equals function have type T. This implies that for each invocation of equals the two arguments must be of the same type. The compiler admits the ?rst call (both arguments being of Int) and the second call (both arguments being of String) but the third attempt causes a compiler error. **表達式語法中的類型參數** >[warning] **花絮**:表達式語法中的類型參數 我們經常遇到的問題,為什么一個方法使用類型參數不能被調用為 method<String>(x) 。編譯器給出的錯誤信息不是非常有幫助的。然而,有一個簡單的理由:上面的代碼如果 < 和 > 都是二元操作符,將被解析產生 (method< Sting)>(x) 。 >[warning] **Trivia**: Type parameters in expression syntax We often get the question why a method with type parameters cannot be called as method<String>(x). The error messages the compiler gives are not very helpful. However,there is a simple reason forthat: The above code is parsed as if both < and > were binary operators, yielding (method < String) > (x).
                  <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>

                              哎呀哎呀视频在线观看