docs: update dart.md (#145)

* doc: update dart.md

* doc: update dart.md
This commit is contained in:
Jefferson 2022-11-19 20:10:13 +08:00 committed by GitHub
parent ff3a5e18ae
commit 3e1fd804db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -462,6 +462,46 @@ class SmartPhone extends Phone {
_playGames();
}
}
```
枚举
-----
```dart
// 简单定义一个枚举类型
enum PlanetType { terrestrial, gas, ice }
// 定义一个行星复杂的枚举类型
enum Planet {
mercury(planetType: PlanetType.terrestrial, moons: 0, hasRings: false),
venus(planetType: PlanetType.terrestrial, moons: 0, hasRings: false),
uranus(planetType: PlanetType.ice, moons: 27, hasRings: true),
neptune(planetType: PlanetType.ice, moons: 14, hasRings: true);
// 定义一个构造函数
const Planet(
{required this.planetType, required this.moons, required this.hasRings});
// 声明枚举类型中的变量
final PlanetType planetType;
final int moons;
final bool hasRings;
// 实现枚举类型中的get 方法
bool get isGiant =>
planetType == PlanetType.gas || planetType == PlanetType.ice;
}
// 使用枚举类型
void main()
{
final yourPlanet = Planet.mercury;
if (!yourPlanet.isGiant) {
print('Your planet is not a "giant planet".');
}
}
```
异常