建议94:区别对待override和new

建议94:区别对待override和new

override和new使类型体系应为继承而呈现出多态性。多态要求子类具有与基类同名的方法,override和new的作用就是:

  • 如果子类中的方法前面带有new关键字,则该法被定义为独立于基类的方法。
  • 如果子类中的方法前面带有override关键字,则子类的对象将调用该方法。而不调用基类的方法。

我们来看一个继承体系:

复制代码
        public class Shape
        {
            public virtual void MethodVirtual()
            {
                Console.WriteLine("base MethodVirtual call");
            }

            public void Method()
            {
                Console.WriteLine("base Method call");
            }
        }

        class Circle : Shape
        {
            public override void MethodVirtual()
            {
                Console.WriteLine("circle override MethodVirtual");
            }
        }

        class Rectangle : Shape
        {

        }

        class Triangle : Shape
        {
            public new void MethodVirtual()
            {
                Console.WriteLine("triangle new MethodVirtual");
            }

            public new void Method()
            {
                Console.WriteLine("triangle new Method");
            }
        }

        class Diamond : Shape
        {
            public void MethodVirtual()
            {
                Console.WriteLine("Diamond default MethodVirtual");
            }

            public void Method()
            {
                Console.WriteLine("Diamond default Method");
            }
        }
复制代码

Shape是所有子类的基类。

Circle类override父类的MethodVirtual,所以即使子类转型为Shape,调用的还是子类方法:

            Shape s = new Circle();
            s.MethodVirtual();
            s.Method();

输出为:

circle override MethodVirtual
base Method call

            Circle s = new Circle();
            s.MethodVirtual();
            s.Method();

输出也为:

circle override MethodVirtual
base Method call

类型Rectangle没有对基类做任何处理,所以无论是否转型为Shape,调用的都是基类Shape的方法。
类型Triangle将基类Shape的virtual方法和非virtual方法都new了一般,所以第一种方法为:

            Shape s = new Triangle();
            s.MethodVirtual();
            s.Method();

因为子类应经new了父类的方法,故子类方法和基类方法完全没有关系了,只要s被转型为Shape,针对s调用搞得都是父类方法。

            Triangle triangle = new Triangle();
            triangle.MethodVirtual();
            triangle.Method();

调用的都是子类方法,输出为:

triangle new MethodVirtual
triangle new Method


类型Diamond包含了两个和基类一模一样的方法,并且没有额外的修饰符。这在编译器中会提出警示。但是如果选择忽略这些警示,程序还是一样可以运行。

            Shape s=new Diamond();
            s.MethodVirtual();
            s.Method();

编译器会默认new的效果,所以输出和显示设置为new时一样。

输出为:

base MethodVirtual call
base Method call

            Diamond s = new Diamond();
            s.MethodVirtual();
            s.Method();

输出为:

Diamond default MethodVirtual
Diamond default Method
最后给一个综合示例:

复制代码
 static void Main(string[] args)
        {
            TestShape();
            TestDerive();
            TestDerive2();
        }

        private static void TestShape()
        {
            Console.WriteLine("TestShape\tStart");
            List<Shape> shapes = new List<Shape>();
            shapes.Add(new Circle());
            shapes.Add(new Rectangle());
            shapes.Add(new Triangle());
            shapes.Add(new Diamond());
            foreach (Shape s in shapes)
            {
                s.MethodVirtual();
                s.Method();
            }
            Console.WriteLine("TestShape\tEnd\n");
        }

        private static void TestDerive()
        {
            Console.WriteLine("TestDerive\tStart");
            Circle circle = new Circle();
            Rectangle rectangle = new Rectangle();
            Triangle triangel = new Triangle();
            Diamond diamond = new Diamond();
            circle.MethodVirtual();
            circle.Method();
            rectangle.MethodVirtual();
            rectangle.Method();
            triangel.MethodVirtual();
            triangel.Method();
            diamond.MethodVirtual();
            diamond.Method();
            Console.WriteLine("TestShape\tEnd\n");
        }

        private static void TestDerive2()
        {
            Console.WriteLine("TestDerive2\tStart");
            Circle circle = new Circle();
            PrintShape(circle);
            Rectangle rectangle = new Rectangle();
            PrintShape(rectangle);
            Triangle triangel = new Triangle();
            PrintShape(triangel);
            Diamond diamond = new Diamond();
            PrintShape(diamond);
            Console.WriteLine("TestDerive2\tEnd\n");
        }

        static void PrintShape(Shape sharpe)
        {
            sharpe.MethodVirtual();
            sharpe.Method();
        }
复制代码

输出为:

TestShape       Start
circle override MethodVirtual
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
TestShape       End

TestDerive      Start
circle override MethodVirtual
base Method call
base MethodVirtual call
base Method call
triangle new MethodVirtual
triangle new Method
Diamond default MethodVirtual
Diamond default Method
TestShape       End

TestDerive2     Start
circle override MethodVirtual
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
base MethodVirtual call
base Method call
TestDerive2     End



 

转自:《编写高质量代码改善C#程序的157个建议》陆敏技

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值