简体   繁体   English

没有在C#中接受0参数的构造方法

[英]Doesn't have a constructer that take 0 arguments in C#

I've got this error while I program in C#: 'BankSystem.Account' does not contain a constructor that takes 0 arguments 我在C#中编程时遇到此错误:“ BankSystem.Account”不包含带有0个参数的构造函数

My classes are: 我的课程是:

Firstly, the Account class: 首先,帐户类:

 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)
    {
    }

Secondly, The CustomerAccount class: 其次,CustomerAccount类:

 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;
    } ......

Because you have defined constructors with parameters in your class, you will not get the default constructor by default. 因为您在类中定义了带有参数的构造函数,所以默认情况下不会获得默认的构造函数。

Your account class has defined constructors: 您的帐户类已定义了构造函数:

public Account(string inName, decimal inAge, decimal inBalance, string inAddress)
public Account(string inName, decimal initialBalance)

you may define a default constructor like. 您可以定义类似的默认构​​造函数。

public Account() 
{
}

The error you are getting is because, your below constructor for CustomerAccount is implicitly calling a default constructor for the Account base class, as you have not specified any other base constructor eg :base(arg1,arg2); 您得到的错误是因为,下面的CustomerAccount构造函数正在隐式调用Account基类的默认构造函数,因为您没有指定任何其他基本构造函数,例如:base(arg1,arg2);

 public CustomerAccount(string newName, decimal initialBalance)
    {
        name = newName;
        balance = initialBalance;
    }

The above is same as: 上面是一样的:

 public CustomerAccount(string newName, decimal initialBalance) : base()

You need to 'chain' to the base constructor here too: 您也需要在此处“链接”到基本构造函数:

public CustomerAccount(string newName, decimal initialBalance)
    : base(newName, 0)    // something like this
{
    name = newName;
    balance = initialBalance;
}

Simple. 简单。 Your Account class doesn't contain a contructor with Zero arguments eg 您的Account类不包含带有零参数的构造函数,例如

public Account()
{

}

The answer is in the error message. 答案在错误消息中。

When you create a new instance of the Account class pass in the correct parameters eg 当您创建Account类的新实例时,请输入正确的参数,例如

Account account = new Account("John Smith", 20.00);

Or create a constructor that takes zero arguments. 或创建一个接受零参数的构造函数。

You are initializing your Account class like this 您正在像这样初始化您的Account

new Account();

but should do 但应该做

new Account("name", ...);

according to your constructor definition. 根据您的构造函数定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM