首页 文章

为什么派生类接口的引用显示基类的字段?

提问于
浏览
0

我正在创建一个带有属性的抽象基类,并在派生类中继承该基类 . 派生类继承基类和接口 . 当我尝试使用接口引用派生类的对象时,我看到基类的属性也未在派生类接口中声明 . 原始代码有点大,但跟随我遵循的方法相同

Base class:

public class PromotionBase : IPromotionBase
{
    private string CurrentPromotion = string.Empty;
    public PromotionBase(string promotionName)
    {
        CurrentPromotion = promotionName;
    }
    public DateTime? StartDate => SettingsManager.GetSettingDateTime(CurrentPromotion, nameof(this.StartDate));
    public DateTime? EndDate => SettingsManager.GetSettingDateTime(CurrentPromotion, nameof(this.EndDate));
    public string PromoCode => SettingsManager.GetSettingString(CurrentPromotion, nameof(this.PromoCode));
    public DateTime? OrderQualificationStartDate => SettingsManager.GetSettingDateTime(CurrentPromotion, nameof(this.OrderQualificationStartDate));
    public DateTime? OrderQualificationEndDate => SettingsManager.GetSettingDateTime(CurrentPromotion, nameof(this.OrderQualificationEndDate));
    public int DiscountPercentage => SettingsManager.GetSettingInt(CurrentPromotion, nameof(this.DiscountPercentage));
    public int UsageLimit => SettingsManager.GetSettingInt(CurrentPromotion, nameof(this.UsageLimit));
    public string ActivePromoTile => SettingsManager.GetSettingString(CurrentPromotion, nameof(this.ActivePromoTile));
    public string EligiblePromoTile => SettingsManager.GetSettingString(CurrentPromotion, nameof(this.EligiblePromoTile));
    public string AddedPromoTile => SettingsManager.GetSettingString(CurrentPromotion, nameof(this.AddedPromoTile));
    public string VerifiedPromoTile => SettingsManager.GetSettingString(CurrentPromotion, nameof(this.VerifiedPromoTile));
}

Derived class:

public class FreeGiftAndShippingPromotionManager : PromotionBase,IFreeGiftAndShippingPromo
{
    public FreeGiftAndShippingPromotionManager() : base(PromotionHelper.PromoName.FreeGiftAndShipping)
    {
    }
    private static Dictionary<int, ProductItemInfo> productItemInfos = null;
    private static DateTime LastProductInfoRefresh = DateTime.MinValue;
    private const int InfoRefreshInterval = 30;
    private const int LowStockLevel = 10;
    public override int GiftProductItemId
    {
        get
        {
            string defaultProductId = "0";
            if(string.IsNullOrEmpty(GiftProductItemIds).Not())
            {
                defaultProductId = "0" + GiftProductItemIds.Split(',').FirstOrDefault();
            }
            return Convert.ToInt32(defaultProductId);
        }
    }
            public ProductItemInfo GetProductInfo(int productItemId)
    {
        GetFreshProductInfos();
        return productItemInfos[productItemId];
    }
}

Derived class interface:

public interface IFreeGiftAndShippingPromo
{
    DateTime? StartDate { get; }
    DateTime? EndDate { get; }
    string PromoCode { get; }
    DateTime? OrderQualificationStartDate { get; }
    DateTime? OrderQualificationEndDate { get; }
    int PurchaseThreshold { get; }
    int DisplayPriority { get; }
    bool IsActive { get; }
    string PromoTitle { get; }
    string PromoIntro { get; }
    int GiftProductItemId { get; }
    string GiftProductItemIds { get; }
    bool ShowPopup { get; }
    bool OrderMeetsPromoMinPurchase(Order order);
    string GetCartPromoTile(Order order);
    bool OrderEligibleForFreeGift(Order order);
    string EligiblePromoTile { get; }
    string AddedPromoTile { get; }
    string VerifiedPromoTile { get; }
    ProductItemInfo GetProductInfo(int productItemId);
    string GetCartProductGridDisplay();
}

我创建了这个结构,所以当我引用派生类的接口时,我只得到派生类接口中的属性 .

但是当我像这样引用派生类的接口时

private IFreeGiftAndShippingPromo Instance_FreeGiftAndShippingPromoManager = new FreeGiftAndShippingPromotionManager();

我可以看到基类的 ActivePromoTile 属性以及所有其他基类属性 . 我可以知道为什么吗?有没有办法我只能获得在派生类接口中声明的成员?

Debugger detail

非常感谢 .

1 回答

  • 2

    调试器显示界面中保存的实际类型,即您在窗口中看到的类型 . 如果您试图在代码中实际使用 ActivePromoTile ,则代码将无法编译 .

相关问题