Created with <3 with dartpad.dev.
Last active
September 21, 2022 12:43
-
-
Save Chralu/2b6bc99899c223a6ba331c91bc47e0b4 to your computer and use it in GitHub Desktop.
Flutter101 - Class simple
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 Person { | |
final String firstname; | |
final String lastname; | |
final double? weight; | |
// Constructeur avec des paramètres | |
// nommés. | |
Person({ | |
required this.firstname, | |
required this.lastname, | |
this.weight, | |
}); | |
// On peut aussi utiliser | |
// des paramètres positionnés | |
// Person( | |
// this.firstname, | |
// this.lastname, | |
// this.weight, | |
// ); | |
// L'annotation `@override` indique qu'il s'agit d'une | |
// surcharge de méthode. | |
@override | |
String toString() { | |
if (weight == null) return "$firstname $lastname"; | |
return "$firstname $lastname ($weight kg)"; | |
} | |
} | |
void main() { | |
final johnDoe = Person( | |
firstname: "John", | |
lastname: "Doe", | |
); | |
print(johnDoe); | |
final johnnyBegood = Person( | |
firstname: "Johnny", | |
lastname: "Begood", | |
weight: 70, | |
); | |
print(johnnyBegood); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment