我对Lab
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
以及我分配给它的是什么?
答案 0 :(得分:3)
您的CreateAccount方法不是构造函数。这是一种工厂方法。它的工作原理如下:
active_account = chosen.CreateAccount(
account_num, account_pin, name, balance, type);
active_account.DoStuff();
您的Bank实例有一个名为CreateAccount的工厂方法,它返回Account类的初始化实例。
答案 1 :(得分:0)
查看提供的代码,active_account用于保存刚刚创建的当前活动帐户,或者有人登录时。
它正在您发布的CreateAccount()函数中初始化。同样,您的RetrieveAccount()函数也会调用数据库并将active_account分配给新的Account对象。