那位能来个例子呢?在这个问题上想不通!像HttpContext.Current.Response.Write这个累加的字符应该不是static类型吧?但它却是能在没有传递参数的情况下实现累加字符 public class MyModule : IHttpModule { public void Init(HttpApplication application) { application.BeginRequest += new EventHandler(application_BeginRequest); } void application_BeginRequest(object sender, EventArgs e) { Test test = new Test(); test.Show(); test.Show2(); } public void Dispose() { } } public class Test { public void Show() { System.Web.HttpContext.Current.Response.Write("111111111"); } public void Show2() { System.Web.HttpContext.Current.Response.Write("222222"); } }
static 这个变量是共享的吧?在上面的问题上,有点糊涂了
eg:class Response { public string result=string.Empty; public void Write(string str){ result+=str; } }public class MyModule : IHttpModule { public MyModule () { System.Web.HttpContext.Current.Response=new Response(); } public void Init(HttpApplication application) { application.BeginRequest += new EventHandler(application_BeginRequest); } void application_BeginRequest(object sender, EventArgs e) { Test test = new Test(); test.Show(); test.Show2(); } public void Dispose() { } }
我知道和静态没关系,我为什么要提HttpContext.Current,不是想知道它的功能,我想知道它是否是在同一条线程下,能修改它所在这条线程的数据和这种方式如何用简单的例子实现。 public class Test { public void Show() { System.Web.HttpContext.Current.Response.Write("111111111"); } public void Show2() { System.Web.HttpContext.Current.Response.Write("222222"); } } 上面这条例子,只要这个类在线程执行中的函数下被调用, 它就能在不需要将System.Web.HttpContext类传递入test类,而引用像静态一样的调用方法方式引用
你是不是想说线程中变量同步的问题呢?很多变量都可以在线程中修改,关键看如果同时很多线程修改一个变量是否会引发问题。
如果不会则直接引用,如果会发生问题则当一个线程修改一个变量或对象的时候,需要Lock对象,
直到该线程修改完成,释放Lock,其他线程才可以修改这个变量或对象,
知道这种不是像static共享同一个类里的变量,可又能在同一个线程不同位置(不同函数中调用,好像没有通过参数的传递)修改,而输出时,修改的变量能呈现出来
{
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
} void application_BeginRequest(object sender, EventArgs e)
{
Test test = new Test();
test.Show();
test.Show2();
} public void Dispose()
{
}
}
public class Test
{
public void Show()
{
System.Web.HttpContext.Current.Response.Write("111111111");
}
public void Show2()
{
System.Web.HttpContext.Current.Response.Write("222222");
} }
{
public string result=string.Empty;
public void Write(string str){
result+=str;
}
}public class MyModule : IHttpModule
{
public MyModule ()
{
System.Web.HttpContext.Current.Response=new Response();
} public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
} void application_BeginRequest(object sender, EventArgs e)
{
Test test = new Test();
test.Show();
test.Show2();
} public void Dispose()
{
}
}
在2个show我想用来说明(能在不同的类用这种静态方式访问),
Write(string str)内部被用来保存str的不是静态变量,会是怎么的一种实现
{
public void Show()
{
System.Web.HttpContext.Current.Response.Write("111111111");
}
public void Show2()
{
System.Web.HttpContext.Current.Response.Write("222222");
} }
上面这条例子,只要这个类在线程执行中的函数下被调用,
它就能在不需要将System.Web.HttpContext类传递入test类,而引用像静态一样的调用方法方式引用