Dart Classes
Building blocks of object-oriented programming
🏗️ What are Dart Classes?
Classes are blueprints for creating objects in Dart. They define properties (variables) and methods (functions) that objects will have, making code organized and reusable.
// Simple class definition
class Person {
String name = 'Unknown';
int age = 0;
void introduce() {
print('Hi, I am $name and I am $age years old');
}
}
Class Components
Properties
Variables that store data
class Car {
String brand = 'Toyota';
int year = 2023;
double price = 25000.0;
}
Methods
Functions that define behavior
class Calculator {
int add(int a, int b) {
return a + b;
}
void showResult(int result) {
print('Result: $result');
}
}
Constructors
Special methods to create objects
class Student {
String name;
int grade;
Student(this.name, this.grade);
}
Private Members
Hidden properties and methods
class BankAccount {
double _balance = 0; // Private
double get balance => _balance;
}
🔹 Creating a Basic Class
Let's create a simple class step by step:
class Dog {
// Properties (instance variables)
String name;
String breed;
int age;
// Constructor
Dog(this.name, this.breed, this.age);
// Methods
void bark() {
print('$name says: Woof! Woof!');
}
void sleep() {
print('$name is sleeping...');
}
void displayInfo() {
print('Name: $name');
print('Breed: $breed');
print('Age: $age years old');
}
}
void main() {
// Creating objects from the class
Dog myDog = Dog('Buddy', 'Golden Retriever', 3);
myDog.displayInfo();
myDog.bark();
myDog.sleep();
}
Output:
Name: Buddy
Breed: Golden Retriever
Age: 3 years old
Buddy says: Woof! Woof!
Buddy is sleeping...
🔹 Getters and Setters
Control access to class properties:
class Rectangle {
double _width = 0;
double _height = 0;
// Getter
double get area => _width * _height;
// Setter
set width(double value) {
if (value > 0) {
_width = value;
} else {
print('Width must be positive');
}
}
set height(double value) {
if (value > 0) {
_height = value;
} else {
print('Height must be positive');
}
}
// Getters for width and height
double get width => _width;
double get height => _height;
}
void main() {
Rectangle rect = Rectangle();
rect.width = 5.0;
rect.height = 3.0;
print('Width: ${rect.width}');
print('Height: ${rect.height}');
print('Area: ${rect.area}');
}
Output:
Width: 5.0
Height: 3.0
Area: 15.0
🔹 Static Members
Properties and methods that belong to the class, not instances:
class MathUtils {
static const double pi = 3.14159;
static int calculationCount = 0;
static double circleArea(double radius) {
calculationCount++;
return pi * radius * radius;
}
static void showCalculationCount() {
print('Total calculations: $calculationCount');
}
}
void main() {
// Access static members without creating objects
print('Pi value: ${MathUtils.pi}');
double area1 = MathUtils.circleArea(5);
double area2 = MathUtils.circleArea(3);
print('Circle 1 area: $area1');
print('Circle 2 area: $area2');
MathUtils.showCalculationCount();
}
Output:
Pi value: 3.14159
Circle 1 area: 78.53975
Circle 2 area: 28.274309999999996
Total calculations: 2