Encapsulation, Getters and Setters
Encapsulation is the object-oriented programming principle of hiding internal data and only allowing access through controlled interfaces, such as getters and setters. In the BankAccount and Temperature examples, private fields and controlled access methods ensure that data cannot be changed in unsafe or unintended ways.
Encapsulation means restricting direct access to an object's data and exposing it only through controlled methods. In Dart, you do this by using private fields, getters, and setters. This keeps your objects safe, consistent, and easier to maintain.
A getter is a special method that lets you read a private field's value from outside the class, without giving direct access. You define a getter using the get keyword. For instance, double get balance => _balance; lets you access the balance safely.
A setter is a special method that lets you update a private field's value from outside the class, but with control. You define a setter using the set keyword. For example, you can write set deposit(double amount) { if (amount > 0) { _balance += amount; } } to ensure only positive amounts are added.
bank_account.dart
12345678910111213141516171819class BankAccount { double _balance = 0; double get balance => _balance; set deposit(double amount) { if (amount > 0) { _balance += amount; } } } void main() { var account = BankAccount(); account.deposit = 100; print("Current balance: \$${account.balance}"); account.deposit = -50; // Invalid deposit, ignored print("After invalid deposit: \$${account.balance}"); }
The BankAccount class above shows how encapsulation works in practice. The _balance field is private, so you cannot change it directly from outside the class. Instead, you use the deposit setter, which only allows positive amounts to be added. The balance getter lets you read the value safely. This ensures that the balance cannot become negative or be changed in unexpected ways, protecting the integrity of your data.
temperature.dart
12345678910111213141516171819class Temperature { double _celsius = 0; double get celsius => _celsius; set celsius(double value) { if (value >= 0) { _celsius = value; } } } void main() { var temp = Temperature(); temp.celsius = 25; print("Temperature: ${temp.celsius}"); temp.celsius = -10; // Invalid, ignored print("After invalid set: ${temp.celsius}"); }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Fantastico!
Completion tasso migliorato a 11.11
Encapsulation, Getters and Setters
Scorri per mostrare il menu
Encapsulation is the object-oriented programming principle of hiding internal data and only allowing access through controlled interfaces, such as getters and setters. In the BankAccount and Temperature examples, private fields and controlled access methods ensure that data cannot be changed in unsafe or unintended ways.
Encapsulation means restricting direct access to an object's data and exposing it only through controlled methods. In Dart, you do this by using private fields, getters, and setters. This keeps your objects safe, consistent, and easier to maintain.
A getter is a special method that lets you read a private field's value from outside the class, without giving direct access. You define a getter using the get keyword. For instance, double get balance => _balance; lets you access the balance safely.
A setter is a special method that lets you update a private field's value from outside the class, but with control. You define a setter using the set keyword. For example, you can write set deposit(double amount) { if (amount > 0) { _balance += amount; } } to ensure only positive amounts are added.
bank_account.dart
12345678910111213141516171819class BankAccount { double _balance = 0; double get balance => _balance; set deposit(double amount) { if (amount > 0) { _balance += amount; } } } void main() { var account = BankAccount(); account.deposit = 100; print("Current balance: \$${account.balance}"); account.deposit = -50; // Invalid deposit, ignored print("After invalid deposit: \$${account.balance}"); }
The BankAccount class above shows how encapsulation works in practice. The _balance field is private, so you cannot change it directly from outside the class. Instead, you use the deposit setter, which only allows positive amounts to be added. The balance getter lets you read the value safely. This ensures that the balance cannot become negative or be changed in unexpected ways, protecting the integrity of your data.
temperature.dart
12345678910111213141516171819class Temperature { double _celsius = 0; double get celsius => _celsius; set celsius(double value) { if (value >= 0) { _celsius = value; } } } void main() { var temp = Temperature(); temp.celsius = 25; print("Temperature: ${temp.celsius}"); temp.celsius = -10; // Invalid, ignored print("After invalid set: ${temp.celsius}"); }
Grazie per i tuoi commenti!