Created
September 28, 2022 02:46
-
-
Save matanlurey/9ab8e0020aab9592624f9c9db4548674 to your computer and use it in GitHub Desktop.
An example of abstract fields in Dart.
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
// An interface that defines two mutable fields. | |
abstract class Ball { | |
abstract int x; | |
abstract int y; | |
} | |
// Implements the fields by initializing in constructor. | |
class BallImplConstructor implements Ball { | |
@override | |
int x; | |
@override | |
int y; | |
BallImplConstructor(this.x, this.y); | |
} | |
// Implements the field by intitializing with values. | |
class BallImplDefault implements Ball { | |
@override | |
var x = 0; | |
@override | |
var y = 0; | |
} | |
// Implements the field by promising it will be initialized. | |
// | |
// This is pretty clearly the worst option unless there are some lifecycle | |
// requirements we've not aware of, like initializing these values in a | |
// another function. | |
class BallImplLate implements Ball { | |
@override | |
late int x; | |
@override | |
late int y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment