请高人解答这个调用过程。是怎么调用的。static 变量与构造函数在这里的功能是什么了?借宝地说一下我遇到的问题:
示例1 :
public partial class Test : System.Web.UI.Page
{
class A
{
public static int X;
static A()
{
X = Test.Y + 1;
}
}
public static int Y = A.X + 1;
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + Test.Y.ToString();
}
运行结果为:1,2
示例2:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + B.Y.ToString();
}
}
class A
{
public static int X;
static A()
{
X = B.Y + 1;
}
}
class B
{
public static int Y=A.X+1;
static B()
{
}
}
运行结果为:2,1示例3:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + B.Y.ToString();
}
}
class A
{
public static int X;
static A()
{
X = B.Y + 1;
}
}
class B
{
public static int Y ;
static B()
{
Y = A.X + 1;
}
}
运行结果为:2,1示例4:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + B.Y.ToString();
}
}
class A
{
public static int X;
static A()
{
X = B.Y + 1;
}
}
class B
{
public static int Y =A.X + 1 ;
B()
{
}
}
运行结果为:1,2
4次运行结果都列出来。请高手指教每个程序的运行过程。为什么这个值。调试的时候有些断点为什么达不到。或者同一个程序。每次运行的执行顺序都不一样。
这个问题我想对理解静态构造函数以及静态变量都是一个很好的例子。希望大家都有进步。
示例1 :
public partial class Test : System.Web.UI.Page
{
class A
{
public static int X;
static A()
{
X = Test.Y + 1;
}
}
public static int Y = A.X + 1;
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + Test.Y.ToString();
}
运行结果为:1,2
示例2:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + B.Y.ToString();
}
}
class A
{
public static int X;
static A()
{
X = B.Y + 1;
}
}
class B
{
public static int Y=A.X+1;
static B()
{
}
}
运行结果为:2,1示例3:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + B.Y.ToString();
}
}
class A
{
public static int X;
static A()
{
X = B.Y + 1;
}
}
class B
{
public static int Y ;
static B()
{
Y = A.X + 1;
}
}
运行结果为:2,1示例4:
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Label1.Text = A.X.ToString() + "," + B.Y.ToString();
}
}
class A
{
public static int X;
static A()
{
X = B.Y + 1;
}
}
class B
{
public static int Y =A.X + 1 ;
B()
{
}
}
运行结果为:1,2
4次运行结果都列出来。请高手指教每个程序的运行过程。为什么这个值。调试的时候有些断点为什么达不到。或者同一个程序。每次运行的执行顺序都不一样。
这个问题我想对理解静态构造函数以及静态变量都是一个很好的例子。希望大家都有进步。
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2