Book Image

Learning Dart, Second Edition - Second Edition

By : Ivo Balbaert
Book Image

Learning Dart, Second Edition - Second Edition

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Learning Dart Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 the classes in Java or C#, it's nothing special and we can already see certain simplifications:

  • The short constructor notation lets the parameter values flow directly into the properties:

    BankAccount(this.owner, this.number, this.balance) { … }
  • The this keyword 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, like 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 this keyword in the initializer expression...