Private Members
When designing robust applications in TypeScript, it is important to control how the internal state of a class can be accessed and modified. The private access modifier allows you to define properties and methods that are hidden from outside the class. This means only the class itself can read or change these members, helping you hide implementation details and prevent accidental misuse. By marking a property or method as private, you ensure that it cannot be accessed, modified, or called from outside the class, including from subclasses.
1234567891011121314151617181920212223242526272829303132333435363738class BankAccount { private balance: number; constructor(initialBalance: number) { this.balance = initialBalance; } private logTransaction(amount: number, type: string): void { console.log(`${type} of $${amount}. New balance: $${this.balance}`); } public deposit(amount: number): void { this.balance += amount; this.logTransaction(amount, "Deposit"); } public withdraw(amount: number): void { if (amount > this.balance) { console.log("Insufficient funds"); return; } this.balance -= amount; this.logTransaction(amount, "Withdrawal"); } public getBalance(): number { return this.balance; } } const account = new BankAccount(1000); account.deposit(200); // Deposit of $200. New balance: $1200 account.withdraw(500); // Withdrawal of $500. New balance: $700 console.log(account.getBalance()); // 700 // The following lines would cause a compilation error: // account.balance = 5000; // account.logTransaction(100, "Test");
Encapsulation is a core principle of object-oriented programming. By using private members, you protect the integrity of your class's data and logic, making it easier to maintain and refactor code without breaking external components that depend on your class. Private members enforce clear boundaries between a class's internal workings and its public interface. This helps prevent accidental changes to internal state and allows you to safely update or optimize the class's implementation in the future, knowing that external code cannot rely on or interfere with its private details.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 5
Private Members
Deslize para mostrar o menu
When designing robust applications in TypeScript, it is important to control how the internal state of a class can be accessed and modified. The private access modifier allows you to define properties and methods that are hidden from outside the class. This means only the class itself can read or change these members, helping you hide implementation details and prevent accidental misuse. By marking a property or method as private, you ensure that it cannot be accessed, modified, or called from outside the class, including from subclasses.
1234567891011121314151617181920212223242526272829303132333435363738class BankAccount { private balance: number; constructor(initialBalance: number) { this.balance = initialBalance; } private logTransaction(amount: number, type: string): void { console.log(`${type} of $${amount}. New balance: $${this.balance}`); } public deposit(amount: number): void { this.balance += amount; this.logTransaction(amount, "Deposit"); } public withdraw(amount: number): void { if (amount > this.balance) { console.log("Insufficient funds"); return; } this.balance -= amount; this.logTransaction(amount, "Withdrawal"); } public getBalance(): number { return this.balance; } } const account = new BankAccount(1000); account.deposit(200); // Deposit of $200. New balance: $1200 account.withdraw(500); // Withdrawal of $500. New balance: $700 console.log(account.getBalance()); // 700 // The following lines would cause a compilation error: // account.balance = 5000; // account.logTransaction(100, "Test");
Encapsulation is a core principle of object-oriented programming. By using private members, you protect the integrity of your class's data and logic, making it easier to maintain and refactor code without breaking external components that depend on your class. Private members enforce clear boundaries between a class's internal workings and its public interface. This helps prevent accidental changes to internal state and allows you to safely update or optimize the class's implementation in the future, knowing that external code cannot rely on or interfere with its private details.
Obrigado pelo seu feedback!