Properties and Methods
In Dart, classes can have properties (also called fields or attributes) and methods (functions that belong to the class). Think of properties as the characteristics of an object, and methods as the actions that object can perform.
Properties
Properties are variables that store data within a class. They define what an object "has" or its state.
class Car {
// Properties
String brand;
String model;
int year;
bool isRunning = false;
// Constructor
Car(this.brand, this.model, this.year);
}
Methods
Methods are functions defined inside a class that can access and modify the object's properties. They define what an object can "do".
class Car {
String brand;
String model;
int year;
bool isRunning = false;
Car(this.brand, this.model, this.year);
// Methods
void startEngine() {
isRunning = true;
print('$brand $model engine started!');
}
void stopEngine() {
isRunning = false;
print('$brand $model engine stopped.');
}
String getCarInfo() {
return '$year $brand $model';
}
int getAge() {
return DateTime.now().year - year;
}
}
Using Properties and Methods
Here's how you can create objects and use their properties and methods:
void main() {
// Create a car object
Car myCar = Car('Toyota', 'Camry', 2020);
// Access properties
print('Brand: ${myCar.brand}');
print('Model: ${myCar.model}');
print('Year: ${myCar.year}');
print('Is running: ${myCar.isRunning}');
// Call methods
myCar.startEngine();
print('Car info: ${myCar.getCarInfo()}');
print('Car age: ${myCar.getAge()} years');
myCar.stopEngine();
}
Property Types
Properties can be of different types and have different access levels:
| Property Type | Description | Example |
|---|---|---|
| Instance Properties | Belong to a specific instance of the class | String name; |
| Static Properties | Belong to the class itself, shared by all instances | static int count = 0; |
| Final Properties | Can only be set once, usually in constructor | final String id; |
| Private Properties | Start with underscore, only accessible within the same library | String _password; |
Method Types
class BankAccount {
String _accountNumber;
double _balance = 0.0;
static int totalAccounts = 0;
BankAccount(this._accountNumber) {
totalAccounts++;
}
// Instance method
void deposit(double amount) {
_balance += amount;
print('Deposited \$${amount}. New balance: \$${_balance}');
}
// Getter method
double get balance => _balance;
// Setter method
set accountNumber(String newNumber) {
_accountNumber = newNumber;
}
// Static method
static int getTotalAccounts() {
return totalAccounts;
}
// Private method
void _validateTransaction(double amount) {
if (amount <= 0) {
throw Exception('Amount must be positive');
}
}
}
Key Points to Remember
- Properties store the state/data of an object
- Methods define the behavior/actions of an object
- Use
thiskeyword when you need to refer to the current instance - Private members start with an underscore (
_) - Static members belong to the class, not to any specific instance
- Getters and setters provide controlled access to properties
Think of a class like a blueprint for a house. Properties are like the rooms, windows, and doors (what the house has), while methods are like the actions you can perform (opening doors, turning on lights, etc.).