Added zh-tw translation to Dart

This commit is contained in:
Bob 2020-01-30 17:19:48 +08:00
parent 7e9c52d4fd
commit e157a92b38

566
zh-tw/dart-tw.html.markdown Normal file
View File

@ -0,0 +1,566 @@
---
language: dart
lang: zh-tw
filename: learndart-tw.dart
contributors:
- ["Joao Pedrosa", "https://github.com/jpedrosa/"]
translators:
- ["Bob Lu", "https://github.com/LuPoYi/"]
---
Dart
JavaScript
JavaScript Dart
Dart
```javascript
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 example8Array = const ["Example8 const array"],
example8Map = const {"someKey": "Example8 const map"};
example8() {
print(example8Array[0]);
print(example8Map["someKey"]);
}
// Dart 使 for () {} while () {}
// for (.. in ..) {} ,
// forEach
var example9Array = const ["a", "b"];
example9() {
for (var i = 0; i < example9Array.length; i++) {
print("Example9 for loop '${example9Array[i]}'");
}
var i = 0;
while (i < example9Array.length) {
print("Example9 while loop '${example9Array[i]}'");
i++;
}
for (var e in example9Array) {
print("Example9 for-in loop '${e}'");
}
example9Array.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);
}
//
example14() {
var a = true;
if (a) {
print("Example14 true, a is $a");
}
a = null;
if (a) {
print("Example14 true, a is $a");
} else {
print("Example14 false, a is $a"); //
}
// null可以轉換成bool型
var b; // b是動態類型
b = "abc";
try {
if (b) {
print("Example14 true, b is $b");
} else {
print("Example14 false, b is $b");
}
} catch (e) {
print("Example14 error, b is $b"); //
}
b = null;
if (b) {
print("Example14 true, b is $b");
} else {
print("Example14 false, b is $b"); //
}
// null不能轉換成bool型
var c = "abc";
c = null;
//
// if (c) {
// print("Example14 true, c is $c");
// } else {
// print("Example14 false, c is $c");
// }
}
// 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);
}
}
//
// 使 [ ]
example31() {
findVolume31(int length, int breath, [int height]) {
print('length = $length, breath = $breath, height = $height');
}
findVolume31(10,20,30); //
findVolume31(10,20); //
}
//
// 使 { },
// :
//
//
example32() {
findVolume32(int length, int breath, {int height}) {
print('length = $length, breath = $breath, height = $height');
}
findVolume32(10,20,height:30); // &
findVolume32(10,20); //
}
//
//
// 使
example33() {
findVolume33(int length, int breath, {int height=10}) {
print('length = $length, breath = $breath, height = $height');
}
findVolume33(10,20,height:30); //
findVolume33(10,20); //
}
// 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 // dart formatter把所有項目都換行
].forEach((ef) => ef());
}
```
##
Dart API
Dart
* [https://www.dartlang.org](https://www.dartlang.org)
* [https://try.dartlang.org](https://try.dartlang.org)