<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國際加速解決方案。 廣告
                Typedef 是一個定義用于匿名類型類型檢查的類型的構造。Typedef 聲明必須使用如下語法: ~~~ typedef Name = typedeclaration ~~~ Name必須遵守和類名相同的命名規則, typedeclaration 是類型定義的簽名。 最常見的用法關于 typedef 是給一個匿名對象以形式的表現。 ~~~ typedef Color = { r: Int, g: Int, b: Int} ~~~ 這樣使用: ~~~ var white : Color = { r: 255, g: 255, b: 255 }; ~~~ typedef hi可以用來定義其他類型,如函數或者已有定義的快捷方式。 ~~~ typedef GenericFunction < T > = Void - > T typedef IntArray = Array < Int > typedef P = Person typedef TS = ThreeState enum ThreeState { Checked; Unchecked; Indeterminated; } class Person { public var name : String; public function new() { } } ~~~ 當聲明一個匿名對象的類型,有兩種語法的可能: ~~~ typedef A = { var x : Float; var y : Float; } typedef B = { x : Float, y : Float } ~~~ 這種情況下,聲明是等價的,但是你可以發現不同于一個包含至少有一個函數定義的 typedef : ~~~ typedef A = { function say(text : String) : String; } typedef B = { say : String - > String } ~~~ 在這種情況,兩個幾乎一樣,唯一區別是函數參數在第一個聲明被命名,而第二個則匿名。另外的不同是第一個格式里可以添加 private/public 訪問修飾符(默認總是 public )。 typedef 還可以有類型參數,和類跟接口類似。 ~~~ typedef Leaf < T > = { parent : Node < T > , data : T } ~~~ typedef 語法是一個重要的Haxe語言特性。 typedef 在很多方面是一個靈活的接口替代選擇。typedef驗證只在編譯時發生并且知識結構上檢查;編譯器檢查傳遞的對象有需要的字段但是不假設對象的類型定義。這創造了有趣的可能性,下面的例子,handle() 函數接受一個HashNme類型的參數。HasName是一個 typedef,定義它的類型的值必須有一個String類型的name字段。好處是不只是匿名對象可以滿足這個結構要求,而類的實例同樣可以。 ~~~ class Person { public var name : String; public function new(n : String) { name = n; } } class Item { public var name : String; public var price : Float; public function new(n : String, p : Float) { name = n; price = p; } } typedef HasName = { name : String } class Main { public static function handle(o : HasName) { trace(o.name); } public static function main() { var person = new Person(“John”); var item = new Item(“Laptop PC”, 999.9); var user = { name : “Jane” }; handle(person); handle(item); handle(user); } } ~~~ 你可以簡化聲明移除 typedef 定義并且替換處理聲明通過如下行: ~~~ public static function handle(o : { name : String}) ~~~ typedef并不是嵌入到生成的輸出中,而是只用于類型在編譯時的檢查。他們執行了一種基于慣例的編程,開發人員需要構造帶有某些特征的結構但并不需要綁定到類似接口或者基類的形式定義。可以定義一個約定對于一個項目,而不用提供任何代碼讓同僚實現一個完整的應用。這可以在Web開發環境中帶來很大的好處,通常有許多庫可以幫助我們工作。 不僅使用在對象,而且任何類型定義都可以使用。舉個有趣的例子,有一個函數接收另一個函數作為參數;參數可能是一個內聯函數,一個對象方法,或者一個類靜態函數。這里有一個例子: ~~~ class StringDecoration { var prefix : String; public function new(prefix : String) { this.prefix = prefix; } public function decorate(s : String) { return prefix + s; } } class Main { public static function print(s : String, decorate : String -> String) { var o = decorate(s); trace(o); } public static function quote(s : String) { return ‘”’ + s + ‘”’; } public static function main() { var decorator = new StringDecoration(“- > “); var s = “John”; print(s, quote); // traces “John” print(s, decorator.decorate); // traces - > John print(s, function(s){ return “-- “ + s + “ --”; }); // traces -- John - } } ~~~ print()的第二個參數是一個typedef對一個接收一個字符串作為參數并返回一個String的函數。在 main() 方法,pirnt()函數用來傳遞靜態函數 quote(),然后類StringDecoration的實例函數 decorate(),最后一個內聯函數定義。 # 迭代器和可迭代性 * * * * * 在Haxe標準庫中,兩個非常常用的typedef是:Iterator<T> 和 Iterable<T>。 它們的定義如下: ~~~ typedef Iterator < T > = { function hasNext() : Bool; function next() : T; } typedef Iterable < T > = { function iterator() : Iterator < T > ; } ~~~ 因為在第三章已經了解過,這些結構被定義來處理對象集合,他們很容易的實現在類定義中。這個例子里,一個隨機的迭代器被定義。迭代器會返回一個n個隨機字符的序列。 ~~~ class RandomSequence { private var letters : Int; private var counter : Null < Int > ; public function new(letters : Int) { this.letters = letters; } public function hasNext() : Bool { if(counter == null) { // the iterator has to be initialized counter = 0; } if(counter < letters) { return true; } else { // before returning false, the counter variable must // be reset in case the instance has to be used again counter = null; return false; } } public function next() : String { counter++; return Std.chr(Std.int(Math.random() * 26)+ 65); } } ~~~ 代碼不顯式聲明,它是實現Iterator typedef 。這個隱式發生因為類定義正確的方法。這個類這樣使用: ~~~ class Main { static function main() { var sequence = new RandomSequence(10); for(letter in sequence) { trace(letter); } } } ~~~ 這個例子會輸出十次隨機字母。 在實踐中Haxe在背后做的死改變 for(序列中的字母)聲明: ~~~ while(sequence.hasNext()) { var letter = sequence.next(); // ... } ~~~ 這自動發生每次一個用在for語句中的對象符合 Iterator<T> typedef 。相同的處理是被保留對符合Iterable<T>的對象。這可以用在當對象可以返回一個結合但是有一個間接的中介。 Array<T>是 Iterable<T>,可以用來演示它的用法。下面的兩個聲明實際上是相同的;編譯器在內部改變第二個使其表現的像第一個。 ~~~ for(i in [1,2,4,8].iterator()) { trace(i); } for(i in [1,2,4,8]) { trace(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>

                              哎呀哎呀视频在线观看