代码之家  ›  专栏  ›  技术社区  ›  nfplee

父/子体系结构问题

  •  0
  • nfplee  · 技术社区  · 14 年前

    public class Transaction
    {
       public int TransactionID { get; set; }
       public TransactionTypes Type { get; set; } // Enum for the type of transaction
       public decimal Amount { get; set; }
       public virtual decimal GrandTotal { get; set; } // In this case this would simply be the Amount
    }
    
    public class MembershipTransaction : Transaction
    {
       public decimal ExtraAmount { get; set; }
       public override decimal GrandTotal { get { return base.GrandTotal + ExtraAmount; } }
    }
    

    我想知道这笔交易的总额是否应该自动包括额外的金额。这样做的好处是,如果我得到所有的事务,无论事务类型如何,GrandTotal的数字都是正确的。使用上述逻辑,我当前必须切换每个事务类型并返回派生类型的GrandTotal。

    如果有人能帮我澄清一下,我会很感激的。谢谢

    3 回复  |  直到 14 年前
        1
  •  2
  •   Fredrik Mörk    14 年前

    总计是一个总计,因此,如果它包括 ExtraAmount . 在代码可能只需要关于基类的知识的情况下,这也是有意义的 Transaction 获取正确的 GrandTotal 价值观。

    TransactionTypes

        2
  •  2
  •   Adriaan Stander    14 年前

    每个事务类型都应该知道如何计算自己的额外金额(为零、金额百分比、固定括号内的金额),以便业务逻辑驻留在重写基/抽象类的类中。

    这将允许您在不知道实际事务类型的情况下使用总计。

        3
  •  1
  •   Si Robinson    14 年前

    ExtraAmount和GrandTotal的计算应该包含在基类中。

    希望有帮助。