C#重写和覆写区别

    class Model
    {       
        public virtual void Test1()
        {
            UnityEngine.Debug.Log("Model Test1");
        }

        public virtual void Test2()
        {
            UnityEngine.Debug.Log("Model Test2");
        }
    }

  


class PlayerModelLocator:Model
    {
        static PlayerModelLocator mInstance = null;
        public static PlayerModelLocator Instance
        {
            get
            {
                if (mInstance == null)
                    mInstance = new PlayerModelLocator();
                return mInstance;
            }
        }
        public override void Reset()
        {
            mInstance = null;
        }
        //覆写
        public new void Test1()
        {
            Debug.Log("PlayerModelLocator Test1");
        }
        //重新
        public override void Test2()
        {
            Debug.Log("PlayerModelLocator Test2");
        }
    }


PlayerModelLocator.Instance.Test1();
            PlayerModelLocator.Instance.Test2();
            Model playerModel = PlayerModelLocator.Instance as Model;
            playerModel.Test1();
            playerModel.Test2();



打印结果:

   PlayerModelLocator Test1

   PlayerModelLocator Test2

   Model Test1

  PlayerModelLocator Test2

你可能感兴趣的:(C#基础)