Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ チャレンジ:クラスでプライベートプロパティを実装する | JavaScriptクラスと継承の習得
JavaScriptロジックとインタラクション

bookチャレンジ:クラスでプライベートプロパティを実装する

メニューを表示するにはスワイプしてください

課題

ユーザーの銀行口座を安全に管理するために、BankAccount クラスを作成します。各口座には所有者と残高がありますが、残高は直接アクセスできないように保護したいと考えています。目的は、プライベートプロパティを使用して残高への意図しない変更を防ぎ、メソッドを通じて制御されたアクセスのみを許可することです。

  1. プライベート残高プロパティ:
    • BankAccount クラス内でプライベートプロパティ #balance を宣言します。
  2. コンストラクタ:
    • コンストラクタは ownerinitialBalance をパラメータとして受け取ります;
    • owner をパブリックプロパティに、initialBalance をプライベートプロパティ #balance に代入します。
  3. メソッドの追加:
    • deposit: パラメータとして amount を受け取るメソッドを定義します。amount が0より大きい場合、#balanceamount を加算します;
    • withdraw: パラメータとして amount を受け取るメソッドを定義します。amount が0より大きく、かつ #balance 以下の場合、amount#balance から減算します;
    • getBalance: 所有者の名前と口座残高を含む文字列(例:"Account balance for John: $1500")を返すメソッドを定義します。
123456789101112131415161718192021222324252627282930313233343536
class BankAccount { #_____; // Declare private property constructor(owner, initialBalance) { this._____ = owner; this.#_____ = initialBalance; } deposit(_____) { if (_____) { this.#_____ += _____; } } withdraw(_____) { if (_____ && _____) { this.#_____ -= _____; } } getBalance() { return `Account balance for ${this._____}: $${this.#_____}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
copy
  • #balance クラスでプライベートプロパティ BankAccount を宣言します;
  • コンストラクタで owner をパブリックプロパティに、initialBalance をプライベートプロパティ #balance に代入します;
  • deposit メソッドを定義し、パラメータとして amount を受け取ります。amount が0より大きい場合、amount#balance を加算します;
  • withdraw メソッドを定義し、パラメータとして amount を受け取ります。amount が0より大きく、かつ #balance 以下の場合、amount#balance から減算します;
  • getBalance メソッドを定義し、所有者の名前と口座残高を返す文字列を返します。
123456789101112131415161718192021222324252627282930313233343536
class BankAccount { #balance; // Declare private property constructor(owner, initialBalance) { this.owner = owner; this.#balance = initialBalance; } deposit(amount) { if (amount > 0) { this.#balance += amount; } } withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; } } getBalance() { return `Account balance for ${this.owner}: $${this.#balance}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Output: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Output: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
copy

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  8

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  8
some-alt