【发布时间】:2020-11-14 16:35:35
【问题描述】:
我正在尝试在派生类中创建一个构造函数,该构造函数从基类获取 initialBalance 和一个实例变量调用 interestRate。我收到错误代码 CS7036,没有给出与“Account.Account(decimal)”所需的形式参数“balance”相对应的参数。
namespace BankApp
{
class SavingsAccount : Account
{
private decimal interestRate;
private decimal interest;
public SavingsAccount(decimal percentage, decimal balance) // error is thrown here
{
interestRate = percentage;
IntialBalance = balance;
}
public void CalculateInterest()
{
interest = IntialBalance * interestRate;
}
}
}
namespace BankApp
{
class Account
{
private decimal accountBalance;
//constructor
public Account(decimal balance)
{
IntialBalance = balance; // validate the initial balance in property
}
// ensures intial balance is >= 0
public decimal IntialBalance
{
get { return accountBalance; }
set
{
if (value >= 0)
accountBalance = value;
else
throw new ArgumentOutOfRangeException("IntialBalance", value, "Intial balance must be >= 0");
}
}
// Returns current balance
public decimal Balance
{
get { return accountBalance; }
}
// deposits into account
public decimal Credit()
{
Console.WriteLine("Enter the amount you would like to deposit: ");
decimal deposit = Convert.ToInt32(Console.ReadLine());
accountBalance += deposit;
return accountBalance;
}
// withdrawl from account
public decimal Debit()
{
Console.WriteLine("Enter the amount you would like to withdrawl: ");
decimal withdrawl = Convert.ToInt32(Console.ReadLine());
if (accountBalance >= withdrawl)
accountBalance = accountBalance - withdrawl;
else
Console.WriteLine("Debit amount exceeded account balance");
return accountBalance;
}
}
}
【问题讨论】:
标签: c# inheritance constructor derived-class