5.9. Introduction to object-oriented programming in Dart: Constructors

Página 65

Object-oriented programming (OOP) is a programming paradigm that uses the idea of ​​"objects" to represent data and methods. Dart, the programming language used to develop Flutter apps, is an object-oriented language. In this context, let's explore a fundamental aspect of OOP in Dart: the constructors.

Constructors in Dart are special functions that are used to create and initialize an object of a class. They have the same name as the class and can have parameters to initialize object attributes. Constructors are called when you create an instance of a class using the 'new' keyword.

To illustrate, let's consider a 'Car' class. This class can have attributes like 'brand', 'model' and 'year'. To initialize these attributes when we create a 'Car' object, we can use a constructor. See example below:

class Car {
  String tag;
  model string;
  int year;

  Car(String make, String model, int year) {
    this.mark = mark;
    this.model = model;
    this.year = year;
  }
}

Here, 'Car' is the constructor. It accepts three parameters and uses them to initialize the object's attributes. To create a 'Car' object, we would do the following:

Car myCar = new Car('Toyota', 'Corolla', 2020);

In this case, 'myCar' is an object of class 'Car'. Its 'brand' is 'Toyota', its 'model' is 'Corolla' and its 'year' is 2020.

Dart also offers named constructors. These are constructors that have names in addition to the class name. They are useful when a class needs more than one constructor. For example, the class 'Car' can have a constructor named 'Car.Used' to create used cars:

class Car {
  String tag;
  model string;
  int year;

  Car(String make, String model, int year) {
    this.mark = mark;
    this.model = model;
    this.year = year;
  }

  used.car(this.make, this.model) {
    year = DateTime.now().year - 1;
  }
}

Here, 'Used.car' is a named constructor. It accepts two parameters for 'make' and 'model', and sets 'year' to the year before the current year. To create a used car, we would do the following:

Car myCarUsed = new Car.used('Toyota', 'Corolla');

In Dart, we also have constant constructors. They are used to create immutable objects, that is, objects whose values ​​cannot be changed after they are created. To create a constant constructor, we use the 'const' keyword.

Finally, Dart also offers the sugar syntax for constructors: variable initialization constructors. They allow you to initialize variables directly in constructor parameters, making the code more concise:

class Car {
  String tag;
  model string;
  int year;

  Car(this.brand, this.model, this.year);
}

Constructors are an essential part of object-oriented programming in Dart. They allow you to control how objects are created and initialized, providing greater flexibility and security in your code.

Now answer the exercise about the content:

What are constructors in Dart object-oriented programming and how are they used?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

665.10. Introduction to object-oriented programming in Dart: Operator overloading

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text