//构造器的相互调用用:

using System;
namespace Constructor4
{
    class Test
    {
        int i;
        string s;
        Test()
        {
            i = 1;//在这里初始化,其它构造器调用它,比在成员变量直接初始化好,减少了冗余代码的产生
            s = "abcd";//在这里初始化,其它构造器调用它,比在成员变量直接初始化好,减少了冗余代码的产生
        }
        Test(int i):this()
        {
            this.i = i;
        }
        Test(string s):this()
        {
            this.s = s;
        }
        Test(int i, string s):this()
        {
            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:26  glave  阅读(129)  评论(0)    收藏  举报