Can't define static abstract string property

后端 未结 5 1106
别跟我提以往
别跟我提以往 2021-02-19 10:03

I\'ve run into an interesting problem and am looking for some suggestions on how best to handle this...

I have an abstract class that contains a static method that accep

5条回答
  •  爱一瞬间的悲伤
    2021-02-19 10:55

    Just use new to override a static method in a derived class. Nothing that makes new a bad thing to do for virtual methods and properties applies since the type name must be supplied:

    public class BaseClass
    {
        public static int Max { get { return 0; } }
    }
    
    public class InteriorClass : BaseClass
    {
    }
    
    public class DerivedClass : InteriorClass
    {
        public new static int Max { get { return BaseClass.Max + 1; } }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("BaseClass.Max = {0}", BaseClass.Max);
            Console.WriteLine("InteriorClass.Max = {0}", InteriorClass.Max);
            Console.WriteLine("DerivedClass.Max = {0}", DerivedClass.Max);
            Console.ReadKey();
        }
    }
    

提交回复
热议问题