zoukankan      html  css  js  c++  java
  • new 和 override 的区别

    new是继承类对基类方法的重写而在继承类中产生新的方法,这时基类方法和继承方法之间没有任何的关系了,可是override就不同了,它也是对基类中方法的重写,但此时只是继承类重写了一次基类的方法。

    下面给大家举个例子:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Test
    {
        class A
        {
            public virtual void Test()
            {
                Console.WriteLine("A");
            }
        }
        class A1 : A
        {
            public override void Test()
            {
                Console.WriteLine("A1");
            }
        }
        class A2 : A
        {
            public new void Test()
            {
                Console.WriteLine("A2");
            }
        } 
     
     
        class Program
        {
            static void Main(string[] args)
            {
                A a1 = new A1();
                A a2 = new A2();
    
                a1.Test();
                a2.Test();
                (a2 as A2).Test();
    
                Console.Read(); 
    
            }
        }
    }
    
  • 相关阅读:
    Matrix
    Color the ball
    Coupons
    密码箱
    Milking Grid
    Substrings
    亲和串
    Jzzhu and Cities
    transition多个属性同时渐变(left/top)
    CSS3提供的transition动画
  • 原文地址:https://www.cnblogs.com/wangguowen27/p/2605776.html
Copyright © 2011-2022 走看看