初探Dart编程语言

发表时间: 2023-09-30 09:42

函数

// 可选参数void userSettings({int age = 0, String name = ""}) {  print("name: $name age: $age");}// 必传参数void requiredFunc({required int age, String name = ""}) {  print("name: $name age: $age");}void main(List<String> args) {  requiredFunc(age: 10);}

异步编程 - Future

异步编程 — dart 单线程
与 Js Promise 类似, then 、async等
伪代码:

Future<int> future = getFuture();future.then(value => handleValue())  .catchError(error => handleError())  .whenComplete() => handlerComplete()

数据类型

  • Number
  • String
  • Boolean
  • List
  • Map
  • Runes
  • Symbols
int number = 15;// 类型://    Number//    String//    Boolean//    List//    Map//    Runes//    Symbolsvar list = [1, 2, 3];var list1 = const [1, 2, 3];void printList(ele) {  print(ele);}// 类型检测void judge() {  if (number is int) {    print("is number.");  } else {    print("is not number.");  }}// 级联赋值void cascade() {  String s = (new StringBuffer()        ..write('a ')        ..write('b ')        ..write('c '))      .toString();  print(s);}void main() {  cascade();}

继承、实现、混入

继承顺序: mixins -> extends -> implements

class Cat {  void show() {    print("cat.");  }}class Dog {  void show() {    print("dog.");  }}class Owner {  void show() {    print("owner.");  }}class Person1 extends Owner with Cat, Dog {  // 重写继承的方法  // void show() {  //   print("person1.");  // }  // 继承顺序: mixins -> extends -> implements}class Person2 extends Owner with Cat implements Dog {}void main(List<String> args) {  Person2()..show();}