日期:2014-05-18  浏览次数:20485 次

怎么才能完成这个构造函数例子?
class class1 : System.Web.UI.Page 

  string a; 
  public class1() //构造函数定义 
  { 
  a="你好"; 
  } 
  protected void Page_Load(object sender, EventArgs e) 
  { 
  class1 box = new class1(); //初始化sd,并且隐式调用构造函数 
  Response.Write(box.a); 
  } 

为什么输出的是空呢? 
怎么才能完成这个构造函数例子?

------解决方案--------------------
没明白..

但猜想楼主肯定是想在其它操作(如页面中的按钮事件)中获得 a 的值.
建议看看ASP.NET运行机制.

C# code

class class1 : System.Web.UI.Page  
{  
    private string a
    {
      get{
           if(ViewState["a"] != null)
             return (string)ViewState["a"];
           return "";
         }
      set{
           ViewState["a"] = value;
         }
     }

    public class1()    //构造函数定义  
    {  
        a="你好";  
    }  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        class1 box = new class1();    //初始化sd,并且隐式调用构造函数  
        Response.Write(box.a);  
    }  
}