我正在用 C# 做作业,其中我处理抽象类。
public abstract class Account
{
public abstract bool Credit(double amount);
public abstract bool Debit(double amount);
}
public class SavingAccount : Account
{
public override bool Credit(double amount)
{
bool temp = true;
temp = base.Credit(amount + calculateInterest());
return temp;
}
public override bool Debit(double amount)
{
bool flag = true;
double temp = getBalance();
temp = temp - amount;
if (temp < 10000)
{
flag = false;
}
else
{
return (base.Debit(amount));
}
return flag;
}
}
当我调用 base.Debit() 或 base.Credit() 时,它会给出无法调用抽象成员的错误。 请帮助我。
最佳答案
你不能调用抽象方法,这个想法是声明为抽象的方法只需要派生类来定义它。使用 base.Debit
会影响尝试调用抽象方法,这是无法完成的。仔细阅读您的代码,我认为这就是您想要的 Debit()
public abstract class Account
{
protected double _balance;
public abstract bool Credit(double amount);
public abstract bool Debit(double amount);
}
public class SavingAccount : Account
{
public double MinimumBalance { get; set; }
public override bool Debit(double amount)
{
if (amount < 0)
return Credit(-amount);
double temp = _balance;
temp = temp - amount;
if (temp < MinimumBalance)
{
return false;
}
else
{
_balance = temp;
return true;
}
}
public override bool Credit(double amount)
{
if (amount < 0)
return Debit(-amount);
_balance += amount;
return true;
}
}
关于c# - 不能调用抽象成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10218686/