Can I have a base class where each derived class has its own copy of a static property?

前端 未结 4 2023
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 01:52

I have something like the following situation below:

class Base
{
     public static int x;
     public int myMethod()
     {
          x += 5;
          ret         


        
4条回答
  •  悲&欢浪女
    2021-01-13 02:40

    Static dictionary with Type as keys should do. I mean avoid logic repetition and different values for each derived type but shared among instances.

    public class Base
    {
        private static Dictionatry _values;
    
        public int MyMethod()
        {
            _values[this.GetType()]+=5;
            return _values[this.GetType()];
        }
    }
    

提交回复
热议问题