Skip to content

Instantly share code, notes, and snippets.

@dicenull
Created March 14, 2024 08:15
Show Gist options
  • Save dicenull/024d452258d74f373d2979c689abbcd8 to your computer and use it in GitHub Desktop.
Save dicenull/024d452258d74f373d2979c689abbcd8 to your computer and use it in GitHub Desktop.
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);
/// `enum`のコンストラクタ
const Planet(
{required this.planetType, required this.moons, required this.hasRings});
/// 変数はすべて`final`になります
final PlanetType planetType;
final int moons;
final bool hasRings;
/// getterやメソッドを定義できます
bool get isGiant =>
planetType == PlanetType.gas || planetType == PlanetType.ice;
}
void main() {
final yourPlanet = Planet.venus;
if (!yourPlanet.isGiant) {
print('Your planet is not a "giant planet".');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment