"对象"不包含"名称"的定义

use*_*342 0 c# polymorphism inheritance

我有一条错误消息告诉我:

'BankAccount.account'不包含'withdraw'的定义.

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccounts
{
class account
{
    protected string name;
    protected float balance;
    public account(string n, float b)
    {
        name = n;
        balance = b;
    }

    public void deposit(float amt)
    {
        balance -= amt;
    }

    public void display()
    {
        Console.WriteLine("Name: {0}. Balance: {1}.", name, balance);
    }
}

class savingaccount : account
{
    static int accno = 1000;
    int trans;
    public savingaccount(string s, float b) : base(s, b)
    {
        trans = 0;
        accno++;
    }
    public void withdraw (float amt)
    {
        if (trans >= 10)
        {
            Console.WriteLine("Number of transactions exceed 10.");
            return;
        }
        if (balance - amt < 500)
            Console.WriteLine("Below minimum balance.");
        else
        {
            base.withdraw(amt);
            trans++;
        }
    }
    public void deposit(float amt)
    {
        if (trans >= 10)
        {
            Console.WriteLine("Number of transactions exceed 10.");
            return;
        }
        base.deposit(amt);
        trans++;
    }
    public void display()
    {
        Console.WriteLine("Name: {0}. Account no.: {1}. Balance: {2}", name, accno,        balance);
    }
}

class currentaccount : account
{
    static int accno = 1000;
    public currentaccount(string s, float b) : base(s, b)
    {
        accno++;
    }
    public void withdraw(float amt)
    {
        if (balance - amt < 0)
            Console.WriteLine("No balance in account.");
        else
            balance -= amt;
    }
    public void display()
    {
        Console.WriteLine("Name: {0}. Account no.: {1}. Balance: {2}.", name, accno, balance);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

我不明白为什么它不承认它.它是类savingaccount中的一个方法.

Joe*_*oey 6

你在打电话

base.withdraw(amt);
Run Code Online (Sandbox Code Playgroud)

来自你的班级savingsaccount.但是基类(account)没有这样的方法.所以编译器绝对正确.