将操作类型作为参数传递c#

时间:2017-09-28 23:35:56

标签: c#

有没有办法将ActionType作为参数传递给一个新方法?我想将新的单一方法称为另外两种不同的方法(提取和存款)。

class Account
{

    enum ActionType
    {
        Withdraw,
        Deposit,
    }

    private void WriteDeposit()
    {
        StreamWriter outputFile;
        outputFile = File.AppendText("account.log");

        outputFile.WriteLine("{0},{1}", DateTime.Now, ActionType.Deposit);
        outputFile.Close();
    }

    private void WriteWithdraw()
    {
        StreamWriter outputFile;
        outputFile = File.AppendText("account.log");

        outputFile.WriteLine("{0},{1}", DateTime.Now, ActionType.Withdraw);
        outputFile.Close();
    }

1 个答案:

答案 0 :(得分:2)

根据您的问题,以下内容应该有效。

class Account
{

    enum ActionType
    {
        Withdraw,
        Deposit,
    }

    public void Withdraw()
    {
        WriteAction(ActionType.Withdraw);
    }

    private void WriteAction(ActionType action)
    {
        StreamWriter outputFile;
        outputFile = File.AppendText("account.log");

        outputFile.WriteLine("{0},{1}", DateTime.Now, action);
        outputFile.Close();
    }
}