A touch of class – how to use classes and objects
We saw in Chapter 1, Dart – A Modern Web Programming Language, how a class contains members such as properties, a constructor, and methods (refer to banking_v2.dart
). For those familiar with classes in Java or C#, it's nothing special and we can see already certain simplifications:
- The short constructor notation lets the parameter values flow directly into the properties:
BankAccount(this.owner, this.number, this.balance) { … }
- The keyword
this
is necessary here and refers to the actual object (being constructed), but it is rarely used elsewhere (only when there is a name conflict). Initialization of instance variables can also be done in the so-called initializer list, in this shorter version of the constructor:BankAccount(this.owner, this.number, this.balance): dateCreated = new DateTime.now();
- The variables are initialized after the colon (
:
) and are separated by a comma. You cannot use the keywordthis
in the initializer...