0

我是编程新手,这门课让我很头疼。

这是我的代码(类型基于用户输入)

public static Account CreateAccount(int type)
{
    switch (type) 
    { 
        case 1:
            SaveAcc savings = new SaveAcc();  
        break;

        default:
            Console.WriteLine("No such choice");
        break;

    }
    return new Account();
}

这些是我的课:

class Account 
{
    protected int balance;
    protected int accountnr = 1;
    protected bool credit;

    public Account()
    {
        newNr++;
        accountnr = newNr;
    }

    public override string ToString()
    {
        return "AccNr: " + Nr.ToString("G") + ", balance: " + balance.ToString("C");
    }
}

class SaveAcc: Account
{
    public int rate;
    public SaveAcc()
    {
        credit = true;
        rate = 0.03;
    }

    public override string ToString()
    {
       return "AccNr: " + Nr.ToString("G") + ", balance: " + balance.ToString("C") + credit.ToString();
    }
}

当我创建 SavAcc 对象时,正在调用“错误”覆盖。我的目标是显示位于 SavAcc 继承类中的覆盖 ToString 方法提供的所有信息。我错过了一些明显的东西吗?

4

1 回答 1

0

改变这个:

switch (type) 
{ 
        case 1:
            SaveAcc savings = new SaveAcc();  
        break;

        default:
            Console.WriteLine("No such choice");
        break;

}
return new Account();

对此:

switch (type) 
{ 
    case 1:
        return new SaveAcc();  
    default:
        Console.WriteLine("No such choice");
        return new Account();
}
于 2016-03-31T20:44:02.383 回答