C#base()构造函数顺序 [英] C# base() constructor order

查看:52
本文介绍了C#base()构造函数顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
C#构造函数执行顺序

class Foo
{
    public int abc;
    Foo()
    {
       abc = 3;
    }

}

class Bar : Foo
{
    Bar() : base()
    {
       abc = 2;
    }
}

在上面的示例中,当创建Bar对象时,BarObject.abc的值是什么?是先调用基本构造函数,还是先运行Bar()/then/base()构造函数?

In the example above, when an object of Bar is created, what will be the value of BarObject.abc? Is the base constructor called first, or is Bar() run, /then/ the base() constructor?

推荐答案

将是2.构造函数按从基类第一个到继承类的顺序运行.

It'll be 2. Constructors run in order from base class first to inherited class last.

请注意,初始化程序(静态变量和实例变量)的运行方向相反.

Note that initialisers (both static and instance variables) run in the opposite direction.

完整序列在此处: http://www.csharp411.com/c-object-initialization/

这篇关于C#base()构造函数顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆