Skip to content

Instantly share code, notes, and snippets.

@dicenull
Created March 14, 2024 08:15
Show Gist options
  • Save dicenull/9250b09c51cfbbe1410f6b7b19676ef1 to your computer and use it in GitHub Desktop.
Save dicenull/9250b09c51cfbbe1410f6b7b19676ef1 to your computer and use it in GitHub Desktop.
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