<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國際加速解決方案。 廣告
                # [X分鐘速成Y](http://learnxinyminutes.com/) ## 其中 Y=dart 源代碼下載:?[learndart-cn.dart](http://learnxinyminutes.com/docs/files/learndart-cn.dart) Dart 是編程語言王國的新人。 它借鑒了許多其他主流語言,并且不會偏離它的兄弟語言 JavaScript 太多。 就像 JavaScript 一樣,Dart 的目標是提供良好的瀏覽器集成。 Dart 最有爭議的特性必然是它的可選類型。 ~~~ import "dart:collection"; import "dart:math" as DM; // 歡迎進入15分鐘的 Dart 學習。 http://www.dartlang.org/ // 這是一個可實際執行的向導。你可以用 Dart 運行它 // 或者在線執行! 可以把代碼復制/粘貼到這個網站。 http://try.dartlang.org/ // 函數聲明和方法聲明看起來一樣。 // 函數聲明可以嵌套。聲明使用這種 name() {} 的形式, // 或者 name() => 單行表達式; 的形式。 // 右箭頭的聲明形式會隱式地返回表達式的結果。 example1() { example1nested1() { example1nested2() => print("Example1 nested 1 nested 2"); example1nested2(); } example1nested1(); } // 匿名函數沒有函數名。 example2() { example2nested1(fn) { fn(); } example2nested1(() => print("Example2 nested 1")); } // 當聲明函數類型的參數的時候,聲明中可以包含 // 函數參數需要的參數,指定所需的參數名即可。 example3() { example3nested1(fn(informSomething)) { fn("Example3 nested 1"); } example3planB(fn) { // 或者不聲明函數參數的參數 fn("Example3 plan B"); } example3nested1((s) => print(s)); example3planB((s) => print(s)); } // 函數有可以訪問到外層變量的閉包。 var example4Something = "Example4 nested 1"; example4() { example4nested1(fn(informSomething)) { fn(example4Something); } example4nested1((s) => print(s)); } // 下面這個包含 sayIt 方法的類聲明,同樣有一個可以訪問外層變量的閉包, // 就像前面的函數一樣。 var example5method = "Example5 sayIt"; class Example5Class { sayIt() { print(example5method); } } example5() { // 創建一個 Example5Class 類的匿名實例, // 并調用它的 sayIt 方法。 new Example5Class().sayIt(); } // 類的聲明使用這種形式 class name { [classBody] }. // classBody 中可以包含實例方法和變量, // 還可以包含類方法和變量。 class Example6Class { var example6InstanceVariable = "Example6 instance variable"; sayIt() { print(example6InstanceVariable); } } example6() { new Example6Class().sayIt(); } // 類方法和變量使用 static 關鍵詞聲明。 class Example7Class { static var example7ClassVariable = "Example7 class variable"; static sayItFromClass() { print(example7ClassVariable); } sayItFromInstance() { print(example7ClassVariable); } } example7() { Example7Class.sayItFromClass(); new Example7Class().sayItFromInstance(); } // 字面量非常方便,但是對于在函數/方法的外層的字面量有一個限制, // 類的外層或外面的字面量必需是常量。 // 字符串和數字默認是常量。 // 但是 array 和 map 不是。他們需要用 "const" 聲明為常量。 var example8A = const ["Example8 const array"], example8M = const {"someKey": "Example8 const map"}; example8() { print(example8A[0]); print(example8M["someKey"]); } // Dart 中的循環使用標準的 for () {} 或 while () {} 的形式, // 以及更加現代的 for (.. in ..) {} 的形式, 或者 // 以 forEach 開頭并具有許多特性支持的函數回調的形式。 var example9A = const ["a", "b"]; example9() { for (var i = 0; i < example9A.length; i++) { print("Example9 for loop '${example9A[i]}'"); } var i = 0; while (i < example9A.length) { print("Example9 while loop '${example9A[i]}'"); i++; } for (var e in example9A) { print("Example9 for-in loop '${e}'"); } example9A.forEach((e) => print("Example9 forEach loop '${e}'")); } // 遍歷字符串中的每個字符或者提取其子串。 var example10S = "ab"; example10() { for (var i = 0; i < example10S.length; i++) { print("Example10 String character loop '${example10S[i]}'"); } for (var i = 0; i < example10S.length; i++) { print("Example10 substring loop '${example10S.substring(i, i + 1)}'"); } } // 支持兩種數字格式 int 和 double 。 example11() { var i = 1 + 320, d = 3.2 + 0.01; print("Example11 int ${i}"); print("Example11 double ${d}"); } // DateTime 提供了日期/時間的算法。 example12() { var now = new DateTime.now(); print("Example12 now '${now}'"); now = now.add(new Duration(days: 1)); print("Example12 tomorrow '${now}'"); } // 支持正則表達式。 example13() { var s1 = "some string", s2 = "some", re = new RegExp("^s.+?g\$"); match(s) { if (re.hasMatch(s)) { print("Example13 regexp matches '${s}'"); } else { print("Example13 regexp doesn't match '${s}'"); } } match(s1); match(s2); } // 布爾表達式必需被解析為 true 或 false, // 因為不支持隱式轉換。 example14() { var v = true; if (v) { print("Example14 value is true"); } v = null; try { if (v) { // 不會執行 } else { // 不會執行 } } catch (e) { print("Example14 null value causes an exception: '${e}'"); } } // try/catch/finally 和 throw 語句用于異常處理。 // throw 語句可以使用任何對象作為參數。 example15() { try { try { throw "Some unexpected error."; } catch (e) { print("Example15 an exception: '${e}'"); throw e; // Re-throw } } catch (e) { print("Example15 catch exception being re-thrown: '${e}'"); } finally { print("Example15 Still run finally"); } } // 要想有效地動態創建長字符串, // 應該使用 StringBuffer。 或者 join 一個字符串的數組。 example16() { var sb = new StringBuffer(), a = ["a", "b", "c", "d"], e; for (e in a) { sb.write(e); } print("Example16 dynamic string created with " "StringBuffer '${sb.toString()}'"); print("Example16 join string array '${a.join()}'"); } // 字符串連接只需讓相鄰的字符串字面量挨著, // 不需要額外的操作符。 example17() { print("Example17 " "concatenate " "strings " "just like that"); } // 字符串使用單引號或雙引號做分隔符,二者并沒有實際的差異。 // 這種靈活性可以很好地避免內容中需要轉義分隔符的情況。 // 例如,字符串內容里的 HTML 屬性使用了雙引號。 example18() { print('Example18 <a href="etc">' "Don't can't I'm Etc" '</a>'); } // 用三個單引號或三個雙引號表示的字符串 // 可以跨越多行,并且包含行分隔符。 example19() { print('''Example19 <a href="etc"> Example19 Don't can't I'm Etc Example19 </a>'''); } // 字符串可以使用 $ 字符插入內容。 // 使用 $ { [expression] } 的形式,表達式的值會被插入到字符串中。 // $ 跟著一個變量名會插入變量的值。 // 如果要在字符串中插入 $ ,可以使用 \$ 的轉義形式代替。 example20() { var s1 = "'\${s}'", s2 = "'\$s'"; print("Example20 \$ interpolation ${s1} or $s2 works."); } // 可選類型允許作為 API 的標注,并且可以輔助 IDE, // 這樣 IDE 可以更好地提供重構、自動完成和錯誤檢測功能。 // 目前為止我們還沒有聲明任何類型,并且程序運行地很好。 // 事實上,類型在運行時會被忽略。 // 類型甚至可以是錯的,并且程序依然可以執行, // 好像和類型完全無關一樣。 // 有一個運行時參數可以讓程序進入檢查模式,它會在運行時檢查類型錯誤。 // 這在開發時很有用,但是由于增加了額外的檢查會使程序變慢, // 因此應該避免在部署時使用。 class Example21 { List<String> _names; Example21() { _names = ["a", "b"]; } List<String> get names => _names; set names(List<String> list) { _names = list; } int get length => _names.length; void add(String name) { _names.add(name); } } void example21() { Example21 o = new Example21(); o.add("c"); print("Example21 names '${o.names}' and length '${o.length}'"); o.names = ["d", "e"]; print("Example21 names '${o.names}' and length '${o.length}'"); } // 類的繼承形式是 class name extends AnotherClassName {} 。 class Example22A { var _name = "Some Name!"; get name => _name; } class Example22B extends Example22A {} example22() { var o = new Example22B(); print("Example22 class inheritance '${o.name}'"); } // 類也可以使用 mixin 的形式 : // class name extends SomeClass with AnotherClassName {}. // 必需繼承某個類才能 mixin 另一個類。 // 當前 mixin 的模板類不能有構造函數。 // Mixin 主要是用來和輔助的類共享方法的, // 這樣單一繼承就不會影響代碼復用。 // Mixin 聲明在類定義的 "with" 關鍵詞后面。 class Example23A {} class Example23Utils { addTwo(n1, n2) { return n1 + n2; } } class Example23B extends Example23A with Example23Utils { addThree(n1, n2, n3) { return addTwo(n1, n2) + n3; } } example23() { var o = new Example23B(), r1 = o.addThree(1, 2, 3), r2 = o.addTwo(1, 2); print("Example23 addThree(1, 2, 3) results in '${r1}'"); print("Example23 addTwo(1, 2) results in '${r2}'"); } // 類的構造函數名和類名相同,形式為 // SomeClass() : super() {}, 其中 ": super()" 的部分是可選的, // 它用來傳遞參數給父類的構造函數。 class Example24A { var _value; Example24A({value: "someValue"}) { _value = value; } get value => _value; } class Example24B extends Example24A { Example24B({value: "someOtherValue"}) : super(value: value); } example24() { var o1 = new Example24B(), o2 = new Example24B(value: "evenMore"); print("Example24 calling super during constructor '${o1.value}'"); print("Example24 calling super during constructor '${o2.value}'"); } // 對于簡單的類,有一種設置構造函數參數的快捷方式。 // 只需要使用 this.parameterName 的前綴, // 它就會把參數設置為同名的實例變量。 class Example25 { var value, anotherValue; Example25({this.value, this.anotherValue}); } example25() { var o = new Example25(value: "a", anotherValue: "b"); print("Example25 shortcut for constructor '${o.value}' and " "'${o.anotherValue}'"); } // 可以在大括號 {} 中聲明命名參數。 // 大括號 {} 中聲明的參數的順序是隨意的。 // 在中括號 [] 中聲明的參數也是可選的。 example26() { var _name, _surname, _email; setConfig1({name, surname}) { _name = name; _surname = surname; } setConfig2(name, [surname, email]) { _name = name; _surname = surname; _email = email; } setConfig1(surname: "Doe", name: "John"); print("Example26 name '${_name}', surname '${_surname}', " "email '${_email}'"); setConfig2("Mary", "Jane"); print("Example26 name '${_name}', surname '${_surname}', " "email '${_email}'"); } // 使用 final 聲明的變量只能被設置一次。 // 在類里面,final 實例變量可以通過常量的構造函數參數設置。 class Example27 { final color1, color2; // 更靈活一點的方法是在冒號 : 后面設置 final 實例變量。 Example27({this.color1, color2}) : color2 = color2; } example27() { final color = "orange", o = new Example27(color1: "lilac", color2: "white"); print("Example27 color is '${color}'"); print("Example27 color is '${o.color1}' and '${o.color2}'"); } // 要導入一個庫,使用 import "libraryPath" 的形式,或者如果要導入的是 // 核心庫使用 import "dart:libraryName" 。還有一個稱為 "pub" 的包管理工具, // 它使用 import "package:packageName" 的約定形式。 // 看下這個文件頂部的 import "dart:collection"; 語句。 // 導入語句必需在其它代碼聲明之前出現。IterableBase 來自于 dart:collection 。 class Example28 extends IterableBase { var names; Example28() { names = ["a", "b"]; } get iterator => names.iterator; } example28() { var o = new Example28(); o.forEach((name) => print("Example28 '${name}'")); } // 對于控制流語句,我們有: // * 必需帶 break 的標準 switch 語句 // * if-else 和三元操作符 ..?..:.. // * 閉包和匿名函數 // * break, continue 和 return 語句 example29() { var v = true ? 30 : 60; switch (v) { case 30: print("Example29 switch statement"); break; } if (v < 30) { } else if (v > 30) { } else { print("Example29 if-else statement"); } callItForMe(fn()) { return fn(); } rand() { v = new DM.Random().nextInt(50); return v; } while (true) { print("Example29 callItForMe(rand) '${callItForMe(rand)}'"); if (v != 30) { break; } else { continue; } // 不會到這里。 } } // 解析 int,把 double 轉成 int,或者使用 ~/ 操作符在除法計算時僅保留整數位。 // 讓我們也來場猜數游戲吧。 example30() { var gn, tooHigh = false, n, n2 = (2.0).toInt(), top = int.parse("123") ~/ n2, bottom = 0; top = top ~/ 6; gn = new DM.Random().nextInt(top + 1); // +1 because nextInt top is exclusive print("Example30 Guess a number between 0 and ${top}"); guessNumber(i) { if (n == gn) { print("Example30 Guessed right! The number is ${gn}"); } else { tooHigh = n > gn; print("Example30 Number ${n} is too " "${tooHigh ? 'high' : 'low'}. Try again"); } return n == gn; } n = (top - bottom) ~/ 2; while (!guessNumber(n)) { if (tooHigh) { top = n - 1; } else { bottom = n + 1; } n = bottom + ((top - bottom) ~/ 2); } } // 程序的唯一入口點是 main 函數。 // 在程序開始執行 main 函數之前,不期望執行任何外層代碼。 // 這樣可以幫助程序更快地加載,甚至僅惰性加載程序啟動時需要的部分。 main() { print("Learn Dart in 15 minutes!"); [example1, example2, example3, example4, example5, example6, example7, example8, example9, example10, example11, example12, example13, example14, example15, example16, example17, example18, example19, example20, example21, example22, example23, example24, example25, example26, example27, example28, example29, example30 ].forEach((ef) => ef()); } ~~~ ## 延伸閱讀 Dart 有一個綜合性網站。它涵蓋了 API 參考、入門向導、文章以及更多, 還包括一個有用的在線試用 Dart 頁面。 http://www.dartlang.org/ http://try.dartlang.org/
                  <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>

                              哎呀哎呀视频在线观看