简体   繁体   English

在其他方法中调用参数化方法

[英]Calling Parameterized Methods in Other Methods

I don't know if I have chosen a correct headline, but I do hope I have. 我不知道我是否选择了正确的标题,但我希望我有。

I am currently trying to get a better understand of methods in C#, and in doing so I thought that I'd make a simple BankAccount example. 我目前正在尝试更好地理解C#中的方法,并且这样做的时候,我认为我会做一个简单的BankAccount示例。

So, what I have is this: 所以,我有这个:

  1. I have three methods: 我有三种方法:
    • a method to "deposit" money. 一种“存”钱的方法。
    • a method to "withdraw" money. 一种“提取”资金的方法。
    • a method to print everything (name, balance). 一种打印所有内容(名称,余额)的方法。

class BankAccount
{
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
    private string _Name;

    public int Balance
    {
        get { return _Balance; }
        set { _Balance = value; }
    }
    private int _Balance;

    public BankAccount(string name)
    {
        Name = name;
        Balance = 1000;
    }

    // deposit money
    public int Deposit(int balance)  
    {
        Balance += balance;

        return Balance;
    }

    // withdraw money
    public int WithDraw(int balance) 
    {
        Balance -= balance;

        return Balance;
    }

    // print to console
    public void Print()
    {
        Console.WriteLine("Owner: " + Name
             + "\nYour current balance is: $" + Balance);             
    }
}

What I want to do is this: 我想做的是这样的:

if I call "deposit" in Main and pass it a value, I want the print method to show me the amount (same goes for "withdraw"). 如果我在Main中调用“ deposit”并传递一个值,我希望print方法向我显示金额(“ withdraw”也是如此)。

How do I achieve this? 我该如何实现? I have tried some controle statements, but I don't know how to do this correctly with methods that have parameters? 我已经尝试了一些controle语句,但是我不知道如何使用具有参数的方法正确地执行此操作?

I hope that someone can shed some light on this issue of mine. 我希望有人可以阐明我的这个问题。

What you can do is overload the method to do more than one thing, for example you can create an overload that takes an int (the balance being subtracted or added) and a string saying which action happening, then you can have this method in the code along with the already existing one 您可以做的就是重载该方法要做的一件事情,例如,您可以创建一个重载,该重载需要一个int (减去或添加余额)和一个string ,该string说明发生了什么动作,然后您可以在代码以及已经存在的代码

public void Print(int balance, string action)
{
    Console.WriteLine("Owner: " + Name
         + "\nYour current balance is: $" + Balance
         + "and you " + action + ": $" + balance);
}

This could be used by passing the string action as "withdrew" or "deposited" depending on which method calls it. 可以通过将字符串action传递为"withdrew""deposited"具体取决于调用该方法的方法。

Using this overload allows you to both output the original Print method, if they want to know their balance but never withdrew or deposited, and the new version all depending on which parameters you pass 使用此重载,既可以输出原始的Print方法(如果他们想知道其余额但从不撤消或沉积),也可以输出新版本,具体取决于您传递的参数

For more information on overloading see this MSDN page 有关超载看到更多的信息, 这个 MSDN页

Example Usage: 用法示例:

public int Deposit(int balance)  
{
    Balance += balance;
    Print(balance, "deposited"); //prints current balance AND action that was completed
    return Balance;
}

public void ShowBalance()
{
    Print(); //Just prints current balance
}

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

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