解决方案 »

  1.   

    Class Test
    {
       int a;
       string b;   public Test()
       {
         a=1;
         b="test";
       }   public void Function01()
       {
         a=2;
         b="csdn";
       }
    }
      

  2.   

    Class Test
    {
       int a;
       string b;   public Test(int i,string str) //带参数的构造函数
       {
           a=i;
           b=str;
       }   public void Function01()
       {
            //this.a;   可直接调用
            //b=str;   }
       
       //或者创建函数,返回初始化的值
       public int GetIntValue(){  
           return a;
       }
       public string GetStringValue(){
           return b;
       }
    }//外部调用
    Test test=new Test(1,"Test");
    test.GetStringValue();//返回b的值
      

  3.   

    构造函数是在创建类的实例时自动调用的,并且,父类中的构造函数也都会被调用如果父类中有不止一个构造函数,还可以在子类中用关键字base指定调用那个构造函数
      

  4.   

    定义当然不能在构造函数内,最好的方式是定义属性
    public string b
    {
    get{...}
    set{...}
    }