我有一個類優惠券:與非繼承的方法處理在子類中
public abstract class Voucher
{
public int Id { get; set; }
public decimal Value { get; protected set; }
public const string SuccessMessage = "Applied";
}
和子類心願單
public class GiftVoucher : Voucher
{
}
和另一個子DiscountVoucher
public class DiscountVoucher : Voucher
{
public decimal Threshold { get; private set; }
public string FailureMessage { get { return "Please spend £{0} to use this discount"; } }
}
你可以看到DiscountVoucher有一些特定的屬性Threshold和FailureMessage,它們分別代表了amou如果用戶沒有花費這筆錢,則需要花費用於獲得折扣和失敗消息。
我的問題是這樣的。我有券對象的集合,什麼我不希望在我的代碼做的是這樣的
if (voucher is DiscountVoucher)
{
// cast voucher to a DiscountVoucher and then call the specific methods on it
}
東西,因爲這不是在所有的維護。同時,我不想將這些具體方法放在憑證抽象類中,因爲它們不適用於所有類型的憑證。有誰知道如何設計這個功能?
您必須對您的收藏執行哪些任務? – foowtf