【问题标题】:Hi I am trying to understand this Account class constructor嗨我想了解这个 Account 类构造函数
【发布时间】:2022-06-17 07:23:39
【问题描述】:

命名空间分支演示 { 内部接口 IAccountState { IAccountState Deposit(Action addToBalance); IAccountState 取款(操作减法余额); IAccountState 冻结(); IAccountState HolderVerified(); IAccountState Close();

}

}

命名空间分支演示 { 内部类账户 { 公共小数余额 { 得到;私人套装; }

    private IAccountState State { get; set; }

问题是如何使用这个构造函数?

    public Account(Action onUnfreeze)
    {
        this.State = new NotVerified(onUnfreeze);
    }

    
    public void Deposit(decimal amount)
    {
        this.State = this.State.Deposit(() => { this.Balance += amount; });
    }

    
    public void Withdraw(decimal amount)
    {
        this.State = this.State.Withdraw(() => { this.Balance -= amount; });
    }

    public void HolderVerified()
    {
        this.State = this.State.HolderVerified();
    }

    public void Close()
    {
        this.State = this.State.Close();
    }

    public void Freeze()
    {
        this.State = this.State.Freeze();
    }

}

}

命名空间分支演示 { 内部类 Active : IAccountState { 私人操作 OnUnfreeze { 获取; }

    public Active(Action onUnfreeze)
    {
        this.OnUnfreeze = onUnfreeze;
    }

    public IAccountState Deposit(Action addToBalance)
    {
        addToBalance();
        return this;
    }

    public IAccountState Withdraw(Action subtractFromBalance)
    {
        subtractFromBalance();
        return this;
    }

    public IAccountState Freeze() => new Frozen(this.OnUnfreeze);
    public IAccountState HolderVerified() => this;
    public IAccountState Close() => new Closed();

}

}

命名空间分支演示 { 内部类已关闭:IAccountState { public IAccountState Deposit(Action addToBalance) => this; 公共 IAccountState 取款(操作减法余额)=> 这个; 公共 IAccountState 冻结() => 这个; 公共 IAccountState HolderVerified() => 这个; public IAccountState Close() => this;

}

}

命名空间分支演示 { 内部类冻结:IAccountState { 私人操作 OnUnfreeze { 获取; }

    public Frozen(Action onUnfreeze)
    {
        this.OnUnfreeze = onUnfreeze;
    }

    public IAccountState Deposit(Action addToBalance)
    {
        this.OnUnfreeze();
        addToBalance();
        return new Active(this.OnUnfreeze);
    }

    public IAccountState Withdraw(Action subtractFromBalance)
    {
        this.OnUnfreeze();
        subtractFromBalance();
        return new Active(this.OnUnfreeze);
    }

    public IAccountState Freeze() => this;
    public IAccountState HolderVerified() => this;
    public IAccountState Close() => new Closed();

}

}

命名空间分支演示 { 内部类 NotVerified : IAccountState { 私人操作 OnUnfreeze { 获取; }

    public NotVerified(Action onUnfreeze)
    {
        this.OnUnfreeze = onUnfreeze;
    }

    public IAccountState Close() => new Closed();

    public IAccountState Deposit(Action addToBalance)
    {
        addToBalance();
        return this;
    }

    public IAccountState Freeze() => this;

    public IAccountState HolderVerified() => new Active(this.OnUnfreeze);

    public IAccountState Withdraw(Action subtractFromBalance) => this;

}

}

【问题讨论】:

  • 您发布的代码似乎超出了您的问题的合理范围。请阅读How to Ask 以及如何制作minimal reproducible example;提供 MRE 可帮助用户回答您的问题,并帮助未来的用户与您的问题相关。

标签: c#


猜你喜欢
  • 2014-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多