查看override的IL

Override示例

下面我们看一个Override的Example

namespace MyCollection
{
    public class MyBase
    {
        public virtual string Meth1()
        {
            return "MyBase-Meth1";
        }
 
        public virtual string Meth2()
        {
            return "MyBase-Meth2";
        }
 
        public virtual string Meth3()
        {
            return "MyBase-Meth3";
        }
    }
 
    class MyDerived : MyBase
    {
        // 使用 override 关键字重写虚方法 Meth1:
        public override string Meth1()
        {
            return "MyDerived-Meth1";
        }
 
        // 使用 new 关键字显式隐藏
        // 虚方法 Meth2:
        public new string Meth2()
        {
            return "MyDerived-Meth2";
        }
 
        // 由于下面声明中没有指定任何关键字
        // 因此将发出一个警告来提醒程序员
        // 此方法隐藏了继承的成员 MyBase.Meth3():
        public string Meth3()
        {
            return "MyDerived-Meth3";
        }
    }
    public class VirtualExample
    {
        public static void Main()
        {
            MyDerived mD = new MyDerived();
            MyBase mB = (MyBase)mD;
 
            System.Console.WriteLine(mD.Meth1());
            System.Console.WriteLine(mD.Meth2());
            System.Console.WriteLine(mD.Meth3());
            System.Console.WriteLine("以上为类MyDerived的显示结果!\n");
 
            System.Console.WriteLine(mB.Meth1());
            System.Console.WriteLine(mB.Meth2());
            System.Console.WriteLine(mB.Meth3());
            System.Console.WriteLine("以上为MyBase的显示结果!\n");
            System.Console.WriteLine("按任意键退出...");
            System.Console.ReadLine();
        }
    }
}

相关文章:

  • 2021-11-22
  • 2021-05-26
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2021-08-15
猜你喜欢
  • 2021-11-24
  • 2022-02-28
  • 2022-12-23
  • 2021-09-01
  • 2022-03-02
相关资源
相似解决方案