Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dealing with non-inherited methods in a subclass
- public abstract class Voucher
- {
- public int Id { get; set; }
- public decimal Value { get; protected set; }
- public const string SuccessMessage = "Applied";
- }
- public class GiftVoucher : Voucher
- {
- }
- public class DiscountVoucher : Voucher
- {
- public decimal Threshold { get; private set; }
- public string FailureMessage { get { return "Please spend £{0} to use this discount"; } }
- }
- if (voucher is DiscountVoucher)
- {
- // cast voucher to a DiscountVoucher and then call the specific methods on it
- }
- public virtual string FailureMessage { get { return string.Empty; } }
- public abstract class Voucher
- {
- public int Id { get; set; }
- public decimal Value { get; protected set; }
- public const string SuccessMessage = "Applied";
- public decimal Threshold { get { return 0.0; } }
- public string FailureMessage { get { return ""; } }
- }
- public abstract class Voucher
- {
- public bool isValid(ShoppingCart sc);
- public string FailureMessage { get { return "This voucher does not apply"; } }
- // ...
- }
- public class DiscountVoucher : Voucher
- {
- private decimal Threshold;
- public override bool isValid(ShoppingCart sc)
- {
- return (sc.total >= Threshold);
- }
- public override string FailureMessage
- {
- get { return FormatString("Please spend £{0} to use this discount", Threshold); }
- }
- public abstract class Voucher
- {
- public int Id { get; set; }
- public decimal Value { get; protected set; }
- public virtual string SuccessMessage { get { return "Applied"; } }
- public virtual string FailureMessage { get { return String.Empty; } }
- public virtual bool Ok { get { return true; } }
- }
- public class GiftVoucher : Voucher { }
- public class DiscountVoucher : Voucher
- {
- public decimal Threshold { get; private set; }
- public override string FailureMessage { get { return "Please spend £{0} to use this discount"; } }
- public override bool Ok { get { return Value >= Threshold; } }
- }
- if (voucher.Ok) {
- Console.WriteLine(voucher.SuccessMessage);
- } else {
- Console.WriteLine(voucher.FailureMessage);
- }
Add Comment
Please, Sign In to add comment