小编Gir*_*ram的帖子

如何在C#中调用方法中的构造函数

情景是

隐藏BankAccount的构造函数.为了允许构造BankAccount,创建一个名为CreateNewAccount的公共静态方法,负责根据请求创建和返回新的BankAccount对象.此方法将充当创建新BankAccounts的工厂.

我使用的代码就像

private BankAccount()
{
 ///some code here
}

//since the bank acc is protected, this method is used as a factory to create new bank accounts
public static void CreateNewAccount()
{
    Console.WriteLine("\nCreating a new bank account..");
    BankAccount();
}
Run Code Online (Sandbox Code Playgroud)

但这不断抛出错误.我不知道如何在同一个类的方法中调用构造函数

c# constructor

3
推荐指数
1
解决办法
1274
查看次数

标签 统计

c# ×1

constructor ×1