“Object” does not contain a definition for “name”

后端 未结 6 1972
误落风尘
误落风尘 2021-01-28 21:39

I\'m having an error message that tells me this:

\'BankAccount.account\' does not contain a definition for \'withdraw\'.

Here\'s my code:

using S         


        
6条回答
  •  清歌不尽
    2021-01-28 22:05

    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++;
            }
        }
    }
    

提交回复
热议问题