简体   繁体   English

“对象”不包含“名称”的定义

[英]“Object” does not contain a definition for “name”

I'm having an error message that tells me this:我收到一条错误消息,告诉我:

'BankAccount.account' does not contain a definition for 'withdraw'. “BankAccount.account”不包含“提款”的定义。

Here's my code:这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccounts
{
class account
{
    protected string name;
    protected float balance;
    public account(string n, float b)
    {
        name = n;
        balance = b;
    }

    public void deposit(float amt)
    {
        balance -= amt;
    }

    public void display()
    {
        Console.WriteLine("Name: {0}. Balance: {1}.", name, balance);
    }
}

class savingaccount : account
{
    static int accno = 1000;
    int trans;
    public savingaccount(string s, float b) : base(s, b)
    {
        trans = 0;
        accno++;
    }
    public 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++;
        }
    }
    public void deposit(float amt)
    {
        if (trans >= 10)
        {
            Console.WriteLine("Number of transactions exceed 10.");
            return;
        }
        base.deposit(amt);
        trans++;
    }
    public void display()
    {
        Console.WriteLine("Name: {0}. Account no.: {1}. Balance: {2}", name, accno,        balance);
    }
}

class currentaccount : account
{
    static int accno = 1000;
    public currentaccount(string s, float b) : base(s, b)
    {
        accno++;
    }
    public void withdraw(float amt)
    {
        if (balance - amt < 0)
            Console.WriteLine("No balance in account.");
        else
            balance -= amt;
    }
    public void display()
    {
        Console.WriteLine("Name: {0}. Account no.: {1}. Balance: {2}.", name, accno, balance);
    }
}

} }

I don't understand why it doesn't recognize it.我不明白为什么它不识别它。 It is a method in the class savingaccount.它是类 Savingaccount 中的一个方法。

You're calling你打电话

base.withdraw(amt);

from your class savingsaccount .来自你的班级savingsaccount But the base class ( account ) has no such method.但是基类( account )没有这样的方法。 So the compiler is absolutely correct.所以编译器是绝对正确的。

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.请注意,我将 "deposit" 更改为+= ,并使方法成为virtual方法,以便子类可以override该方法,这是(我强烈怀疑)这里的意图。 Additionally, float is a really bad choice for storing money.此外, float是存储资金的一个非常糟糕的选择。 decimal might be a better choice. decimal可能是更好的选择。 As a stylistic change, I also capitalized the names.作为文体上的变化,我也将名称大写。

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 .我假设你的意图是要定义基本withdraw在基法account类,因此,它会通过两个继承savingaccountcurrentaccount You should declare it as virtual in order to allow it to be overridden by the derived classes, if required.如果需要,您应该将其声明为virtual以便允许派生类覆盖它。

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. currentaccount类想必不需要修改这个继承方法的逻辑,所以可以完全省略。 On the other hand, in your savingaccount class, you can override the method to implement your custom behaviour:另一方面,在您的savingaccount类中,您可以覆盖该方法以实现您的自定义行为:

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

您在savingaccount类中调用base.withdraw(amt) ,但基类account未定义此方法。

If you look closely, you'll see that your account class in fact doesn't have a withdraw method.如果您仔细观察,您会发现您的account类实际上没有withdraw方法。

I'd guess you meant to have your account class contain a virtual method withdraw , defined like this: public virtual void withdraw(float amt) { ... }我猜你的意思是让你的account类包含一个虚拟方法withdraw ,定义如下: 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 :)您的代码也存在命名样式问题,但这可能不在此问题的范围内:)

正如大家已经指出的,你应该在你的基类账户上声明withdraw()方法,这样所有的派生类都可以继承该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM