CLR call Virtual Methods

来源:互联网 发布:java源程序的扩展名是 编辑:程序博客网 时间:2024/04/30 11:05

CLR via C# has following rules:

The callvirt IL instruction can be used to call instance and virtual methods, not static methods. When the callvirt instruction is used to call an instance or virtual method, you must specify a variable that refers to an object.When the callvirt IL instruction is used to call a nonvirtual instance method, the type of the variable indicates which type defines the method that the CLR should call. When the callvirt IL instruction is used to call a virtual instance method, the CLR discovers the actual type of the object being used to make the call and then calls the method polymorphically.

 

Take a look at following two examples:

Example1.

    class BaseClass
    {
        internal void TestMethod1()
        {
            Console.WriteLine("BaseMethod1");
            TestMethod2();
        }

        internal virtual void TestMethod2()
        {
            Console.WriteLine("BaseMethod2");
        }
    }

    class SubClass : BaseClass
    {
        internal new void TestMethod1()
        {
            Console.WriteLine("SubMethod1");
            TestMethod2();
            base.TestMethod1();
        }

        internal override void TestMethod2()
        {
            Console.WriteLine("SubMethod2");
        }
    }

 

        static void Main(string[] args)
        {
            SubClass  sub = new SubClass ();
            sub.TestMethod1();
        }

 

What's the output on console?

SubMethod1

SubMethod2

BaseMethod1

SubMethod2

 

Example2:

    class BaseClass
    {
        internal void TestMethod1()
        {
            Console.WriteLine("BaseMethod1");
            TestMethod2();
        }

        internal virtual void TestMethod2()
        {
            Console.WriteLine("BaseMethod2");
        }
    }

    class SubClass : BaseClass
    {
        internal new void TestMethod1()
        {
            Console.WriteLine("SubMethod1");
            TestMethod2();
            base.TestMethod1();
        }

        internal override void TestMethod2()
        {
            Console.WriteLine("SubMethod2");
        }
    }

    class SubSubClass : SubClass
    {
        internal new void TestMethod1()
        {
            Console.WriteLine("SubSubMethod1");
            TestMethod2();
            base.TestMethod1();
        }

        internal override void TestMethod2()
        {
            Console.WriteLine("SubSubMethod2");
        }
    }

        static void Main(string[] args)
        {
            SubSubClass sub = new SubSubClass();
            sub.TestMethod1();
        }

 

What's the output on console?

SubSubMethod1

SubSubMethod2

SubMethod1

SubSubMethod2

BaseMethod1

SubSubMethod2

 

原创粉丝点击