-2

我有一个使用这种方法的基本类,包括

public class Account
{
    //MEMBERS
    private int acctNo;
    protected double balance;
    public double deposit;


    // CONSTRUCTORS
    public Account() //member intitilization     
    {
        acctNo = 54534190;
        balance = 7500;
        deposit= 1500;



    }

    //PROPERTIES 
    public int AcctNo
    {
        get {return acctNo; }
        set {acctNo = value; }
    }
    public double Balance
    {
        get { return balance; }
        set { balance = value; }
    }

    public double Deposit
    {
        get {return deposit; }
        set   {deposit = value; }
    }
public virtual double getDeposit (double amount)
{
    double transactionAmt=0.00;
    if (amount>0)
    {
        balance+=amount;
       transactionAmt= amount;
    }
    return transactionAmt;
}

现在在我的实际程序中,我试图输出这个方法。我的 writeline 会是什么样子?

我试着写这个:

 static void Main(string[] args)
    {
        Console.WriteLine("CREATING ACCOUNT");
        Account myAcctDefault = new Account();

        DumpContents(myAcctDefault);
        Pause();
      }


    static void DumpContents(Account account)
    {

        Console.WriteLine(" output {0}", account.getDeposit());
    }

我收到一条错误消息:

方法 'getDeposit' 没有重载需要 0 个参数。

我做错了什么,我试图输出这个方法不正确吗?

任何帮助、见解或建议都会非常有帮助。

我是 c# 的新手,我敢肯定你能说出来。在这种情况下输出方法的正确过程是什么?

4

3 回答 3

10

我收到一条错误消息,提示“方法 'getDeposit' 没有重载需要 0 个参数”。我究竟做错了什么

正是它所说的。这是您的方法调用:

Console.WriteLine(" output {0}", account.getDeposit());

...这是方法声明:

public virtual double getDeposit (double amount)

请注意该方法如何声明参数 - 但您没有提供参数。要么你需要去掉参数,要么你需要在方法调用中添加一个参数。或者您需要更改为使用不同的方法 - 一种不会改变帐户余额的方法。(在这种情况下,您似乎不太可能这样做。)也许您应该添加一个Balance属性:

// Please note that this should probably be decimal - see below
public double Balance { get { return balance; } }

然后调用它:

Console.WriteLine(" output {0}", account.Balance);

此外:

  • 对于财务数量,通常比decimal使用double. 阅读我关于十进制浮点二进制浮点的文章以获取更多信息。
  • 您的getDeposit方法不遵循 .NET 命名约定,其中(至少公共)方法以 PascalCase 命名,并带有前导大写字母
  • 您的getDeposit方法的名称很奇怪,因为它不是“获得”存款 - 它正在存款(并返还余额)
  • 您的getDeposit方法总是返回传递给它的值,除非它是负数。这对我来说似乎很奇怪 - 如果它要返回任何东西,它不应该返回余额吗?
  • 你的getDeposit方法默默地忽略了负存款。我希望这会引发错误,因为尝试进行负存款表示 IMO 编程错误。
于 2013-10-23T06:44:08.247 回答
6

你的getDeposit方法接受一个你没有传递给它的参数。取决于您要实现的目标是将值传递给方法:

static void DumpContents(Account account)
{
    double deposit = 1000;
    Console.WriteLine(" output {0}", account.getDeposit(deposit));
}

或从方法签名中删除此参数参数。

于 2013-10-23T06:44:22.817 回答
1
//You have to pass a double value into the method, because there is only one method    
//and wants a double paramater: 

//this is what you created: 
public double getDeposit(double amount) // <-
{
    double transactionAmt = 0.00;
    if (amount > 0)
    {
    balance += amount;
    transactionAmt = amount;
    }
    return transactionAmt;
}

//This how you should call it: 
static void DumpContents(Account account)
{
    Console.WriteLine(" output {0}", account.getDeposit(34.90)); //<- 
}
于 2013-10-23T07:24:54.133 回答