好了,这是多个问题合在一起。老实说,我需要一位导师或面对面交谈的人来澄清其中的一些概念,但我在网上上了这个c#课程,教授拒绝与我交流,所以我的下一步是来这里。我知道这是很多材料,但任何指导都会非常感谢。
首先,这个赋值的目的是利用类中的继承。本质上,您应该创建一个名为Account的基类,该基类初始化所有变量,并为每个变量设置get和set (我不完全了解如何使用它们)以及设置Credit和Debit方法。最后,创建一个要在派生类中修改的PrintAccount方法。
接下来,创建一个从Account继承所有内容的SavingsAccount类(我认为我在创建该类时使用了":Account“,这是正确的)。在其中,您应该使用override修改PrintAccount方法,让它在被调用时提供额外的信息。之后,创建一个继承自Account的CheckingAccount类。现在您必须修改借方和贷方以及PrintAccount。说明是通过“调用Account类并使用布尔值来查看是否有钱被提取”来修改Debit/Credit。不知道那是什么意思。另外,您应该再次重写PrintAccount,使其显示特定于类的信息。
在所有这些之后,创建一个测试类来测试已经到位的所有东西。我最初的问题是确保我的所有类都正确继承,并且我没有做任何不必要的事情。接下来,当我覆盖PrintAccount类时,我显然做错了什么。最后,调用Account类对Debit/Credit方法进行布尔测试。
namespace Assignment5
{
class Account
{
//variables
private double balance;
private string accountName;
private int accountNumber;
public Account(string acctName, int acctNum, double acctBal)
{
//what is the purpose of doing this?
accountName = acctName;
accountNumber = acctNum;
balance = acctBal;
}
//gets and sets
public double Balance
{
get { return balance; }
set
{
if (value >= 0)
balance = value;
else
balance = 0;
}
}
public string AccountName
{
get { return accountName; }
}
public int AccountNumber
{
get { return accountNumber; }
}
//credit, debit and print methods
public void Credit(double a)
{
balance += a;
}
public void Debit(double a)
{
if (a > balance)
Console.WriteLine("Insufficient Funds.");
else
balance -= a;
}
public void PrintAccount()
{
Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}",
accountName, accountNumber, balance);
}
class SavingsAccount : Account //this is how the derived class inherits from the base class, right?
{
public SavingsAccount(string acctName, int acctNum, double acctBal, double interest)
: base(acctName, acctNum, acctBal)
{
accountName = acctName;
accountNumber = acctNum;
balance = acctBal;
interestRate = interest;
}
private double interestRate;
public double InterestRate
{
get { return interestRate; }
set
{
if (value < 0)
interestRate = 0;
else
interestRate = value;
}
}
public void CalculateInterest()
{
balance = balance * interestRate;
Console.WriteLine("Account Name:\t{0}\nAccount Number:\t{1}\nBalance:\t{2:C}\nInterest Rate:\t{3:P2}",
accountName, accountNumber, balance, interestRate);
}
public override string PrintAccount()
{
return string.Format("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nInterest Rate :\t{3:P2}",
accountName, accountNumber, balance, interestRate);
}
}
class CheckingAccount : Account
{
public CheckingAccount(string acctName, int acctNum, double acctBal, double fee)
: base(acctName, acctNum, acctBal)
{
accountName = acctName;
accountNumber = acctNum;
balance = acctBal;
feeCharged = fee;
}
private double feeCharged;
public double FeeAmmount
{
set
{
if (value < 0)
feeCharged = 0;
else
feeCharged = value;
}
}
//No idea how to "invoke the Account class and use a boolean value to see if
//money was withdrawn."
//public void Credit(double a)
//{
// balance += a;
// balance -= feeCharged;
//}
//public void Debit(double a)
//{
// if (a > balance)
// Console.WriteLine("Insufficient Funds.");
// else
// balance -= a;
// balance -= feeCharged;
//}
public override string PrintAccount()
{
return string.Format("Account Name :\t{0}\nAccount Number :\t{1)\nBalance :\t{2:C}\nFee Charged :\t{3:C}",
accountName, accountNumber, balance, feeCharged);
}
}
class AccountTest
{
static void Main(string[] args)
{
//Step 1 & 2
CheckingAccount lemmonsChecking = new CheckingAccount("Lemmons-Checking", 1, 1000, 3);
SavingsAccount lemmonsSavings = new SavingsAccount("Lemmons-Savings", 2, 2000, .05);
//Step 3 & 4
lemmonsChecking.PrintAccount();
lemmonsSavings.PrintAccount();
//Step 5 & 6
Console.WriteLine("\nDeposit $100 into checking.");
lemmonsChecking.Credit(100);
lemmonsChecking.PrintAccount();
//Step 7 & 8
Console.WriteLine("\nWithdraw $50 from checking.");
lemmonsChecking.Debit(50);
lemmonsChecking.PrintAccount();
//Step 9 & 10
Console.WriteLine("\nWithdraw $6000 from checking.");
lemmonsChecking.Debit(6000);
lemmonsChecking.PrintAccount();
//Step 11 & 12
Console.WriteLine("\nDeposit $3000 into savings.");
lemmonsSavings.Credit(3000);
lemmonsSavings.PrintAccount();
//Step 13 & 14
Console.WriteLine("\nWithdraw $200 from savings.");
lemmonsSavings.Debit(200);
lemmonsSavings.PrintAccount();
//Step 15 & 16
Console.WriteLine("\nCalculate interest on savings.");
lemmonsSavings.CalculateInterest();
//Step 17 & 18
Console.WriteLine("\nWithdraw $10,000 from savings.");
lemmonsSavings.Debit(10000);
lemmonsSavings.PrintAccount();
Console.WriteLine("\nPress the [ENTER] key to continue.");
Console.ReadLine();
//I keep receiving errors based around the override I placed on the PrintAccount()
}
}
}
}
发布于 2013-11-28 04:27:16
您的PrintAccount
方法应该在基类中标记为virtual
。Account
class...this告诉编译器,允许在派生类中重写该方法。
public virtual void PrintAccount()
{
Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1}\nBalance :\t{2:C}", accountName, accountNumber, balance);
}
发布于 2013-11-28 04:15:56
通过调用Account
类,我假设它意味着在基类中调用该方法的实现,您可以通过,
public class CheckingAccount : Account
{
// And you can use this to specify whether something
// was withdrawn from the account
private bool moneyWithdrawn;
public bool MoneyWithdrawn { get { return this.moneyWithdrawn; } }
public void Credit(double a)
{
...
base.Credit(a);
}
public void Debit(double a)
{
...
base.Debit(a);
}
}
除此之外,继承和覆盖语法看起来是正确的。你可能需要对bool
进行一些创造性的尝试。希望这能有所帮助!
https://stackoverflow.com/questions/20252134
复制相似问题