如何调用基类以及继承和重写?

时间:2013-11-27 20:07:55

标签: c# oop inheritance override invoke

好的,这就是多个问题集于一身。老实说,我需要一位导师或某人亲自交谈,以清理其中的一些概念,但我正在网上接受这个c#课,教授拒绝与我沟通,所以我的下一步就是来这里。我知道这是很多材料,但任何指导都会非常感激。

首先,这个赋值的目的是在类中使用继承。基本上你应该创建一个名为Account的基类,它初始化你的所有变量并为每个变量设置get和sets(我不是完全如何利用它们)以及设置Credit和Debit方法。最后创建一个PrintAccount方法,该方法将在派生类中进行修改。

接下来,创建一个SavingsAccount类,它继承Account中的所有内容(我认为我在创建类时使用“:Account”正确执行了此操作)。在其中,您应该使用override修改PrintAccount方法,以便在调用时提供其他信息。之后,创建一个继承自Account的CheckingAccount类。现在您必须修改借记和贷记以及PrintAccount。方向说通过“调用Account类并使用布尔值来查看是否撤回资金来修改借方/贷方”。不知道这意味着什么。此外,您应该再次覆盖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()


            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的PrintAccount方法应该在基础virtual类中标记为Account ...这告诉编译器允许在派生类中重写它。

public virtual void PrintAccount()
{
    Console.WriteLine("Account Name :\t{0}\nAccount Number :\t{1}\nBalance :\t{2:C}", accountName, accountNumber, balance);
}

答案 1 :(得分:0)

通过调用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获得创意。希望有所帮助!