问题
I'm having an error message that tells me this:
'BankAccount.account' does not contain a definition for 'withdraw'.
Here's my code:
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);
}
}
}
I don't understand why it doesn't recognize it. It is a method in the class savingaccount.
回答1:
You're calling
base.withdraw(amt);
from your class savingsaccount
. But the base class (account
) has no such method. So the compiler is absolutely correct.
回答2:
It looks like you simply missed the method from the base type:
public virtual void Deposit(float amt)
{
balance += amt;
}
public virtual void Withdraw(float amt)
{
balance -= amt;
}
Note I changed "deposit" to +=
, and made the method virtual
so that subclasses can override
the method, which is (I strongly suspect) what the intent is here. Additionally, float
is a really bad choice for storing money. decimal
might be a better choice. As a stylistic change, I also capitalized the names.
回答3:
I assume your intention was to define the basic withdraw
method in the base account
class, so that it would be inherited by both savingaccount
and currentaccount
. You should declare it as virtual
in order to allow it to be overridden by the derived classes, if required.
class account
{
public virtual void withdraw(float amt)
{
if (balance - amt < 0)
Console.WriteLine("No balance in account.");
else
balance -= amt;
}
}
The currentaccount
class presumably does not need to modify the logic of this inherited method, so you can omit it altogether. On the other hand, in your savingaccount
class, you can override the method to implement your custom behaviour:
class savingaccount : account
{
public override 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++;
}
}
}
回答4:
You call base.withdraw(amt)
in your savingaccount
class, but the base class account
does not define this method.
回答5:
If you look closely, you'll see that your account
class in fact doesn't have a withdraw
method.
I'd guess you meant to have your account
class contain a virtual method withdraw
, defined like this:
public virtual void withdraw(float amt) { ... }
Then, in your inherited classes you'd want to override this method, like so:
class currentaccount : account
{
public override void withdraw(float amt)
{
...
base.withdraw(amt)
...
}
...
}
There's also a naming style issue with your code, but this is probably not in the scope of this question :)
回答6:
As everybody pointed out allready, you should declare the withdraw() method on your base class acocunt, so all the derived classes can inherit the method.
来源:https://stackoverflow.com/questions/13138406/object-does-not-contain-a-definition-for-name