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

后端 未结 6 1970
误落风尘
误落风尘 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:04

    You're calling

    base.withdraw(amt);
    

    from your class savingsaccount. But the base class (account) has no such method. So the compiler is absolutely correct.

    0 讨论(0)
  • 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++;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-28 22:07

    As everybody pointed out allready, you should declare the withdraw() method on your base class acocunt, so all the derived classes can inherit the method.

    0 讨论(0)
  • 2021-01-28 22:08

    You call base.withdraw(amt) in your savingaccount class, but the base class account does not define this method.

    0 讨论(0)
  • 2021-01-28 22:10

    It looks like you simply missed the method from the base type:

    public virtual void Deposit(float amt)
    {
        balance += amt;
    }
    public virtual void Withdraw(float amt)
    {
        balance -= amt;
    }
    

    Note I changed "deposit" to +=, and made the method virtual so that subclasses can override the method, which is (I strongly suspect) what the intent is here. Additionally, float is a really bad choice for storing money. decimal might be a better choice. As a stylistic change, I also capitalized the names.

    0 讨论(0)
  • 2021-01-28 22:14

    If you look closely, you'll see that your account class in fact doesn't have a withdraw method.

    I'd guess you meant to have your account class contain a virtual method withdraw, defined like this: public virtual void withdraw(float amt) { ... }

    Then, in your inherited classes you'd want to override this method, like so:

    class currentaccount : account
    {
        public override void withdraw(float amt)
        {
            ...
            base.withdraw(amt)
            ...
        }
        ...
    }
    

    There's also a naming style issue with your code, but this is probably not in the scope of this question :)

    0 讨论(0)
提交回复
热议问题