C# 父子类的构造函数执行流程 2019/06/14

 //子类
public class AlarmParameter_PT1 : BaseBatchCommand
{

        public AlarmParameter_PT1(Plugin plugin, string name, string description, bool enable)
            : base(plugin, name, description, enable)
        {
            this.ID = "AlarmParameter_PT1";
        }

}




//父类
public class BaseBatchCommand : IBatchCommand
{
         public BaseBatchCommand(Plugin plugin, string name, string description, bool enable)
        {
            this.id = "BaseBatchCommand";
            this.Plugin = plugin;
            this.Name = name;
            this.Description = description;
            this.IsEnable = enable;
        }
}

//////实例化

     object[] args = new object[] { plugin, name, description, enable };
     return (IBatchCommand)Activator.CreateInstance(type, args);

程序流程:

1.子类构造函数的base

2.父类构造函数

3.执行父类构造函数(所以此时id = "BaseBatchCommand")

4.再执行子类构造函数(此时id被修改成"AlarmParameter_PT1")

总结:

当程序初始化子类的时候,执行流程是:子类base -> 父类 -> 父类构造函数 ->子类构造函数

 

你可能感兴趣的:(知识点记录)