目錄
1. 注釋
1.1?單行注釋
1.2 多行注釋
1.3?文檔注釋
2. 函數
2.1?定義
2.2 可選參數
2.3 可選參數 默認值
2.4?命名參數 默認值
2.5?函數內定義
2.6 Funcation 返回函數對象
2.7?匿名函數
2.8 作用域
3. 操作符
3.1 操作符表
3.2 算術操作符
3.3?相等相關的操作符
3.4?類型判定操作符
3.5?條件表達式
3.6?位和移位操作符
3.7?級聯操作符
3.8?其他操作符
4.?流程控制
5.?異常
錯誤的兩種類型
5.1?Exception 類
5.2?Error 類
5.3?拋出錯誤
5.4?捕獲錯誤
5.5?重新拋出錯誤以及finally
5.6?自定義異常
1. 注釋
1.1?單行注釋
// Symbol libraryName = new Symbol('dart.core');
1.2 多行注釋
/*
* Symbol
*
Symbol libraryName = new Symbol('dart.core'); MirrorSystem mirrorSystem = currentMirrorSystem(); LibraryMirror libMirror = mirrorSystem.findLibrary(libraryName); libMirror.declarations.forEach((s, d) => print('$s - $d'));
*/
一般用在需要說明 類 函數 功能 輸入 輸出
1.3?文檔注釋
/// `main` 函數
///
/// 符號
/// 枚舉
///
void main() {
? ...
}
類、函數 請用 /// 方式定義,后期導出文檔有幫助
2. 函數
2.1?定義
int add(int x) {return x + 1;
}調用
add(1);
2.2 可選參數
int add(int x, [int? y, int? z]) {if (y == null) {y = 1;}if (z == null) {z = 1;}return x + y + z;
}調用
add(1, 2);
可選參數:
使用方括號?[]
?來定義可選參數?y
?和?z
。調用函數時可以選擇傳入或者不傳入這兩個參數。
可空類型:
int?
?表示?int
?類型的值可以是?null
。如果沒有使用?int?
,那么 Dart 不允許把?null
?賦值給該參數。
默認值邏輯:
在函數內部,通過?if (y == null)
?和?if (z == null)
?檢查?y
?和?z
?是否為?null
,如果是則賦值為默認的?1
。
2.3 可選參數 默認值
int add(int x, [int y = 1, int z = 2]) {return x + y;
}調用
int(1, 2);
就是cpp的缺省值
2.4?命名參數 默認值
int add({int x = 1, int y = 1, int z = 1}) {return x + y + z;
}調用
int(x: 1, y: 2);
這個會比可選參數更靈活,可以調用的時候再指定傳參
2.5?函數內定義
void main(){int add(int x){return x + x;}print(add(1));
}
cpp的就是lambda
2.6 Funcation 返回函數對象
Function makeAdd(int x) {return (int y) => x + y;
}調用
var add = makeAdd(1);
print(add(5));
這里是add接收了一個函數對象,然后在print里面調用了返回的函數對象
2.7?匿名函數
下面代碼定義了只有一個參數 item 且沒有參數類型的匿名方法。 List 中的每個元素都會調用這個函數,打印元素位置和值的字符串:
const list = ['apples', 'bananas', 'oranges'];
list.forEach((item) {print('${list.indexOf(item)}: $item');
});
箭頭函數 如果只有一個表達式
list.forEach((item) => print('${list.indexOf(item)}: $item'));
2.8 作用域
下面是一個嵌套函數中變量在多個作用域中的示例:
bool topLevel = true;void main() {var insideMain = true;void myFunction() {var insideFunction = true;void nestedFunction() {var insideNestedFunction = true;assert(topLevel);assert(insideMain);assert(insideFunction);assert(insideNestedFunction);}}
}
注意 nestedFunction() 函數可以訪問包括頂層變量在內的所有的變量。簡單說就是被包含的作用域內可以訪問包含其的外部變量
3. 操作符
3.1 操作符表
優先級順序?上面左邊
?優先級高于?右邊下面
3.2 算術操作符
3.3?相等相關的操作符
3.4?類型判定操作符
int a = 123;String b = 'ducafecat';String c = 'abc';print(a as Object);print(b is String);print(c is! String);
3.5?條件表達式
bool isFinish = true;String txtVal = isFinish ? 'yes' : 'no';bool isFinish;isFinish = isFinish ?? false;orisFinish ??= false;
3.6?位和移位操作符
3.7?級聯操作符
StringBuffer sb = StringBuffer();sb..write('hello')..write('word')..write('\n')..writeln('doucafecat');
如果沒有級聯操作符,就需要 sb.write(...);sb.write(...);....
3.8?其他操作符
String a;print(a?.length);
4.?流程控制
if else;for;while;do while;switch case;break;continue;相信這些就不用博主多說了哈
值得一提的是 continue 有一點不一樣
continue 跳轉指定位置(就像cpp的go to,新增了這個功能)
String command = "close";
switch (command) {case "open":print("open");break;case "close":print("close");continue doClear;case "close2":print("close2");continue doClear;doClear:case "doClose":print("doClose");break;default:print("other");
}
5.?異常
錯誤的兩種類型
5.1?Exception 類
Exception class
可以捕獲,可以安全處理
5.2?Error 類
Error class
一般用在不可恢復,容易崩潰的情況
5.3?拋出錯誤
// Exception 對象throw new FormatException('這是一個格式錯誤提示');// Error 對象throw new OutOfMemoryError();// 任意對象throw '這是一個異常';
5.4?捕獲錯誤
try {// throw new FormatException('這是一個格式錯誤提示');throw new OutOfMemoryError();} on OutOfMemoryError {print('沒有內存了');} catch (e) {print(e);}
5.5?重新拋出錯誤以及finally
try {throw new OutOfMemoryError();} on OutOfMemoryError {print('沒有內存了');rethrow;} catch (e) {print(e);} finally {print('end');}
try里面是拋出錯誤,on是捕獲特定錯誤,catch是捕獲所有錯誤,retgrow是重新拋出異常,finally是不管有沒有錯都會走的!
5.6?自定義異常
implements關鍵字可以自定義異常
class DioError implements Exception {DioError(this.message, this.type);final String message;final String type;@overrideString toString() {return 'DioError{message: $message, type: $type}';}
}void main(List<String> args) {throw DioError("error", "type");
}
創作不易,希望讀者三連支持?💖
贈人玫瑰,手有余香?💖