zoukankan      html  css  js  c++  java
  • 了解一下new关键字实现阻断继承的原理

    一些经典.net面试题里,经常会有关于new关键字的考察,其中肯定会问到new关键字用在方法前有什么用之类的,通常同学们都能答出是阻断继承或者说是为了表明与父类同名方法独立开。但是进一步询问为什么或讲讲原理时,大部分的猿人还是回答不上来的。下面简单分析一下,个人理解的实现原理。

    从一个经典的面试代码题入手,有如下题目(估计面霸们一看就知道答案了)

    View Code
     1     public class Book
     2     {
     3         public virtual string GetDescription()
     4         {
     5             return "a normal book";
     6         }
     7     }
     8 
     9     public class AspNetBook : Book
    10     {
    11         public new string GetDescription()
    12         {
    13             return "an aspnet book";
    14         }
    15     }
    16 
    17     public class WcfBook : Book
    18     {
    19         public override string GetDescription()
    20         {
    21             return "an wcf book";
    22         }
    23     }
    24 
    25     class Program
    26     {
    27 
    28         static void Main(string[] args)
    29         {
    30             Book b = null;
    31 
    32             AspNetBook a = new AspNetBook();
    33             Console.WriteLine(a.GetDescription()); // callvirt   instance string CallMethodTest.AspNetBook::GetDescription()
    34 
    35             b = a;
    36             Console.WriteLine(b.GetDescription()); // callvirt   instance string CallMethodTest.Book::GetDescription()
    37 
    38             WcfBook c = new WcfBook();
    39             Console.WriteLine(c.GetDescription()); //  callvirt   instance string CallMethodTest.Book::GetDescription()
    40 
    41             b = c;
    42             Console.WriteLine(b.GetDescription()); // callvirt   instance string CallMethodTest.Book::GetDescription()
    43 
    44             Console.ReadLine();
    45         }
    46     }

    输出结果是:

    an aspnet book

    a normal book

    a wcf book

    a wcf book

    具体如何实现还是从分析IL入手: 

      红色标记的调用代码旁边的注释是对应的IL代码,有个明显的区别是调用new关键字修饰的AspNetBook实例的方法时,是直接找的AspNetBook的方法,而没有从父类的方法开始找。当通过父类方法调用AspNetBook的方法时,从父类的方法开始调用,IL给的是callvirt的方法,该方法用于调用继承多态的方法。callvirt回去查找最终的实现方法,当遇到关键字new时,停止查找,即直接调用父类Book的方法。

         而对于已经重写了父类方法的WCFBook实例,不管是直接通过实例调用,还是通过父类的方法调用,生成的IL代码都是一致的,生成的IL都是通过callvirt的方法调用,先从父类的方法开始查找,寻找最终的实现方法。

         也就是说上述IL代码的生成,取决于代码的实现,暂时理解到这。

  • 相关阅读:
    【topcoder SRM 702 DIV 2 250】TestTaking
    【24.67%】【codeforces 551C】 GukiZ hates Boxes
    【TP SRM 703 div2 500】 GCDGraph
    【TP SRM 703 div2 250】AlternatingString
    【35.02%】【codeforces 734A】Vladik and flights
    Android总结篇系列:Android Service
    java之常用正则表达式
    一种软件定义的固态盘融合存储方法
    MongoDB常用语句
    编程界十大顶级IDE
  • 原文地址:https://www.cnblogs.com/shenba/p/2267999.html
Copyright © 2011-2022 走看看