//类构造器的重载及THIS关键字

//this关键字,指类在被实例化时,会自动把实例化产生的对象赋给this,每个对象都产生一个各自的this

//当成员变量名与参数名相同时this就显得重要,this表示实例化的对象,引用的是对象成员,如果没使用this则编译器会自动去寻找最近的变量,实际开发中尽量避免使用相同的变量名

using System;
namespace Constructor4
{
    class Test
    {
        int i;
        string s;
        Test()
        {
       
        }
        Test(int i)
        {
            this.i = i;
        }
        Test(string s)
        {
            this.s = s;
        }
        Test(int i, string s)
        {
            this.i = i;
            this.s = s;
        }
        static void Main()
        {
            Test test1 = new Test();
            Console.WriteLine(test1.i.ToString() + test1.s);
            Test test2 = new Test(2);
            Console.WriteLine(test2.i.ToString() + test2.s);

            Test test3 = new Test("aaa");
            Console.WriteLine(test3.i.ToString() + test3.s);

            Test test4 = new Test(4,"bbb");
            Console.WriteLine(test4.i.ToString() + test4.s);

        }
    }
}

posted on 2008-01-21 17:22  glave  阅读(157)  评论(0)    收藏  举报