zoukankan      html  css  js  c++  java
  • 关于在C#中构造函数中调用虚函数的问题

    在C#中如果存在类的继承关系,应避免在构造函数中调用虚函数。这是由于C#的运行机制造成的,原因如下:

    新建一个类实例时,C#会先初始化该类(对类变量赋值,并将函数记在函数表中),然后再初始化父类。构造函数的执行则相反,先执行父类的,再执行本身的。

    如果类包含对父类虚函数的重载,在父类构造函数中调用虚函数时,会执行函数表中继承类的虚函数,这就有问题了。

    下面是个测试例子:

    public class Base
        {
            public Base()
            {
                System.Console.WriteLine("Base.Base");
                ABitDangerousCall();
            }
    
            public virtual void ABitDangerousCall()
            {
                System.Console.WriteLine("Base.ABitDangerousCall");
            }
    
            private class Inner
            {
                public Inner()
                {
                    System.Console.WriteLine("Base.Inner.Inner");
                }
            }
            private Inner inner = new Inner();
        }
    
        class Derived : Base
        {
            public Derived()
            {
                System.Console.WriteLine("Derived.Derived");
                ctorInitializedMember = 5;
            }
    
            // ctorInitializedMember is default initialized to zero before the constructor initializes it.
            private int ctorInitializedMember;
            private int derivedInt = 6;
    
            public override void ABitDangerousCall()
            {
                System.Console.WriteLine(String.Format("Derived.ABitDangerousCallctorInitializedMember={0} derivedInt={1}", ctorInitializedMember, derivedInt));
            }
    
            private class Inner
            {
                public Inner()
                {
                    System.Console.WriteLine("Derived.Inner.Inner");
                }
            }
            private Inner inner = new Inner();
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
                Derived d = new Derived();
                Console.Read();
            }
        }

    如果真有在构造函数中调用虚函数的必要,应将该类或该重载的虚函数设为seal,以避免被继承。

  • 相关阅读:
    基于node.js 的 websocket的移动端H5直播开发
    c# 基于RTMP推流 PC+移动端 拉流播放
    Android Studio解决Error:moudle not specified
    能ping通域名,却不能上网
    转 Postman访问Webapi的Get/Post/Put/Delte请求
    Sqlite 参数化 模糊查询 解决方案
    autofac使用总结
    windows7 安装pytorch
    linux nginx 如何配置多个端口
    委托应用实例演变
  • 原文地址:https://www.cnblogs.com/xpvincent/p/4459452.html
Copyright © 2011-2022 走看看