Created
March 14, 2024 08:15
-
-
Save dicenull/9250b09c51cfbbe1410f6b7b19676ef1 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
class Spacecraft { | |
String name; | |
DateTime? launchDate; | |
int? get launchYear => launchDate?.year; | |
Spacecraft(this.name, this.launchDate) {} | |
Spacecraft.unlaunched(String name) : this(name, null); | |
void describe() { | |
print('Spacecraft: $name'); | |
var launchDate = this.launchDate; | |
if (launchDate != null) { | |
int years = DateTime.now().difference(launchDate).inDays ~/ 365; | |
print('Launched: $launchYear ($years years ago)'); | |
} else { | |
print('Unlaunched'); | |
} | |
} | |
} | |
class Orbiter extends Spacecraft { | |
double altitude; | |
Orbiter(super.name, DateTime super.launchDate, this.altitude); | |
} | |
void main() { | |
final orbiter = Orbiter('Endeavour', DateTime(1992, 5, 7), 960); | |
orbiter.describe(); | |
print(orbiter.altitude); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment