Encapsulation in Practice
Encapsulation is a fundamental concept in object-oriented programming that helps you design classes with robust, controlled access to their internal data. It means hiding the internal state of an object and requiring all interaction to occur through well-defined methods. This practice is essential for robust class design because it protects the integrity of your data, makes your code easier to maintain, and reduces the chance of bugs caused by unintended modifications.
Encapsulation is the practice of keeping the internal details of a class hidden from the outside world, exposing only what is necessary through public methods. In object-oriented programming, encapsulation ensures that objects control their own data and behavior, leading to safer and more maintainable code.
Program.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950using System; namespace ConsoleApp { public class BankAccount { private decimal balance; public BankAccount(decimal initialBalance) { balance = initialBalance; } public void Deposit(decimal amount) { if (amount > 0) { balance += amount; } } public bool Withdraw(decimal amount) { if (amount > 0 && amount <= balance) { balance -= amount; return true; } return false; } public decimal GetBalance() { return balance; } } public class Program { public static void Main(string[] args) { BankAccount account = new BankAccount(100); account.Deposit(50); bool success = account.Withdraw(30); Console.WriteLine("Balance: " + account.GetBalance()); Console.WriteLine("Withdrawal successful: " + success); } } }
In the BankAccount class, encapsulation is achieved by declaring the balance field as private. This means that code outside the class cannot access or modify balance directly. Instead, you must use the public methods Deposit, Withdraw, and GetBalance to interact with the balance.
By restricting direct access, you ensure that only valid operations—like adding a positive amount or withdrawing only if there are sufficient funds—can change the state of the account. This prevents accidental or malicious changes that could leave the object in an invalid state.
PublicFieldsExample.cs
123456// This code is for illustration only and should not be used in practice. public class BankAccount { public decimal balance; }
If you make fields like balance public, as shown above, any code can change the value to anything at any time. This can lead to serious problems, such as setting the balance to a negative number, bypassing business rules, or causing bugs that are hard to trace. Encapsulation helps you avoid these risks by forcing all changes to go through controlled methods.
1. What is encapsulation?
2. Why should fields usually be private in a class?
3. How does encapsulation help prevent bugs?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Fantastisk!
Completion rate forbedret til 4.17
Encapsulation in Practice
Stryg for at vise menuen
Encapsulation is a fundamental concept in object-oriented programming that helps you design classes with robust, controlled access to their internal data. It means hiding the internal state of an object and requiring all interaction to occur through well-defined methods. This practice is essential for robust class design because it protects the integrity of your data, makes your code easier to maintain, and reduces the chance of bugs caused by unintended modifications.
Encapsulation is the practice of keeping the internal details of a class hidden from the outside world, exposing only what is necessary through public methods. In object-oriented programming, encapsulation ensures that objects control their own data and behavior, leading to safer and more maintainable code.
Program.cs
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950using System; namespace ConsoleApp { public class BankAccount { private decimal balance; public BankAccount(decimal initialBalance) { balance = initialBalance; } public void Deposit(decimal amount) { if (amount > 0) { balance += amount; } } public bool Withdraw(decimal amount) { if (amount > 0 && amount <= balance) { balance -= amount; return true; } return false; } public decimal GetBalance() { return balance; } } public class Program { public static void Main(string[] args) { BankAccount account = new BankAccount(100); account.Deposit(50); bool success = account.Withdraw(30); Console.WriteLine("Balance: " + account.GetBalance()); Console.WriteLine("Withdrawal successful: " + success); } } }
In the BankAccount class, encapsulation is achieved by declaring the balance field as private. This means that code outside the class cannot access or modify balance directly. Instead, you must use the public methods Deposit, Withdraw, and GetBalance to interact with the balance.
By restricting direct access, you ensure that only valid operations—like adding a positive amount or withdrawing only if there are sufficient funds—can change the state of the account. This prevents accidental or malicious changes that could leave the object in an invalid state.
PublicFieldsExample.cs
123456// This code is for illustration only and should not be used in practice. public class BankAccount { public decimal balance; }
If you make fields like balance public, as shown above, any code can change the value to anything at any time. This can lead to serious problems, such as setting the balance to a negative number, bypassing business rules, or causing bugs that are hard to trace. Encapsulation helps you avoid these risks by forcing all changes to go through controlled methods.
1. What is encapsulation?
2. Why should fields usually be private in a class?
3. How does encapsulation help prevent bugs?
Tak for dine kommentarer!