2

Labforに問題classがあり、参照の割り当て方法がわからないという問題があります。

これが私のコードです:

class Bank
{
    static List<Account> accounts = new List<Account>();
    static Account active_account; // this will be an active 
    // Account (reference) retrieved from 
    // the bank. This will need to initialised either by 
    // CreateAccount or by RetrieveAccount.

    static void Main()
    {
        bool running = true, valid_option = false;
        int user_option = 0, account_num = 0, account_pin = 0;
        string name = "", type = "";
        decimal balance = 0, credit = 0;
        Bank chosen = new Bank();

        Console.Write("account number:");
                    account_num = SafeReadInteger(0);
                    Console.Write("pin number:");
                    account_pin = SafeReadInteger(0);
                    Console.Write("Client name:");
                    name = Console.ReadLine();
                    Console.Write("Balance:");
                    balance = SafeReadDecimal(0);
                    Console.Write("type:");
                    type = Console.ReadLine();
                    Console.Write("Credit:");
                    credit = SafeReadDecimal(0);
        chosen.CreateAccount(account_num, account_pin, name, balance, type);
    }
}

コメントは講師からです、

コンストラクターまでスクロールダウンします。

public Account CreateAccount(int ac_num_, int pin_, string owner_, decimal balance_, string type)
{
    Account newAcc = null;

    newAcc = new Account(ac_num_, pin_, owner_, balance_, type); // first uses the         constructor to create an account
    accounts.Add(newAcc); // second it inserts the account in the list and return the       reference to the account
    // depending on the type of account, a credit limit is set
    active_account = new Account(ac_num_, pin_);
    return newAcc;
}

私の質問は、どこで初期化active_accountし、何に割り当てるのですか?

4

2 に答える 2

3

CreateAccountメソッドはコンストラクターではありません。ファクトリメソッドです。それはこのように動作します:

active_account = chosen.CreateAccount(
    account_num, account_pin, name, balance, type);

active_account.DoStuff();

Bankインスタンスには、Accountクラスの初期化されたインスタンスを返すCreateAccountという名前のファクトリメソッドがあります。

于 2012-05-29T04:58:50.813 に答える
0

提供されたコードを見ると、active_accountは、作成されたばかりの現在のアクティブなアカウントを保持するため、または誰かがログインしたときに使用されます。

投稿したCreateAccount()関数で初期化されています。同様に、RetrieveAccount()関数はデータベースを呼び出し、active_accountを新しいAccountオブジェクトにも割り当てます。

于 2012-05-29T05:03:40.327 に答える