5.2. Introduction to Object Oriented Programming in Dart: Classes and Objects

Página 58

Object-oriented programming (OOP) is a programming paradigm that uses "objects" - data structures consisting of data fields, also known as attributes; and methods, also known as functions or procedures. Each object is an instance of a class, which is like a blueprint or blueprint for how the object should behave. In Dart, as in many other modern programming languages, OOP is a fundamental concept that facilitates the organization and structure of code.

Classes

In Dart, a class is a structure that serves as a template for creating objects. A class defines the attributes and behaviors that an object can have. Attributes are represented by variables and behaviors are represented by functions.

To define a class in Dart, we use the keyword 'class' followed by the name of the class. Variables and functions are defined inside curly braces. For example:

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

  void accelerate() {
    print('Accelerating...');
  }

  void brake() {
    print('Braking...');
  }
}

In this example, the 'Car' class has three variables: 'make', 'model' and 'year'. It also has two functions: 'accelerate' and 'brake'. Every object created from this class will have these attributes and behaviors.

Objects

An object is an instance of a class. When we create an object, we are essentially creating a copy of the class that can have its own values ​​for the variables defined in the class.

To create an object in Dart, we use the 'new' keyword followed by the class name and parentheses. For example:

var myCar = new Car();

In this example, 'myCar' is an object of class 'Car'. We can define the values ​​for the 'make', 'model' and 'year' variables of the 'myCar' object as follows:

myCar.brand = 'Toyota';
myCarro.model = 'Corolla';
myCar.year = 2020;

We can also call the 'accelerate' and 'brake' functions of the 'myCar' object as follows:

myCar.accelerate();
myCar.brake();

In summary, object-oriented programming in Dart is a powerful concept that allows us to create organized and structured code. Classes allow us to define models for objects, and objects allow us to create instances of those models with their own values ​​and behaviors. This makes it easier to reuse code and maintain code as it grows and becomes more complex.

It is important to note that object-oriented programming is just one of many approaches to programming. While it's a powerful tool, it's not always the best choice for every situation. However, in many cases, especially when developing complex applications like the ones we can build with Flutter, OOP can be an invaluable tool.

Now answer the exercise about the content:

What is a class in object-oriented programming in Dart?

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

You missed! Try again.

Next page of the Free Ebook:

595.3. Introduction to Object Oriented Programming in Dart: Attributes and Methods

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