1

C# でプログラミング中に次のエラーが発生しました: 'BankSystem.Account' には、0 引数を取るコンストラクターが含まれていません

私のクラスは次のとおりです。

まず、 Account クラス:

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

次に、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;
    } ......
4

4 に答える 4

7

クラスでパラメーターを使用してコンストラクターを定義したため、既定では既定のコンストラクターを取得できません。

アカウント クラスにはコンストラクタが定義されています。

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

のようなデフォルトのコンストラクタを定義できます。

public Account() 
{
}

あなたが得ているエラーはCustomerAccount、他の基本コンストラクターを指定していないため、以下のコンストラクターが Account 基本クラスのデフォルトコンストラクターを暗黙的に呼び出しているためです。:base(arg1,arg2);

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

上記は次と同じです。

 public CustomerAccount(string newName, decimal initialBalance) : base()
于 2012-06-25T09:46:12.937 に答える
7

ここでも基本コンストラクターに「チェーン」する必要があります。

public CustomerAccount(string newName, decimal initialBalance)
    : base(newName, 0)    // something like this
{
    name = newName;
    balance = initialBalance;
}
于 2012-06-25T09:48:04.193 に答える
4

単純。あなたのAccountクラスにはゼロ引数を持つコンストラクターが含まれていません。

public Account()
{

}

答えはエラーメッセージにあります。

クラスの新しいインスタンスを作成するときAccountに、正しいパラメーターを渡します。

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

または、引数を取らないコンストラクターを作成します。

于 2012-06-25T09:52:03.960 に答える
1

Accountこのようにクラスを初期化しています

new Account();

でもやるべき

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

コンストラクターの定義に従って。

于 2012-06-25T09:46:40.607 に答える