【发布时间】:2017-06-20 08:56:54
【问题描述】:
我正在用 C# 实现一个银行模块,其中包含一个储蓄账户、一个支票账户和一个简易储蓄账户。所有账户都有一个所有者和一个余额,他们都可以取款和存款,但他们不能提取超过余额。到这里,很容易。现在,我们的储蓄帐户有一个新方法 applyInterest 和 checksAccount 一个方法 deductFees 和 easySavinAccount 两者都有。我想到的是使用抽象类Account:
public abstract class Account
{
protected string owner { get; set; }
//always use decimal especially for money c# created them for that purpose :)
protected decimal balance { get; set; }
public void deposit(decimal money)
{
if (money >= 0)
{
balance += money;
}
}
public void withdraw(decimal money)
{
if (money > balance)
{
throw new System.ArgumentException("You can't withdraw that much money from your balance");
}
else balance -= money;
}
}
这将被所有 3 个类继承。是否有适合以更好的方式实现这一点的设计模式?特别是对于easySaveAccount,组合可能会有所帮助吗?
谢谢!
【问题讨论】:
标签: c# oop design-patterns