当前位置:首页 > C#教程 > C#高级

【C#学习笔记】类构造函数使用

            using
             System;


            namespace
             ConsoleApplication
{
    
            class
             stu
    {
        
            private
            string
             name;
        
            private
            int
             age;

        
            public
             stu()
        {
            name = "";
            age = -1;
        }

        public stu(string n, int a)
        {
            name = n;
            age = a;
        }

        publicvoid Print()
        {
            Console.Write(name + ":" + age);
        }
    }

    class Program
    {
        staticvoid Main(string[] args)
        {
            stu a=new stu("xiaoming",10);
            a.Print();

            Console.WriteLine();
            stu b = new stu();
            b.Print();

            Console.Read();
        }      
    }
}

 

原文:http://www.cnblogs.com/tiandsp/p/7440428.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!