【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;
        }

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

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

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

            Console.Read();
        }      
    }
}

 

posted @ 2017-08-27 15:09  Dsp Tian  阅读(398)  评论(0编辑  收藏  举报