我收到的錯誤消息告訴我:「對象」不包含「名稱」的定義
'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);
}
}
}
我不明白爲什麼它不承認它。它是類savingaccount中的一個方法。
「account」中沒有'.withdraw'方法(最好是通過這種方式來大寫你的類名),然而你的派生類有它們 - 爲什麼不在''基類中包含'.withdraw'的簽名?你能發佈你如何使用這段代碼嗎?據我所知,錯誤信息是正確的。 –
它看起來像很多這些方法實際上應該'虛擬'/'覆蓋',順便說一句 - 並不「存款」通常*增加*餘額? –