2023-05-21 dart语法

2023-05-21  本文已影响0人  coder_feng

变量与类型

Object dynamic
不同点 只能使用Object属性与方法 编译器会提供所有可能的组合
相同点 可以在后期改变赋值类型 可以在后期改变赋值类型
dynamic a = 'str';
print(a.length);//正确

Object a = 'str';
print(a.length);//错误

dynamic 很容易引起运行时错误

左对齐 右对齐 居中对齐
单元格 单元格 单元格
单元格 单元格 单元格

控制流语句

函数

void main() {
  var calPower = (n) {
    return n * n;
  };
print(calPowder(5));
}
void main() {
  printPower(calPower(8));
}
void printPowder(var power) {
  print(power);
}
int calPower(int n) {
  return n * n;
}
void main() {
  int m1 = calMultiplication(4);
print(m1);//16

int m2 = calMultiplication(4,5);
print(m2);
}

int calMultiplication(int 1,[int 5]) {
  if(b != null) {
    return a * b;
  }else {
      return a * a;
    }
}
void main() {
  int m1 = calMultiplication(4);
print(m1);//16

int m2 = calMultiplication(4,b: 5);
print(m2);
}

int calMultiplication(int 1,[int 5]) {
  if(b != null) {
    return a * b;
  }else {
      return a * a;
    }
}

类和对象

class Person {
  int age;
  String name;

  Person(int age, String name) {
    this.age = age;
    this.name = name;  
  }

  int howOld() {
    return age;
}
class Person {
  int age;
  String name;

  int howOld() {
    reutrn age;
  }
}
class Person {
    int age;
    String name;
    Person(this.age,this.name);
    Person.fromList(List<dynamic>list) {
        age = list[0]
        name = list[1]
    }

    int howOld() {
        return age;
    }
}

void main() {
    Person a = new Person(20,'小A');
    print(a.howOld());

    Person b = new Person.fromList([30,'小B']);
    print(b.howOld());
}

继承与多态

class Person {
    int age;
    String name;
    Person(this.age,this.name);
    Person.fromList(List<dynamic>list) {
        age = list[0]
        name = list[1]
    }

    int howOld() {
        return age;
    }
}

class Male extends Person {
    Male(int age, String name) : super(age,name);

    @override
    int howOld() {
        return age + 1;
    }
}

void main() {
    Person a = new Person(20,'小A');
    print(a.howOld());

    Person b = new Person.fromList([30,'小B']);
    print(b.howOld());

    Person c = new Male(20,'小C');
    print(c.howOld());
}

抽象类与接口

abstract class Person {
    String getName();

    int getAge() {
        return 20;
    }
}

class Male extends Person {
    String getName() {
        return '小A';
    }
}

void main() {
    Person a = new Person();

    Person b = new Male();
    print(b.getName());
}
abstract class Person {
    String getName();

    int getAge() {
        return 20;
    }
}

class Male extends Person {
    String getName() {
        return '小A';
    }
}

class AA implements Male {
    @override 
    int getAge() {
        return 20;
    }

    @override
    String getName() {
        return 'AA';
    }
}

静态与权限

静态 非静态
修饰 static
属于 对象
访问 成员、静态成员、成员函数、静态成员函数 只能访问静态成员和静态成员函数

异步支持

异步运算async -> 延迟队列(await) -> 等待结果(Future) 
void main() {
  String data = '初始化data';
  getData(data);
}

getData(String data) async {
  print(data); //初始化data

  data = await Future.delayed(Duration(seconds: 2),() {
    return '网络数据';
  }) as String;
  
  print(data);//网络数据
}
Future.then

void main() {
  Future<String> future = getData();
  future.then((data) {
    print(data);
  });
}

Future<String> getData() async {
  return await Future.delayed(Duration(seconds: 2),() {
    return '网络数据';
  });
}
void main() {
  Future<String> future = getData();
  future.then((data) {
    print(data);
  }).catchError((e) {
      print(e);
  });
}

Future<String> getData() async {
  return await Future.delayed(Duration(seconds: 2),() {
    throw AssertionError("Error");
  });
}

void main() {
  Future<String> future = getData();
  future.then((data) {
    print(data);
  },onError: (e) {
      print(e);
  });
}

Future<String> getData() async {
  return await Future.delayed(Duration(seconds: 2),() {
    throw AssertionError("Error");
  });
}
void main() {
  Future.wait([
    Future.delayed(new Duration(seconds: 2),() {
      return "hellow";
    }),
    Future.delayed(new Duration(seconds: 2),() {
      return " worrld";
    })
  ]).then((reuslts) {
    print(reuslts[0] + reuslts[1]);
  }).catchError((e) {
    print(e);
  });
}

文档学习

上一篇下一篇

猜你喜欢

热点阅读