override 和 new的区别

override 和 new 的作用就是:

如果子类的方法前面带有new关键字,则该方法被定义为独立于基类的方法。

如果子类的方法前面带有override关键字,则子类的对象将调用该方法,而不是调用基类方法。

说简单点就是若方法前面带new,则子类使用该方法时使用的是new的方法,但若子类转型成父类,用的是父类原来的方法。

说简单点就是若方法前面带override,则无论子类是否转型为父类,均会使用override的方法。


若子类的方法没有带new或override则系统默认是new,但会出现警告。使用override,父类对应的方法应该是virtual修饰的。

示例如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tip94
{
    class Program
    {
        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();
        }


        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");
            }
        }

    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值