C# Tips:在继承中,new 与 override 的区别

来源:互联网 发布:javascript 服务端 编辑:程序博客网 时间:2024/05/17 05:17

在方法、属性的继承中,new关键字表示隐藏基类的实现,使得子类也可以定义与基类同名同形参的方法和属性。

而override关键字表示重写,基类的方法或属性在子类中被重写后,调用子类的方法或属性一定会从子类执行。


说得可能很拗口,但是举个例子一看就明白了。


先看new关键字的例子:

基类 ControlModel.cs

using System;namespace InhibitPropertyDemo{    public class ControlModel    {        private bool isChanged = true;        public bool IsChanged        {            get            {                return this.isChanged;            }            set            {                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ControlModel::IsChanged = {0}", value));                }            }        }    }}

子类 ButtonModel.cs

using System;namespace InhibitPropertyDemo{    public class ButtonModel : ControlModel    {        private bool isChanged = true;        public new bool IsChanged        {            get            {                return base.IsChanged || this.isChanged;            }            set            {                base.IsChanged = value;                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ButtonModel::IsChanged = {0}", value));                }            }        }    }}

测试主程序:Program.cs

using System;namespace InhibitPropertyDemo{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("==== ControlModel model = new ControlModel() ====");            ControlModel controlModel = new ControlModel();            controlModel.IsChanged = true;            controlModel.IsChanged = false;            Console.WriteLine(string.Empty);            Console.WriteLine("==== ButtonModel model = new ButtonModel() ====");            ButtonModel buttonModel = new ButtonModel();            buttonModel.IsChanged = true;            buttonModel.IsChanged = false;            Console.WriteLine(string.Empty);            Console.WriteLine("==== ControlModel model = new ButtonModel() ====");            ControlModel model = new ButtonModel();            model.IsChanged = true;            model.IsChanged = false;            Console.WriteLine(string.Empty);        }    }}

执行结果:


再来看看override的例子:

基类 ControlModel.cs

using System;namespace InhibitPropertyDemo{    public class ControlModel    {        private bool isChanged = true;        public virtual bool IsChanged        {            get            {                return this.isChanged;            }            set            {                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ControlModel::IsChanged = {0}", value));                }            }        }    }}

子类 ButtonModel.cs

using System;namespace InhibitPropertyDemo{    public class ButtonModel : ControlModel    {        private bool isChanged = true;        public override bool IsChanged        {            get            {                return base.IsChanged || this.isChanged;            }            set            {                base.IsChanged = value;                if (this.isChanged != value)                {                    this.isChanged = value;                    Console.WriteLine(string.Format("ButtonModel::IsChanged = {0}", value));                }            }        }    }}

测试主程序 Program.cs 不变。

执行结果:


从结果的对比中我们可以看到,如果我们希望子类 ButtonModel 的实例始终执行子类的方法和属性,应该在继承中用override关键字。



原创粉丝点击