C# 不';在C中没有接受0个参数的构造函数#
我在用C#编程时出错: “BankSystem.Account”不包含接受0个参数的构造函数 我的课程是: 首先,账户类别:C# 不';在C中没有接受0个参数的构造函数#,c#,constructor,C#,Constructor,我在用C#编程时出错: “BankSystem.Account”不包含接受0个参数的构造函数 我的课程是: 首先,账户类别: public abstract class Account : IAccount { private static decimal minIncome = 0; private static int minAge = 18; private string name; private string address; privat
public abstract class Account : IAccount
{
private static decimal minIncome = 0;
private static int minAge = 18;
private string name;
private string address;
private decimal age;
private decimal balance;
public Account(string inName, decimal inAge, decimal inBalance, string inAddress)
{
if (AccountAllowed(inBalance, inAge))
{
name = inName;
address = inAddress;
balance = inBalance;
age = inAge;
Console.WriteLine("We created the account. \nName is " + name + " \nThe address is: "
+ address + "\nThe balance is " + balance);
}
else
{
Console.WriteLine("We cann't create the account. Please check the balance and age!");
}
}
//public CustomerAccount(string newName, decimal initialBalance)
public Account(string inName, decimal initialBalance)
{
}
public class CustomerAccount : Account
{
private decimal balance = 0;
private string name;
public CustomerAccount(string newName, decimal initialBalance)
{
name = newName;
balance = initialBalance;
}
public CustomerAccount(string inName, decimal inAge, decimal inBalance, string inAddress)
: base(inName, inAge)
{
// name = inName;
//age = inAge;
}
public CustomerAccount(string inName, decimal inAge)
: base(inName, inAge)
{
// name = inName;
//age = inAge;
} ......
其次,CustomerAccount类:
public abstract class Account : IAccount
{
private static decimal minIncome = 0;
private static int minAge = 18;
private string name;
private string address;
private decimal age;
private decimal balance;
public Account(string inName, decimal inAge, decimal inBalance, string inAddress)
{
if (AccountAllowed(inBalance, inAge))
{
name = inName;
address = inAddress;
balance = inBalance;
age = inAge;
Console.WriteLine("We created the account. \nName is " + name + " \nThe address is: "
+ address + "\nThe balance is " + balance);
}
else
{
Console.WriteLine("We cann't create the account. Please check the balance and age!");
}
}
//public CustomerAccount(string newName, decimal initialBalance)
public Account(string inName, decimal initialBalance)
{
}
public class CustomerAccount : Account
{
private decimal balance = 0;
private string name;
public CustomerAccount(string newName, decimal initialBalance)
{
name = newName;
balance = initialBalance;
}
public CustomerAccount(string inName, decimal inAge, decimal inBalance, string inAddress)
: base(inName, inAge)
{
// name = inName;
//age = inAge;
}
public CustomerAccount(string inName, decimal inAge)
: base(inName, inAge)
{
// name = inName;
//age = inAge;
} ......
因为您在类中定义了带参数的构造函数,所以默认情况下不会获得默认构造函数 您的帐户类已定义构造函数:
public Account(string inName, decimal inAge, decimal inBalance, string inAddress)
public Account(string inName, decimal initialBalance)
您可以像这样定义默认构造函数
public Account()
{
}
您得到的错误是,您的CustomerAccount
的下面构造函数隐式调用帐户基类的默认构造函数,因为您没有指定任何其他基类构造函数,例如:base(arg1,arg2)代码>
以上内容与:
public CustomerAccount(string newName, decimal initialBalance) : base()
因为您在类中定义了带参数的构造函数,所以默认情况下不会获得默认构造函数
您的帐户类已定义构造函数:
public Account(string inName, decimal inAge, decimal inBalance, string inAddress)
public Account(string inName, decimal initialBalance)
您可以像这样定义默认构造函数
public Account()
{
}
您得到的错误是,您的CustomerAccount
的下面构造函数隐式调用帐户基类的默认构造函数,因为您没有指定任何其他基类构造函数,例如:base(arg1,arg2)代码>
以上内容与:
public CustomerAccount(string newName, decimal initialBalance) : base()
您正在像这样初始化帐户类
new Account();
但我们应该这样做
new Account("name", ...);
根据您的构造函数定义。您正在初始化您的帐户
类,如下所示
new Account();
但我们应该这样做
new Account("name", ...);
根据您的构造函数定义。您也需要在此处“链接”到基本构造函数:
public CustomerAccount(string newName, decimal initialBalance)
: base(newName, 0) // something like this
{
name = newName;
balance = initialBalance;
}
您也需要在此处“链接”到基本构造函数:
public CustomerAccount(string newName, decimal initialBalance)
: base(newName, 0) // something like this
{
name = newName;
balance = initialBalance;
}
简单。您的帐户
类不包含具有零参数的构造函数,例如
public Account()
{
}
答案在错误消息中
创建帐户的新实例时,请输入正确的参数,例如
账户=新账户(“约翰·史密斯”,20.00)代码>
或者创建一个接受零参数的构造函数。简单。您的帐户
类不包含具有零参数的构造函数,例如
public Account()
{
}
答案在错误消息中
创建帐户的新实例时,请输入正确的参数,例如
账户=新账户(“约翰·史密斯”,20.00)代码>
或者创建一个接受零参数的构造函数。您需要向我们展示更多的代码。此错误听起来像是您试图调用其中一个函数而未向其传递任何信息。错误消息包含答案。您不在RBS,是吗?;-)如果调用CustomerAccount上的2参数构造函数,因为它是从account派生的,它将尝试调用base()。由于您的基本帐户上没有无参数构造函数,因此您的代码将失败。您需要向我们显示更多的代码。此错误听起来像是您试图调用其中一个函数而未向其传递任何信息。错误消息包含答案。您不在RBS,是吗?;-)如果调用CustomerAccount上的2参数构造函数,因为它是从account派生的,它将尝试调用base()。由于您的基本帐户上没有无参数构造函数,因此您的代码将失败。但我在代码中没有使用默认构造函数!它是如何产生的?@AliEssa实际上,您的行public CustomerAccount(string newName,decimal initialBalance)
隐式调用了默认的基构造函数,因为正如Habib.OSU指出的,您没有调用特定的基构造函数“在派生类中,如果没有使用base关键字显式调用基类构造函数,则默认构造函数(如果有)将被隐式调用。”但我的代码中没有使用默认构造函数!它是如何产生的?@AliEssa实际上,您的行public CustomerAccount(string newName,decimal initialBalance)
隐式调用了默认的基构造函数,因为正如Habib.OSU指出的,您没有调用特定的基构造函数在派生类中,如果没有使用base关键字显式调用基类构造函数,则默认构造函数(如果有)将被隐式调用