c#分配一个引用
我对c#分配一个引用,c#,initialization,assign,C#,Initialization,Assign,我对class的Lab有一个问题,问题是我不确定如何分配我的参考 这是我的代码: 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
class
的Lab
有一个问题,问题是我不确定如何分配我的参考
这是我的代码:
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);
}
}
我的问题是,我在哪里初始化
活动\u帐户
以及将其分配给什么?您的CreateAccount方法不是构造函数。这是一种工厂方法。它的工作原理如下:
active_account = chosen.CreateAccount(
account_num, account_pin, name, balance, type);
active_account.DoStuff();
您的银行实例有一个名为CreateAccount的工厂方法,该方法返回Account类的初始化实例 查看提供的代码,active_account用于保存刚刚创建的当前活动帐户,或者当有人登录时
它正在您发布的CreateAccount()函数中初始化。类似地,RetrieveAccount()函数将调用数据库并将活动帐户分配给新帐户对象 我发现用尾随下划线标记方法参数的风格令人信服。是你的发明还是讲师的口述。或者你有推荐它的造型引擎吗?
active_account = chosen.CreateAccount(
account_num, account_pin, name, balance, type);
active_account.DoStuff();