Summary: In this tutorial, you’ll learn how to define a Dart class and create objects from the class.
Introduction to the Dart classes #
Dart is an object-oriented programming language. In Dart, everything is an object. An object consists of states and behaviors:
- The state describes the values that an object has at a specified time.
- The behaviors are actions that an object can do to manipulate its state.
To create an object, you need to define a class first. A class is a blueprint for creating objects.
Variables are used to model the state of objects. When variables are defined inside a class, they are called properties of the class.
Functions are used to model the behaviors of objects. When functions are defined inside a class, they are called methods.
Defining a class #
To define a class, you use the class keyword followed by a class name and curly braces:
class MyClass {
}Code language: Dart (dart)By convention, the class names follow the PascalCase naming convention. For example, the following defines the Point class:
class Point {
}Code language: Dart (dart)Creating objects from a class #
To create an object from a class, you use the class name followed by parentheses. It’s like calling a function.
For example, the following creates a new object called p1 from the Point class:
Point p1 = Point();Code language: Dart (dart)Because the Dart compiler can infer the type of the p1 as Point, you can use the var keyword to make the statement more concise:
var p1 = Point();Code language: Dart (dart)In this example, p1 is an object of the Point class. In other words, p1 is an instance of the Point class:
class Point {
}
void main() {
var p1 = Point();
}Code language: Dart (dart)Adding properties to the class #
The following adds the x-coordinate and y-coordinate to the Point class and initialized their values to zeros:
class Point {
int x = 0;
int y = 0;
}Code language: Dart (dart)By doing this, all objects of the Point class will have the x and y properties. To access a property from an object, you use the dot notation as follows:
objectName.propertyCode language: Dart (dart)For example, the following creates a new Point object and assigns the values to the x and y properties:
class Point {
int x = 0;
int y = 0;
}
void main() {
var p1 = Point();
p1.x = 10;
p1.y = 20;
}Code language: Dart (dart)It’s important to note that you can create many objects from the Point class. For example, the following creates two Point objects p1 and p2:
class Point {
int x = 0;
int y = 0;
}
void main() {
var p1 = Point();
p1.x = 10;
p1.y = 20;
var p2 = Point();
p2.x = 100;
p2.x = 200;
}Code language: Dart (dart)In this example, p1 and p2 will have a separate set of properties x and y.
Cascade notation #
The following creates the p1 object and assigns its properties to values:
var p1 = Point();
p1.x = 10;
p1.y = 20;Code language: Dart (dart)In this example, the p1 object is repeated multiple times, each for an assignment. To make it more concise, Dart provides a cascade operator (..) that allows you to chain multiple assignments on the same object without repeating the object name. For example:
class Point {
int x = 0;
int y = 0;
}
void main() {
var p1 = Point()
..x = 10
..y = 20;
}Code language: Dart (dart)It’s important to note that the semicolon (;) only appears on the last line.
Adding a method to a class #
A method is like a function. When you define a function inside a class, it becomes a method. For example, the following adds a method called move() to the Point class that moves the point to a new coordinate:
class Point {
int x = 0;
int y = 0;
void move(int x1, int y1) {
x = x1;
y = y1;
}
}Code language: Dart (dart)Unlike a function, the move() method can access the x and y properties.
To call a method, you also use a dot notation syntax:
objectName.methodName(arguments);Code language: JavaScript (javascript)For example, the following shows how to call the move() method:
p1.move(100, 200);Code language: CSS (css)The following shows a complete program that defines the Point class and displays its object to the console:
class Point {
int x = 0;
int y = 0;
void move(int x1, int y1) {
x = x1;
y = y1;
}
}
void main() {
var p1 = Point()
..x = 10
..y = 20;
p1.move(100, 200);
print(p1);
}
Code language: Dart (dart)Output:
Instance of 'Point'Code language: Dart (dart)When you pass the p1 object to the print() function, the print() function converts the p1 object into a string before displaying it. By default, an object has the following string representation:
Instance of 'ClassName'Code language: Dart (dart)To make a custom string representation of an object, you need to override a method called toString() method, which will be covered in a later tutorial.
For now, you can define a show() method that displays the Point object to the console like this:
class Point {
int x = 0;
int y = 0;
void move(int x1, int y1) {
x = x1;
y = y1;
}
void show() {
print('Point($x,$y)');
}
}
void main() {
var p1 = Point()
..x = 10
..y = 20;
p1.move(100, 200);
p1.show();
}
Code language: Dart (dart)Output:
Point(100,200)Code language: Dart (dart)The is operator #
The is operator returns true if an object is an instance of a class. If an object is not an instance of the class, the is operator returns false:
myObject is MyClassFor example, the following expression returns true because p1 is an instance of the Point class:
var p1 = Point();
print(p1 is Point);Code language: PHP (php)Summary #
- Objects have states and behaviors.
- Properties represent the object’s state, and methods define the object’s behaviors.
- A class is a blueprint for creating objects.
- Use the
classkeyword to define a class. - Use the
isoperator to check if an object is an instance of a class.