Dart tutorial 03 – classes, generics

Time:2021-2-11

Dart tutorial 03 - classes, generics
We learned in the first partDart tutorial 02 – control flow, exceptionNow let’s move on to classes and generics in dart.

Class

Here are the methods to declare a simple class in dart and create an instance of it.

Dart tutorial 03 - classes, generics

Add instance variables and constructors to the class

Dart tutorial 03 - classes, generics

Dart provides a concise syntax for constructor initialization. As follows:

Dart tutorial 03 - classes, generics

As you can see, we have written a line of constructors with the first parameter value set to name and the second parameter value set to age. Now we don’t have to write this.name =Name is such a boring sentence.

Named Constructors

Dart provides another way to define constructors, called named constructors.

Dart tutorial 03 - classes, generics

As you can see, we provide a name for the constructor. In this way, when we call different constructors, the semantics will be clearer, and we don’t need to judge by parameters.

inherit

You can use the extend keyword to inherit other classes in dart.

Here, our Pug class inherits from the dog class, and uses the super keyword to pass in appropriate parameters and call the dog class’s constructor.

You can also use the keyword this after the colon (:) to call other constructors in the same class.

Some initialization operations can be done after colon (:), such as calling constructor, assigning instance variables, etc.

Here we create two named constructors with only the name parameter and call the default Pug constructor.

method

The methods in the class are similar to the normal methods defined in dart.

The coverage method is also very simple.

Setter
By default, any variable you define in a class can be accessed by referring to the variable name on the object, such as dog.name Object variables can also be assigned directly. But sometimes you want to customize the getters and setters of attributes. In dart, you can use the get and set keywords to customize the getters and setters.

Now that the class properties are still visible and can be changed at will, let’s make the class properties private.

Control accessibility

By default, every property and method you define in a class is public and directly accessible. In dart, you can use the Make any variable or method private. Let’s make the name property private.

Dart tutorial 03 - classes, generics

Abstract classes and methods

You can use the abstract keyword to create an abstract class in dart.

You only need to provide the abstract keyword before the class declaration. For methods, just provide the signature and omit the implementation.

Static method

To make a field / method static, just add the keyword static before declaring it.

enumeration

Dart supports enumeration and is used like other languages. If you’re from the Java language, you’ll be familiar with them.

generic paradigm

Dart fully supports generics. Suppose you are writing a data only class, and you want it to hold any type of data. Here’s how to write this class using generics.


Related articles:

Dart tutorial 01 – variables, types, functions

Dart tutorial 02 – control flow, exception