【问题标题】:Why reference of derived class interface shows field of the base class?为什么派生类接口的引用显示基类的字段?
【发布时间】:2016-05-28 12:19:58
【问题描述】:

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

基类:

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));
}

派生类:

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];
    }
}

派生类接口:

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

非常感谢。

【问题讨论】:

  • 您能否提供代码行来阐明“我可以看到 ActivePromoTile 属性”的含义? (显然你可以在调试器中看到源代码和属性,但我怀疑这就是你要问的)。
  • @AlexeiLevenkov 我添加了调试器的屏幕截图。希望对理解有所帮助。如果您需要其他任何东西,请告诉我。我很乐意提供更多细节。

标签: c# class inheritance interface


【解决方案1】:

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

【讨论】:

  • 谢谢@Scott。当我尝试使用它并没有在智能感知中显示时,它是真的。我的错是我太专注于调试器显示我忘记尝试使用该实例并检查的属性原因。
猜你喜欢
  • 1970-01-01
  • 2013-09-30
  • 2023-03-12
  • 2020-05-11
  • 2012-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多