Classes, Enums, Extensions
Classes
Fields, Constructor, Method
A class groups data and behavior. Rectangle(this.width, this.height) is the constructor shorthand.
Program
Play the program to build a rectangle and call its area method.
classes.dart
class Rectangle {
final int width;
final int height;
Rectangle(this.width, this.height);
int area() => width * height;
}
void main() {
var rect = Rectangle(4, 3);
print(rect.area());
}
class
`class Rectangle` groups fields and methods.
this.x constructor
`Rectangle(this.width, this.height)` assigns straight to fields.
arrow method
`=>` is a one-expression method body.