构造函数命令 [英] Constructor order

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

问题描述

我很好奇两件事。


一个是构造函数处理的顺序,另一个是调试器没有的原因

t显示静态对象构造函数(除非你设置

a断点。


例如,这是我的代码:


************************************************ ** ********************

// DemonstrateDefaultConstructor - 演示默认

//构造函数的工作原理;使用构造函数创建一个类

//然后通过几个场景步骤

//

使用System;


命名空间DemonstrateDefaultConstructor

{

// MyObject - 创建一个带有嘈杂构造函数的类

//和一个内部对象

公共类MyObject

{

//此成员是该类的属性

static MyOtherObject staticObj = new MyOtherObject();

// int tom = 0;

//这个成员是对象的属性

MyOtherObject dynamicObj;


public MyObject()

{

Console.WriteLine(&#MyObject constructor starting");

//

dynamicObj = new MyOtherObject();

Console.WriteLine(" MyObject构造函数结束");

} < br $>
}


// MyOtherObject - 这个类也有一个嘈杂的构造函数

//但没有内部成员

公共类MyOtherObject

{

public MyOtherObject()

{

Console.WriteLine(" MyOtherObject构建);

}

}


公共类Class1

{

public static void Main(string [] args)

{

Console.WriteLine(" Main()starting");

//创建一个对象

MyObject localObject;

Console.W riteLine(" Before localObject defined");

localObject = new MyObject();

Console.WriteLine(" After localObject new statement");

//等待用户确认结果

Console.WriteLine(按Enter键终止...);

Console.Read( );

}

}

}

************* ************************************* ************* *********


生成的控制台显示:

**************** ***********************

Main()开始

在localObject定义之前

MyOtherObject构造

MyObject构造函数启动

MyObject构造函数结束

localObject新语句之后

按Enter键终止......

************************************* ************* ********************


我很好奇为什么MyOtherObject的构造函数在任何之前创建了

其他是在MyObject类中完成的(包括

MyObject构造函数)。以下是它的处理顺序(根据

调试器 - 当你在/ b $ b $ MyOtherObject构造函数中的Console.Write上有断点时。

********************************************* ***** ************

localObject

= new MyObject(); //来自Main


Console.WriteLine(" MyOtherObject construct"); // MyotherObject

constructer


static MyOtherObject staticObj = new MyOtherObject(); // MyObject

定义


public MyObject()// MyObject构造函数


Console.WriteLine(" MyObject构造函数启动"); // MyObject构造函数

******************************** ****************** ************


另外,如果你不是在MyOtherConstuctor行上设置断点,当你跟踪代码时(F11),它甚至不显示



谢谢,


汤姆。

解决方案

Thomas Scheiderich< tf*@deltanet.com> ;写道:


< snip>

生成的控制台显示:
*********** ****************************
Main()启动
在localObject定义之前
MyOtherObject构建
MyObject构造函数启动
MyObject构造函数结束
在localObject新语句之后
按Enter键终止...
************** ************************************ ************** ******

我很好奇为什么在MyObject类中完成任何其他操作之前创建MyOtherObject的构造函数(包括
MyObject构造函数)。


因为你有一个类型初始化器(隐含地,由于

"静态MyOtherObject staticObj = new MyOtherObject()"行,是
在MyObject类中的其他任何东西之前运行。


但是你应该有*两行*行说MyOtherObject

构建,实际上这就是我运行代码时得到的结果。

另外,如果你没有在MyOtherConstuctor线上设置断点,它就没有了甚至在你追踪代码时出现(F11)。




我不知道调试器的变幻莫测,我很害怕。可能

它没有显示类型初始化器。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


你好,


只是一点点评论。


你会在''MyObject构造函数

起始''即使第一个MyOtherObject不是静态的。这是因为在所有实例构造函数的开头都复制了所有的初始化代码,因此构造函数体中的所有代码都将跟随MyOtherObject的创建。

当然Jon怎么说有两个''MyOtherObject构建''行。


-

HTH

B \ rgds



" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

Thomas Scheiderich< tf*@deltanet.com>写道:

< snip>

生成的控制台显示:
*************** ************************
Main()启动
在localObject定义之前构建MyOtherObject
MyObject构造函数启动
MyObject构造函数结束
在localObject新语句之后
按Enter键终止...
****************** ******************************** ****************** **

我很好奇为什么在MyObject类中完成任何其他工作之前创建MyOtherObject的构造函数(包括
MyObject构造函数)。



因为你有一个类型的初始化程序(隐含地,由于
静态MyOtherObject staticObj = new MyOtherObject()行,这是在之前运行的MyObject类中的任何其他内容。

然而,您应该有*两行*表示MyOtherObject构建,实际上这就是我得到的内容我运行代码。

另外,如果你没有在MyOtherConstuctor线上设置断点,那么当你跟踪时它甚至都没有显示出来通过代码(F11)。



我不知道调试器的变幻莫测,我很害怕。可能
它没有显示类型初始化器。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复群组,请不要给我发邮件






Jon Skeet [C#MVP]写道:

Thomas Scheiderich< tf*@deltanet.com>写道:

< snip>

生成的控制台显示:
*************** ************************
Main()启动
在localObject定义之前构建MyOtherObject
MyObject构造函数启动
MyObject构造函数结束
在localObject新语句之后
按Enter键终止...
****************** ****************************** ******************** **

我很好奇为什么在MyObject类(包括
MyObject构造函数)完成任何其他工作之前创建了MyOtherObject的构造函数。

因为你有一个类型的初始化程序(隐含地,由于
静态MyOtherObject staticObj = new MyOtherObject()行,它在
之前运行MyObject类。

然而,您应该有*两行*表示MyOtherObject构建,实际上这就是我运行代码时得到的结果。



我没有2MyOtherObject构建的原因消息是

,因为我忘记了我已经注释掉了这一行:


// dynamicObj = new MyOtherObject();

当我取消注释该行时,我得到的结果我认为你在谈论


************** ************************************ *************

Main()开始

在localObject定义之前

构建MyOtherObject

MyObject构造函数启动

MyOtherObject构造

MyObject构造函数结束

localObject新语句之后

按Enter键终止...

* ************************************************* ************

另外,如果你没有在MyOtherConstuctor上设置断点当你追查代码时,它甚至都没有出现(F11)。



我不知道d的变幻莫测ebugger,我很害怕。可能
它没有显示类型初始化器。



BTW,你如何告诉MyOtherObject是一个类属性而不仅仅是

另一个宾语。它似乎被定义为一个?有什么区别呢

来自正常的班级定义。


谢谢,


汤姆。




I am curious as to 2 things.

One is the order that the constructors are handled and the other is why
the debugger doesn''t show the static objects constructor (unless you set
a breakpoint.

For example, here is my code:

************************************************** ********************
// DemonstrateDefaultConstructor - demonstrate how default
// constructors work; create a class
// with a constructor and then step
// through a few scenarios
using System;

namespace DemonstrateDefaultConstructor
{
// MyObject - create a class with a noisy constructor
// and an internal object
public class MyObject
{
// this member is a property of the class
static MyOtherObject staticObj = new MyOtherObject();
//int tom = 0;
// this member is a property of the object
MyOtherObject dynamicObj;

public MyObject()
{
Console.WriteLine("MyObject constructor starting");
//
dynamicObj = new MyOtherObject();
Console.WriteLine("MyObject constructor ending");
}
}

// MyOtherObject - this class also has a noisy constructor
// but no internal members
public class MyOtherObject
{
public MyOtherObject()
{
Console.WriteLine("MyOtherObject constructing");
}
}

public class Class1
{
public static void Main(string[] args)
{
Console.WriteLine("Main() starting");
// create an object
MyObject localObject;
Console.WriteLine("Before localObject defined");
localObject = new MyObject();
Console.WriteLine("After localObject new statement");
// wait for user to acknowledge the results
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
}
}
************************************************** **********************

The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** ********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor). Here is the order it is handled (according to
the debugger - when you have a breakpoint on Console.Write in the
MyOtherObject constructor.

************************************************** ************
localObject
= new MyObject(); // from Main

Console.WriteLine("MyOtherObject constructing"); //MyotherObject
constructer

static MyOtherObject staticObj = new MyOtherObject(); //MyObject
definition

public MyObject() //MyObject Constructor

Console.WriteLine("MyObject constructor starting");//MyObject Constructor
************************************************** ************

Also, if you don''t set the breakpoint on the MyOtherConstuctor line, it
doesn''t even show up when you are tracing through the code (F11).

Thanks,

Tom.

解决方案

Thomas Scheiderich <tf*@deltanet.com> wrote:

<snip>

The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** ********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).
Because you''ve got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that''s what I get when I run the code.
Also, if you don''t set the breakpoint on the MyOtherConstuctor line, it
doesn''t even show up when you are tracing through the code (F11).



I don''t know about the vagaries of the debugger, I''m afraid. Possibly
it doesn''t show type initializers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Hi,

Just a little comment.

You would''ve had ''MyOtherObject constructing'' before ''MyObject constructor
starting'' even if the first MyOtherObject wasn''t static. That is because all
initliazing code is copied at the begining of all instance constructors,
thus all code in the constructor body will follow MyOtherObject creation.
Of course how Jon said there are two ''MyOtherObject constructing'' lines.

--
HTH
B\rgds
100
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

Thomas Scheiderich <tf*@deltanet.com> wrote:

<snip>

The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** ********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).



Because you''ve got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that''s what I get when I run the code.

Also, if you don''t set the breakpoint on the MyOtherConstuctor line, it
doesn''t even show up when you are tracing through the code (F11).



I don''t know about the vagaries of the debugger, I''m afraid. Possibly
it doesn''t show type initializers.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





Jon Skeet [C# MVP] wrote:

Thomas Scheiderich <tf*@deltanet.com> wrote:

<snip>

The resulting console shows:
***************************************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************ **********************

I am curious as to why the constuctor for MyOtherObject is created
before anything else is done in the MyObject class (including the
MyObject constructor).

Because you''ve got a type initializer (implicitly, due to the
"static MyOtherObject staticObj = new MyOtherObject()" line, which is
being run before anything else in the MyObject class.

You should, however, have *two* lines saying MyOtherObject
constructing, and indeed that''s what I get when I run the code.



The reason I don''t have 2 "MyOtherObject constructing" messages is
because I had forgot that I had commented out the line:

// dynamicObj = new MyOtherObject();
When I uncomment the line I get the results I think you were talking about:

************************************************** *************
Main() starting
Before localObject defined
MyOtherObject constructing
MyObject constructor starting
MyOtherObject constructing
MyObject constructor ending
After localObject new statement
Press Enter to terminate...
************************************************** *************

Also, if you don''t set the breakpoint on the MyOtherConstuctor line, it
doesn''t even show up when you are tracing through the code (F11).



I don''t know about the vagaries of the debugger, I''m afraid. Possibly
it doesn''t show type initializers.


BTW, how do you tell the MyOtherObject is a class property and not just
another object. It seems to be defined as one? What differentiates it
from a normal Class definition.

Thanks,

Tom.




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

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