Created with <3 with dartpad.dev.
Last active
March 27, 2023 04:00
-
-
Save topex-psy/468522de6bc586ae9a68c8e90ef6b4a7 to your computer and use it in GitHub Desktop.
Dart 4. Function, Class & Method
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
void main() { | |
double kuadrat(double n) { | |
return n * n; | |
} | |
print(kuadrat(20)); | |
var spacecraft = Spacecraft("Roket 101", launched: DateTime.tryParse("2022-01-02")); | |
spacecraft.launchDate = "2021-01-02"; | |
spacecraft.describe(); | |
} | |
class Spacecraft { | |
// properties | |
String name; | |
DateTime? launched; | |
// getter | |
int? get launchYear => launched?.year; | |
// setter | |
set launchDate(String date) { | |
launched = DateTime.tryParse(date); | |
} | |
// constructor | |
Spacecraft(this.name, {this.launched}) { | |
// di sini inisialisasi | |
} | |
//method | |
void describe() { | |
print('Spacecraft "$name" ${launched == null | |
? "belum meroket" | |
: "sudah meroket pada $launchYear"}'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment