覆盖父常量变量

本文关键字:变量 常量 覆盖 | 更新日期: 2023-09-27 18:21:05

我试图覆盖类常量变量,但我的方法不起作用。

对于以下代码:

// The following line of code should output "DATE: 9/9/2014"
// But it outputs: "DATE: 31/3/2015"
CustomDateChecker.checkUsedByDate(); 

在以下类别中;如何覆盖变量USED_BY_YEARUSED_BY_MONTHUSED_BY_DAY

public class DateChecker
{
    public const uint                   USED_BY_YEAR            = 2015;
    public const uint                   USED_BY_MONTH           = 3;
    public const uint                   USED_BY_DAY             = 31;
    public static bool checkUsedByDate()
    {
        TaskDialog.MessageBox.Show(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
    }
}
public class CustomDateChecker : DateChecker
{
    // Override Date
    public const uint                   USED_BY_YEAR            = 2014;
    public const uint                   USED_BY_MONTH           = 9;
    public const uint                   USED_BY_DAY             = 9;
}

编辑:阅读建议后,我将变量更改为static readonly,而不是const。但不幸的是,同样的问题,日期没有改变。使用static readonly是一种可能的解决方案吗?

public class DateChecker
{
    public static readonly uint                   USED_BY_YEAR            = 2015;
    public static readonly uint                   USED_BY_MONTH           = 3;
    public static readonly uint                   USED_BY_DAY             = 31;
    public static bool checkUsedByDate()
    {
        TaskDialog.MessageBox.Show(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
    }
}
public class CustomDateChecker : DateChecker
{
    // Override Date
    public static new readonly uint                   USED_BY_YEAR            = 2014;
    public static new readonly uint                   USED_BY_MONTH           = 9;
    public static new readonly uint                   USED_BY_DAY             = 9;
}

覆盖父常量变量

public class DateChecker
    {
        public readonly uint USED_BY_YEAR = 2015;
        public readonly uint USED_BY_MONTH = 3;
        public readonly uint USED_BY_DAY = 31;
        public DateChecker()
        {
        }
        public DateChecker(uint d, uint m, uint y)
        {
            this.USED_BY_DAY = d;
            this.USED_BY_MONTH = m;
            this.USED_BY_YEAR = y;
        }
        public bool checkUsedByDate()
        {
            Console.WriteLine(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
            return false;
        }
    }
    public class CustomDateChecker : DateChecker
    {
        // Override Date
        public new const uint USED_BY_YEAR = 2014;
        public new const uint USED_BY_MONTH = 9;
        public new const uint USED_BY_DAY = 9;
        public CustomDateChecker()
            : base(USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR)
        {
        }

    }
    private static void Main(string[] args)
    {
        CustomDateChecker d = new CustomDateChecker();
        d.checkUsedByDate();
    }
  1. 您正试图覆盖字段或成员。重写是与方法/属性关联的功能,而不是与类的字段或成员关联的功能
  2. 这里的另一个问题是,您试图使用静态方法来获得多态行为,但这不会成功。静态方法与类关联,而不是与类的实例关联。因此,静态方法不能与针对它的虚拟/重写限定符一起使用。为此,应该使用实例方法/非静态方法或属性
  3. 看看下面的例子是否有帮助。

            public class DateChecker
            {
                public virtual uint USED_BY_YEAR { get; set; }
                public virtual uint USED_BY_MONTH { get; set; }
                public virtual uint USED_BY_DAY { get; set; }
                public DateChecker()
                {
                    USED_BY_YEAR = 2015;
                    USED_BY_MONTH = 3;
                    USED_BY_DAY = 31;
                }
                public virtual void checkUsedByDate()
                {
                    Console.Write(string.Format("DATE: {0}/{1}/{2}", USED_BY_DAY, USED_BY_MONTH, USED_BY_YEAR));
                }
            }
         public class CustomDateChecker : DateChecker
        {
            public override uint USED_BY_DAY
            {
                get
                {
                    return 9;
                }
                set
                {
                    base.USED_BY_DAY = value;
                }
            }
            public override uint USED_BY_MONTH
            {
                get
                {
                    return 9;
                }
                set
                {
                    base.USED_BY_MONTH = value;
                }
            }
            public override uint USED_BY_YEAR
            {
                get
                {
                    return 2014;
                }
                set
                {
                    base.USED_BY_YEAR = value;
                }
            }        
        }
        class Program
        {        
            static void Main(string[] args)
            {
                new CustomDateChecker().checkUsedByDate();
            }
        }