Created
March 14, 2024 08:15
-
-
Save dicenull/024d452258d74f373d2979c689abbcd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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