C# Member variable overrides used by base class method

前端 未结 5 943
刺人心
刺人心 2021-01-06 00:21

OK so I admit right off the top that this is a bit screwy … but it does serve a logical purpose. I’m using C# for a current project and I’m trying to find a way to override

5条回答
  •  青春惊慌失措
    2021-01-06 00:35

    class BaseClass
    {
        public virtual string Method()
        {
            return string.Empty;
        }
    }
    
    abstract class BaseClass : BaseClass where T : BaseClass
    {
        protected static string[] strings;
    
        public override string Method()
        {
            return string.Join("  ", strings);
        }
    }
    
    class Subclass1 : BaseClass
    {
        static Subclass1()
        {
            strings = new[] { "class1value1", "class1value2", "class1value3" };
        }
    }
    
    class Subclass2 : BaseClass
    {
        static Subclass2()
        {
            strings = new[] { "class2value1", "class2value2", "class2value3" };
        }
    }
    

    The important part is the generic parameter T which basically functions as an index to the string arrays.

提交回复
热议问题